Last active
December 11, 2015 11:39
-
-
Save patricklucas/4595296 to your computer and use it in GitHub Desktop.
Basic SDL thingy
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
#include "SDL/SDL.h" | |
#include "SDL/SDL_gfxPrimitives.h" | |
#include "SDL/SDL_image.h" | |
#include <string> | |
#define MIN(a, b) (((a) < (b)) ? (a) : (b)) | |
#define MAX(a, b) (((a) > (b)) ? (a) : (b)) | |
const int SCREEN_WIDTH = 640; | |
const int SCREEN_HEIGHT = 480; | |
const int SCREEN_BPP = 32; | |
const int FRAMES_PER_SECOND = 60; | |
const int DOT_WIDTH = 20; | |
const int DOT_HEIGHT = 20; | |
const float DOT_ACC_RATE = 0.25f; | |
SDL_Surface *dot = NULL; | |
SDL_Surface *arrow = NULL; | |
SDL_Surface *screen = NULL; | |
SDL_Event event; | |
class Dot { | |
private: | |
float x, y; | |
float xVel, yVel; | |
float xAcc, yAcc; | |
public: | |
Dot(); | |
void handle_input(); | |
void move(); | |
void show(); | |
}; | |
class Timer { | |
private: | |
int startTicks; | |
int pausedTicks; | |
bool paused; | |
bool started; | |
public: | |
Timer(); | |
void start(); | |
void stop(); | |
void pause(); | |
void unpause(); | |
int get_ticks(); | |
bool is_started(); | |
bool is_paused(); | |
}; | |
SDL_Surface *load_image(std::string filename) { | |
SDL_Surface* loadedImage = NULL; | |
SDL_Surface* optimizedImage = NULL; | |
loadedImage = IMG_Load(filename.c_str()); | |
if (loadedImage != NULL) { | |
optimizedImage = SDL_DisplayFormat(loadedImage); | |
SDL_FreeSurface(loadedImage); | |
if (optimizedImage != NULL) { | |
SDL_SetColorKey(optimizedImage, SDL_SRCCOLORKEY, SDL_MapRGB( | |
optimizedImage->format, 0, 0xFF, 0xFF)); | |
} | |
} | |
return optimizedImage; | |
} | |
void apply_surface(int x, int y, SDL_Surface* source, SDL_Surface* destination, | |
SDL_Rect* clip = NULL) { | |
SDL_Rect offset; | |
offset.x = x; | |
offset.y = y; | |
SDL_BlitSurface(source, clip, destination, &offset); | |
} | |
bool init() { | |
if (SDL_Init(SDL_INIT_EVERYTHING) == -1) { | |
return false; | |
} | |
screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE); | |
if (screen == NULL) { | |
return false; | |
} | |
SDL_WM_SetCaption("Move the Dot", NULL); | |
return true; | |
} | |
bool load_files() { | |
dot = load_image("dot.bmp"); | |
if (dot == NULL) { | |
return false; | |
} | |
return true; | |
} | |
void clean_up() { | |
SDL_FreeSurface(dot); | |
SDL_Quit(); | |
} | |
Dot::Dot() { | |
x = y = xVel = yVel = xAcc = yAcc = 0.0f; | |
} | |
void Dot::handle_input() { | |
if (event.type == SDL_KEYDOWN) { | |
switch (event.key.keysym.sym) { | |
case SDLK_UP: yAcc -= DOT_ACC_RATE; break; | |
case SDLK_DOWN: yAcc += DOT_ACC_RATE; break; | |
case SDLK_LEFT: xAcc -= DOT_ACC_RATE; break; | |
case SDLK_RIGHT: xAcc += DOT_ACC_RATE; break; | |
} | |
} | |
else if (event.type == SDL_KEYUP) { | |
switch (event.key.keysym.sym) { | |
case SDLK_UP: yAcc += DOT_ACC_RATE; break; | |
case SDLK_DOWN: yAcc -= DOT_ACC_RATE; break; | |
case SDLK_LEFT: xAcc += DOT_ACC_RATE; break; | |
case SDLK_RIGHT: xAcc -= DOT_ACC_RATE; break; | |
} | |
} | |
} | |
void Dot::move() { | |
xVel += xAcc; | |
yVel += yAcc; | |
x += xVel; | |
y += yVel; | |
} | |
void Dot::show() { | |
if (x < 0 || x > SCREEN_WIDTH || y < 0 || y > SCREEN_HEIGHT) { | |
int px = MAX(MIN(x, SCREEN_WIDTH - 20), 10); | |
int py = MAX(MIN(y, SCREEN_HEIGHT - 20), 10); | |
rectangleRGBA(screen, px, py, px + 10, py + 10, 0, 0, 0, 255); | |
} | |
apply_surface(x, y, dot, screen); | |
} | |
Timer::Timer() { | |
startTicks = 0; | |
pausedTicks = 0; | |
paused = false; | |
started = false; | |
} | |
void Timer::start() { | |
started = true; | |
paused = false; | |
startTicks = SDL_GetTicks(); | |
} | |
void Timer::stop() { | |
started = false; | |
paused = false; | |
} | |
void Timer::pause() { | |
if (started && !paused) { | |
paused = true; | |
pausedTicks = SDL_GetTicks() - startTicks; | |
} | |
} | |
void Timer::unpause() { | |
if (paused) { | |
paused = false; | |
startTicks = SDL_GetTicks() - pausedTicks; | |
pausedTicks = 0; | |
} | |
} | |
int Timer::get_ticks() { | |
if (started) { | |
if (paused) { | |
return pausedTicks; | |
} else { | |
return SDL_GetTicks() - startTicks; | |
} | |
} | |
return 0; | |
} | |
bool Timer::is_started() { | |
return started; | |
} | |
bool Timer::is_paused() { | |
return paused; | |
} | |
int main(int argc, char* args[]) { | |
bool quit = false; | |
Dot myDot; | |
Timer fps; | |
if (!init()) { | |
return 1; | |
} | |
if (!load_files()) { | |
return 1; | |
} | |
while (!quit) { | |
fps.start(); | |
while (SDL_PollEvent(&event)) { | |
myDot.handle_input(); | |
if (event.type == SDL_QUIT) { | |
quit = true; | |
} | |
} | |
myDot.move(); | |
SDL_FillRect(screen, &screen->clip_rect, SDL_MapRGB(screen->format, 0xFF, 0xFF, 0xFF)); | |
myDot.show(); | |
if (SDL_Flip(screen) == -1) { | |
return 1; | |
} | |
if (fps.get_ticks() < 1000 / FRAMES_PER_SECOND) { | |
SDL_Delay((1000 / FRAMES_PER_SECOND) - fps.get_ticks()); | |
} | |
} | |
clean_up(); | |
return 0; | |
} |
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
CFLAGS=`sdl-config --cflags` | |
LIBS=`sdl-config --libs` -lSDL_image -lSDL_gfx | |
.PHONY: all | |
all: fun | |
fun: main.cpp | |
g++ -o $@ $(CFLAGS) $(LIBS) $^ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment