Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "SDL/SDL.h"
- #include <string>
- #include <iostream>
- #include <stdio.h>
- const int SCH=640;
- const int SCW=480;
- const int BBP=32;
- const int FPS=30;
- const char* name = "TEHGAEM";
- // sprite height and width
- const int SPH=20;
- const int SPW=20;
- using namespace std;
- SDL_Event event;
- SDL_Surface *screen=NULL;
- SDL_Surface *sprite=NULL;
- SDL_Surface *temp = NULL;
- class Player
- {
- private:
- int x,xVel;
- int y,yVel;
- public:
- Player();
- void show();
- void move();
- void handle_input();
- };
- Player::Player()
- {
- x=0;
- y=0;
- xVel=0;
- yVel=0;
- }
- enum KEYS
- {
- UP = SDLK_UP, DOWN= SDLK_DOWN, LEFT = SDLK_LEFT, RIGHT = SDLK_RIGHT
- };
- void apply(int x, int y, SDL_Surface *dest, SDL_Surface *src, SDL_Rect *clip = NULL)
- {
- SDL_Rect offset;
- offset.x=x;
- offset.y=y;
- SDL_BlitSurface (src, clip, dest, &offset);
- }
- void Player::show()
- {
- apply(x,y, screen, sprite);
- }
- void Player::handle_input()
- {
- if (event.type ==SDL_KEYDOWN)
- {
- switch (event.key.keysym.sym)
- {
- case SDLK_UP: yVel -= SPH /2; break;
- case SDLK_DOWN: yVel += SPH /2; break;
- case SDLK_LEFT: xVel -=SPW /2; break;
- case SDLK_RIGHT: xVel +=SPW /2; break;
- }
- }
- else if (event.type == SDL_KEYUP)
- {
- switch(event.key.keysym.sym)
- {
- case SDLK_UP: yVel += SPH /2; break;
- case SDLK_DOWN: yVel -= SPH /2; break;
- case SDLK_LEFT: xVel +=SPW /2; break;
- case SDLK_RIGHT: xVel -=SPW /2; break;
- }
- }
- }
- void Player::move()
- {
- Uint32 start=SDL_GetTicks();
- x+=xVel;
- if ((x<0) || (x+SPW > SCW))
- {
- x-=1;
- }
- y+=yVel;
- if ((y<0) || (y+SPH > SCH))
- {
- y-=1;
- }
- }
- bool init()
- {
- if (SDL_Init(SDL_INIT_EVERYTHING)<0)
- {
- return false;
- }
- screen = SDL_SetVideoMode(SCH,SCW,BBP, SDL_SWSURFACE);
- if (screen == NULL)
- {
- return false;
- }
- SDL_WM_SetCaption(name, NULL);
- return true;
- }
- bool somethings()
- {
- temp = SDL_LoadBMP("sprite.bmp");
- if (temp == NULL)
- {
- return false;
- }
- sprite = SDL_DisplayFormat (temp);
- SDL_FreeSurface(temp);
- if (sprite ==NULL)
- {
- return false;
- }
- return true;
- }
- void clean()
- {
- SDL_FreeSurface(sprite);
- SDL_Quit();
- }
- int console_appinstruct()
- {
- printf("Here are the instructions:");
- std::cout << "Use the arrow keys to turn around" << endl;
- std::cout << "Remove this program by deleting SDL3.exe from task manager"<<endl;
- std::cout << "I'll add a quit button soon :)";
- }
- int main(int argc, char* args[])
- {
- console_appinstruct();
- Player P1;
- Uint32 go =SDL_GetTicks();
- bool quit=false;
- if (init() == false)
- {
- return 1;
- }
- if (somethings() ==false)
- {
- return 1;
- }
- while (quit ==false)
- {
- while (SDL_PollEvent(&event))
- {
- P1.handle_input();
- if (event.type == SDL_KEYDOWN)
- switch (event.key.keysym.sym)
- {
- case SDL_QUIT:
- case SDLK_ESCAPE:
- case SDLK_q:
- quit = true;
- clean();
- break;
- }
- }
- SDL_Delay((1000/FPS)-go);
- if (SDL_Flip(screen) ==-1)
- {
- return 1;
- }
- P1.move();
- P1.show();
- }
- clean();
- return 0;
- }
Add Comment
Please, Sign In to add comment