-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Everything is working beautifully
- Loading branch information
Ben Green
committed
Jul 8, 2009
1 parent
5236644
commit 5518946
Showing
1 changed file
with
40 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,48 @@ | ||
#include <SDL/SDL.h> | ||
#include <queue> | ||
#include "character.h" | ||
|
||
class Character { | ||
protected: | ||
int x, y; | ||
int xVel, yVel; | ||
int speed; | ||
SDL_Surface *sprite; | ||
|
||
public: | ||
|
||
Character() { | ||
x = 0; | ||
y = 0; | ||
xVel = 0, yVel = 0; | ||
speed = 5; | ||
sprite = SDL_CreateRGBSurface(SDL_SWSURFACE, 32, 32, 16, 0,0,0,0); | ||
SDL_FillRect(sprite, NULL, 0x0F00); | ||
} | ||
|
||
virtual void handle_move() = 0; | ||
virtual void handle_show(SDL_Surface* screen) = 0; | ||
}; | ||
Character::Character() { | ||
x = 0; | ||
y = 0; | ||
xVel = 0, yVel = 0; | ||
speed = 5; | ||
sprite = SDL_CreateRGBSurface(SDL_SWSURFACE, 32, 32, 16, 0,0,0,0); | ||
SDL_FillRect(sprite, NULL, 0x0F00); | ||
} | ||
|
||
|
||
class Player: Character { | ||
|
||
public: | ||
|
||
std::queue<SDL_Event> input; | ||
|
||
void handle_input() { | ||
SDL_Event event; | ||
while(!input.empty()) { | ||
event = input.front(); | ||
if(event.type == SDL_KEYDOWN) { | ||
switch(event.key.keysym.sym) { | ||
case SDLK_UP: yVel -= 1; break; | ||
case SDLK_DOWN: yVel += 1; break; | ||
case SDLK_LEFT: xVel -= 1; break; | ||
case SDLK_RIGHT: xVel += 1; break; | ||
} | ||
} else if(event.type == SDL_KEYUP) { | ||
switch(event.key.keysym.sym) { | ||
case SDLK_UP: yVel += 1; break; | ||
case SDLK_DOWN: yVel -= 1; break; | ||
case SDLK_LEFT: xVel += 1; break; | ||
case SDLK_RIGHT: xVel -= 1; break; | ||
} | ||
void Player::handle_input() { | ||
SDL_Event event; | ||
while(!input.empty()) { | ||
event = input.front(); | ||
if(event.type == SDL_KEYDOWN) { | ||
switch(event.key.keysym.sym) { | ||
case SDLK_UP: yVel -= 1; break; | ||
case SDLK_DOWN: yVel += 1; break; | ||
case SDLK_LEFT: xVel -= 1; break; | ||
case SDLK_RIGHT: xVel += 1; break; | ||
} | ||
} else if(event.type == SDL_KEYUP) { | ||
switch(event.key.keysym.sym) { | ||
case SDLK_UP: yVel += 1; break; | ||
case SDLK_DOWN: yVel -= 1; break; | ||
case SDLK_LEFT: xVel += 1; break; | ||
case SDLK_RIGHT: xVel -= 1; break; | ||
} | ||
input.pop(); | ||
} | ||
input.pop(); | ||
} | ||
} | ||
|
||
void handle_move() { | ||
x += speed * xVel; | ||
y += speed * yVel; | ||
} | ||
void handle_show(SDL_Surface* screen) { | ||
SDL_Rect offset; | ||
offset.x = x; | ||
offset.y = y; | ||
SDL_BlitSurface(sprite, NULL, screen, &offset); | ||
} | ||
}; | ||
void Player::handle_move() { | ||
x += speed * xVel; | ||
y += speed * yVel; | ||
} | ||
|
||
void Player::handle_show(SDL_Surface* screen) { | ||
SDL_Rect offset; | ||
offset.x = x; | ||
offset.y = y; | ||
SDL_BlitSurface(sprite, NULL, screen, &offset); | ||
} | ||
|