#include <SDL.h>
#include <SDL_draw.h>
#include <SDL_ttf.h>
#include <cstdlib>
#include <time.h>
#include <SDL_image.h>
using namespace std;
class cruiser;
class level
{
public:
SDL_Surface * screen;
level(SDL_Surface * battleground)
{
screen = battleground;
SDL_Surface * background = SDL_LoadBMP("textures/phone.bmp");
SDL_BlitSurface(background, NULL, screen, NULL);
SDL_Flip(screen);
cruiser(screen, * this);
}
};
class cruiser
{
public:
cruiser(SDL_Surface * screen, level game_level)
{
SDL_Surface * battleship = IMG_Load("textures/battleship.png");
SDL_Rect dstrect;
SDL_Event event;
dstrect.x = 1024/2;
dstrect.y = 768/2;
SDL_BlitSurface(battleship, NULL, game_level.screen, & dstrect);
SDL_Flip(game_level.screen);
while(1)
{
if(SDL_PollEvent(&event))
{
if ( (event.key.keysym.sym == SDLK_LEFT) and (dstrect.x > 70) )
{
dstrect.x -= 5;
battleship = IMG_Load("textures/battleship.png");
SDL_BlitSurface(battleship, NULL, game_level.screen, & dstrect);
SDL_Flip(game_level.screen);
event.key.keysym.sym = NULL;
}
if ( (event.key.keysym.sym == SDLK_RIGHT) and (dstrect.x < (1024 - 200)) )
{
dstrect.x += 5;
battleship = IMG_Load("textures/battleship.png");
SDL_BlitSurface(battleship, NULL, game_level.screen, & dstrect);
SDL_Flip(game_level.screen);
event.key.keysym.sym = NULL;
}
if ( event.key.keysym.sym == SDLK_ESCAPE )
{
exit(0);
}
SDL_Delay(100);
battleship = IMG_Load("textures/battleship.png");
SDL_BlitSurface(battleship, NULL, game_level.screen, &dstrect);
SDL_Flip(game_level.screen);
}
}
}
};
int main(int argc, char *argv[])
{
SDL_Surface *screen;
if (SDL_Init(SDL_INIT_EVERYTHING))
{
fprintf(stderr, "Ошибка SDL: %s\n",SDL_GetError());
return 1;
}
atexit(SDL_Quit);
screen = SDL_SetVideoMode(1024, 768, 32, SDL_ANYFORMAT);
if(!screen)
{
fprintf(stderr, "Unable to set 1024x768 video: %s\n",SDL_GetError());
return 1;
}
level(screen);
return 0;
}
Попробуйте сначала реализовать так, как я описал, только добавьте в класс
BaseMatrix
метод возвращающий размер и вызывайте его из классаMatrix