Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


Konrad Jablonski

Member Since 11 Jun 2012
Offline Last Active Sep 04 2014 09:09 AM

Topics I've Started

Good Books on AI?

05 July 2013 - 06:50 PM

Hello,

Anyone know any good books on Artificial Intelligence for games.

I'm looking into doing crowd simulation.

Something like this http://www.youtube.com/watch?v=pqBSNAOsMDc

 

Thanks :)


#ScreenShotSaturday question

01 July 2013 - 12:05 PM

Hello everyone :)

 

How long does it take since the screenshot is uploaded to "newest screenshots" section to appearing in the random voting system. I can't seem to find my screenshot in there even though it's been uploaded 29th June. Did I miss some criteria?

 

Also I've seen some images of peoples faces being uploaded, do you guys manually filter through the pictures in order to remove them? Just wondering :)

 

This is great idea, I love it! Nice way to interact with community.

Found out about many wonderful games that are out there :)

 

 

 

 


SDL Key/Mouse Simplified Intput Class

29 June 2013 - 07:31 PM

Hello everybody 

 

Today I decided to create controls class for my engine (when I say engine, It is VERY loosely said).

Basically I am trying to make SDL code a little simpler to use, the idea would be you create controls class and then 

you just do 

if (controls->isKeyDown(SLDK_ESCAPE))
{
std::cout << "Escape was Pressed" << std::endl;
}

if (controls->isKeyHeld(SLDK_ESCAPE))
{
std::cout << "Escape is Held" << std::endl;
}

if (controls->isKeyUp(SLDK_ESCAPE))
{
std::cout << "Escape was Released" << std::endl;
}

if (controls->isMouseUp(SDL_LEFT_MOUSE)
{
std::cout << "Left Mouse was Released" << std::endl;
}

and you can guess the rest for the mouse tongue.png

 

If there is already free controls class like that please let me know, or if there is better way of doing controls in SDL smile.png

 

Unfortunately it does not work the way I intended it too. I managed to get Key Presses working until you start pressing multiple keys, then everything goes messy and it starts thinking keys are pressed when they are not... It will know when key is pressed, held and released as long only that key is being pressed at that every moment.

 

The Mouse does not work more on other hand. It recognizes that the key was pressed/released but for example when I do 

if (controls->isMouseDown(SDL_LEFT_MOUSE)
{
std::cout << "Left Mouse was Pressed" << std::endl;
}

and press any of the mouse buttons, it will read that line even when I use Mouse Wheel button. Also isMouseHeld doesn't work at all. It is never read.

 

Mouse controls also go messy when you start touching multiple mouse keys at once. Gets stuck on release, just like in controls. Thinks the mouse was released until 1 button is pressed and resets it.

 

 

 

If this class would work I think it could be not just useful to me but would save some time for anyone else smile.png Instead of reinventing the wheel each time...

 

I really hate posting large chunks of code, but I have no idea where the problem is in the code... so here is .h and .cpp file. I've commented .h file smile.png

 

Thank you for your time in advance smile.png

 

controls.h

#pragma once
#include "stdafx.h"

class DllExport controls
{
public:
	//event is passed down from the engine class
	controls(SDL_Event* theEvent);
	~controls(void);

	//different types of possible key states
	#define KEY_NONE -1
	#define KEY_PRESSED 0
	#define KEY_RELEASED 1
	#define KEY_HOLDING 2

	//returns true key was pressed
	bool isKeyDown(int SDLK_ID);
	//returns true key is being held
	bool isKeyHeld(int SDLK_ID);
	//returns true key was released
	bool isKeyUp(int SDLK_ID);

	//returns true if Mouse button was pressed
	bool isMouseDown(int MouseButton);
	//returns true if Mouse button is being held
	bool isMouseHeld(int MouseButton);
	//returns true if Mouse button was released
	bool isMouseUp(int MouseButton);

	//Gets SDL event
	SDL_Event* GetEvent();

	//this runs every frame
	void Update();
private:
	SDL_Event* events; //Pointer to an SDL events system

	//Holds Current Mouse states
	int LeftMouseState;
	int RightMouseState;
	int LeftMouseHeld;
	int RightMouseHeld;

	//Press and Release Key States
	int KEYS_STATE[322];  // 322 is the number of SDLK_DOWN events
	//Held Key States
	int KEYS_STATE2[322];
};

controls.cpp

#include "stdafx.h"
#include "controls.h"


controls::controls(SDL_Event* theEvents)
{
	events = theEvents;

	for(int i = 0; i < 322; i++) { // init them all to false
		KEYS_STATE[i] = KEY_NONE;
		KEYS_STATE2[i] = KEY_NONE;
	}

	LeftMouseState = KEY_NONE;
	RightMouseState = KEY_NONE;
	LeftMouseHeld = KEY_NONE;
	RightMouseHeld = KEY_NONE;
}


controls::~controls(void)
{
}

bool controls::isMouseDown(int MouseButton)
{

	if (MouseButton == SDL_BUTTON_LEFT)
	{
		if (LeftMouseState == KEY_PRESSED)
			return true;
	}
	else if (MouseButton == SDL_BUTTON_RIGHT)
	{
		if (RightMouseState == KEY_PRESSED)
			return true;
	}

	return false;
}

bool controls::isMouseHeld(int MouseButton)
{

	if (MouseButton == SDL_BUTTON_LEFT)
	{
		if (LeftMouseState == KEY_HOLDING) 
			return true;
	}
	else if (MouseButton == SDL_BUTTON_RIGHT)
	{
		if (RightMouseState == KEY_HOLDING)
			return true;
	}

	return false;
}

bool controls::isMouseUp(int MouseButton)
{

	if (MouseButton == SDL_BUTTON_LEFT)
	{
		if (LeftMouseState == KEY_RELEASED) 
			return true;
	}
	else if (MouseButton == SDL_BUTTON_RIGHT)
	{
		if (RightMouseState == KEY_RELEASED)
			return true;
	}

	return false;
}

void controls::Update()
{

	switch (events->type) {

	case SDL_MOUSEBUTTONDOWN:


		if (events->button.button == SDL_BUTTON_LEFT);
		{
			if (LeftMouseHeld == KEY_NONE)
				LeftMouseState = KEY_PRESSED;
			else
				LeftMouseState = KEY_NONE;

			LeftMouseHeld = KEY_HOLDING;

		}
		if (events->button.button == SDL_BUTTON_RIGHT);
		{
			if (RightMouseHeld == KEY_NONE)
				RightMouseState = KEY_PRESSED;
			else
				RightMouseState = KEY_NONE;

			RightMouseHeld = KEY_HOLDING;
		}
		break;
	case SDL_MOUSEBUTTONUP:

		if (events->button.button == SDL_BUTTON_LEFT);
		{

			if (LeftMouseHeld == KEY_HOLDING)
				LeftMouseState = KEY_RELEASED;
			else
				LeftMouseState = KEY_NONE;

			LeftMouseHeld = KEY_NONE;

		}
		if (events->button.button == SDL_BUTTON_RIGHT);
		{
			if (RightMouseHeld == KEY_HOLDING)
				RightMouseState = KEY_RELEASED;
			else
				RightMouseState = KEY_NONE;

			RightMouseHeld = KEY_NONE;
		}
		break;
	case SDL_KEYDOWN:

		if (KEYS_STATE2[events->key.keysym.sym] == KEY_NONE)
		{
			KEYS_STATE[events->key.keysym.sym] = KEY_PRESSED;
		}
		else
			KEYS_STATE[events->key.keysym.sym] = KEY_NONE;

		KEYS_STATE2[events->key.keysym.sym] = KEY_HOLDING;


		break; 
	case SDL_KEYUP:

		if (KEYS_STATE2[events->key.keysym.sym] == KEY_HOLDING)
		{
			KEYS_STATE[events->key.keysym.sym] = KEY_RELEASED;
		}
		else
			KEYS_STATE[events->key.keysym.sym] = KEY_NONE;

		KEYS_STATE2[events->key.keysym.sym] = KEY_NONE;

		break;
	default:
		break;

	}
}

bool controls::isKeyDown(int SDLK_ID)
{

	if (KEYS_STATE[SDLK_ID] == KEY_PRESSED)
	{
		return true;
	}
	else 
		return false;
}

bool controls::isKeyHeld(int SDLK_ID)
{

	if (KEYS_STATE2[SDLK_ID] == KEY_HOLDING)
	{
		return true;
	}
	else 
		return false;
}


bool controls::isKeyUp(int SDLK_ID)
{

	if (KEYS_STATE[SDLK_ID] == KEY_RELEASED)
	{
		return true;
	}
	else 
		return false;
}


SDL_Event* controls::GetEvent() { return events; }

Forward Declaration of Class breaks the game

27 June 2013 - 03:22 PM

Hello, I've been stuck on this for couple hours now. Help would be hugely apprenticed :)
 
I've cut most of the code out to make it less confusing but I'm having problem passing CMainGame class into a MainCharacters class function. If I do forward declaration of the CMainGame it doesn't error out, but crashes as soon as I use one of the functions from the CMainGame.
 

 

// MainCharacter.h ----------------------------------------------------------------------------------------------------------------
#pragma once
 
#include "stdafx.h"
#include <engine.h>
#include "collisionListener.h"
#include "MainGame.h"
 
class CMainGame; //if this forward declaration of class is added it fixes it and game runs with out errors until I run one of the functions from this class in Update function.
 
class CMainCharacter
{
public: 
CMainCharacter(void);
CMainCharacter(CPhysics* Physics, collisionListener* theCollisionListener);
~CMainCharacter(void);
 
void Update(CMainGame* theGame); // error C2061: syntax error : identifier 'CMainGame'
private:
CSprite* MainCharacter;
CSprite* Sword;
};
 
//MainGame.h  ----------------------------------------------------------------------------------------------------------------
#pragma once
#include <engine.h>
#include <Mathematics.h>
#include "Enemy.h"
#include "MainCharacter.h"
#include "collisionListener.h"
 
class CMainGame : public engine, Mathematics
{
public:
 
CMainGame();
~CMainGame(void);
 
virtual void OnDraw();
virtual void OnUpdate();
 
private:
collisionListener* theCollisionListener;
 
CMainCharacter* MainCharacter;
 
};
----------------------------------------------------------------------------------------------
 
Thank you for help in advance!
 

GLSL Coloring per pixel?

19 January 2013 - 10:45 AM

First of all thank you for taking time to answer my question.
I am very new to shaders.

I am trying to fade out my lines using shaders,
this picture shows exacly what I need:

 

whatIreallyreallywant%20copy.png




My Fragment Shader:
void main() {
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}

My Vertex Shader:
void main() {
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}

This works, and colors them red. Is there a way to check position of the pixel I am coloring. So then I can compare it to the distance away from the beginning of the line. Using that value to calculate the Alpha of the FragColor?
Unless there is another way?

Thank you smile.png


PARTNERS