• Plantilla SDL

De VGAFIB


  • Titol: Petita plantilla de SDL en C++ per fer jocs.
  • Autor: Threpwood
  • Descripció: Es tracta d'un quadrat que es mou per la pantalla fent anar les tecles de direcció.


Contingut

MAIN.CPP

#include <iostream>
#include "game.h"
    
int main(){
    CGame game;
    
    if (not game.bInit())
        std::cerr << "Juego no iniciado" << std::endl;
    
    else game.Main();
        
}

GAME.H

#ifndef GAME_DEFINED
#define GAME_DEFINED
   
#define SCR_W 640
#define SCR_H 480
   
#include <SDL/SDL.h>
   
enum eDir{UP,DOWN,LEFT,RIGHT};
   
class CGame{
public:
    bool bInit();
    void Main();
private:
    SDL_Surface* screen;
    int cicle;
};

#endif

GAME.CPP

#include <iostream>
using namespace std;

#include <SDL/SDL.h>

#include "game.h"
#include "player.h"

bool CGame::bInit(){
	
    SDL_Init( SDL_INIT_VIDEO );
    screen = SDL_SetVideoMode(SCR_W,SCR_H,16,SDL_HWSURFACE);
    
    SDL_ShowCursor(SDL_DISABLE);
    
    cicle = 100;
    
    return true;
    
}
 
void CGame::Main(){
    CPlayer player;
    player.bInit();
    
    bool exit = false;
    while (not exit){
    
    unsigned int time = SDL_GetTicks();
    
    //INPUTS//
    SDL_Event event;
    while(SDL_PollEvent(&event)){
        switch(event.type){
            case SDL_QUIT:
                exit = true;
            break;
      			
            case SDL_KEYDOWN:
                switch(event.key.keysym.sym){
                    default:
                    break;
                }
            break;
        }
    }
 	
    unsigned char *keystate = SDL_GetKeyState(NULL);

    //LOGICS//
    if (keystate[SDLK_UP]) player.Move(UP);
    if (keystate[SDLK_DOWN]) player.Move(DOWN);
    if (keystate[SDLK_LEFT]) player.Move(LEFT);
    if (keystate[SDLK_RIGHT]) player.Move(RIGHT);
    
    //OUTPUTS//
    SDL_FillRect(screen,NULL,0);
    player.Draw(screen);
    SDL_Flip(screen);
    
    //SYNC//
    unsigned int time_now = SDL_GetTicks();
    if ((time_now - time) < cicle)
        SDL_Delay(cicle - (time_now - time));
        
    SDL_Quit();
}

PLAYER.H

#ifndef PLAYER_DEFINED
#define PLAYER_DEFINED
   
#include <iostream>
   
class CPlayer{
public:
    bool bInit();
    void Move(int dir);
    void Draw(SDL_Surface* screen);
private:
    int x,y;
    int step;
};

#endif

PLAYER.CPP

#include <iostream>
using namespace std;

#include <SDL/SDL.h>

#include "player.h"
#include "game.h"

bool CPlayer::bInit(){
    x = SCR_W/2;
    y = SCR_H/2;
    
    step = 8;
    
    return true;
}
   
void CPlayer::Move(int dir){
    switch(dir) {
        case UP:
            y -= step;
        break;
        	
        case DOWN:
            y += step;
        break;
   		
        case LEFT:
            x -= step;
        break;
        
        case RIGHT:
            x += step;
        break;
    }
}

void CPlayer::Draw(SDL_Surface* screen){
    SDL_Rect rect;
    rect.x = x;
    rect.y = y;
    rect.h = rect.w = 16;
    
    SDL_FillRect(screen, &rect, 0x0000FF);
}