Hi!
The keypresses in my new project does not seem to be registered. I've never had this issue before and I don't think it is SDL:s fault this time.
Here is the code in main:
#include <cstdlib>
#include <iostream>
#include <SDL.h>
#include "playertank.h"
int main ( int argc, char** argv )
{
SDL_Init(SDL_INIT_VIDEO);
SDL_Surface* screen = SDL_SetVideoMode(720, 480, 24, SDL_HWSURFACE|SDL_DOUBLEBUF);
SDL_Surface* background = SDL_LoadBMP("Grass.bmp");
SDL_Surface* tank_body = SDL_LoadBMP("TankBody.bmp");
PlayerTank player1(200,200,32,32,10);
SDL_SetColorKey( tank_body, SDL_SRCCOLORKEY, SDL_MapRGB(screen->format, 0, 255, 255) );
bool quit = false;
while(!quit)
{
//Controls
player1.move(SDL_GetKeyState(NULL));
//logics
//draw
SDL_BlitSurface(background, NULL,screen,NULL);
SDL_BlitSurface(tank_body,NULL,screen,player1.getPos());
SDL_Rect* copy = player1.getPos();
std::cout<<copy->y<<std::endl;
SDL_Delay(100);
SDL_Flip(screen);
}
atexit(SDL_Quit);
return 0;
}
And here is the class:
#include "playertank.h"
PlayerTank::PlayerTank(int x, int y, int w, int h, int hp)
{
//ctor
pos_x = x;
pos_y = y;
width = w;
height = h;
HP = hp;
speed = 0;
}
#include <iostream>
#define MAX_SPEED 5
void PlayerTank::move(Uint8* keystate)
{
if(keystate[SDLK_SPACE])
{
if(speed <= MAX_SPEED)
speed+=0.2;
}
if(keystate[SDLK_UP])
{
pos_y-=(int)speed;
}
if(keystate[SDLK_DOWN])
{
pos_y+=(int)speed;
}
if(keystate[SDLK_LEFT])
{
pos_x-=(int)speed;
}
if(keystate[SDLK_RIGHT])
{
pos_x+=5;
}
std::cout<<speed<<std::endl;
}
SDL_Rect* PlayerTank::getPos()
{
position_copy.x = pos_x;
position_copy.y = pos_y;
position_copy.w = height;
position_copy.h = width;
return &position_copy;
}
As you can see it isn't done yet but comment on the existing parts are welcomed.