timesynk/interface/sdl.c

62 lines
1.6 KiB
C

//#if !defined (__APPLE__)
#include <SDL/SDL.h>
//#endif
#include "sdl.h"
#include "../main.h"
#include "../common.h"
#include "../player.h"
#include "../game.h"
int interfaceInit() {
// Load it up!
SDL_Init(SDL_INIT_EVERYTHING);
// Set up our SDL Window
if ((screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE)) == NULL) {
return ERROR;
}
SDL_WM_SetCaption(NAME, NULL);
// Fill our screen w/ black
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
// Update!
SDL_Flip(screen);
return SUCCESS;
}
void interfaceLoop() {
while (SDL_PollEvent(&event)) {
switch(event.type) {
case SDL_QUIT:
is_running = 0;
break;
case SDL_KEYDOWN:
if (event.key.keysym.sym == SDLK_q) {
is_running = 0;
} else if (event.key.keysym.sym == SDLK_UP) {
(*player_commands[PLAYER_MOVE])(NORTH, 1);
} else if (event.key.keysym.sym == SDLK_DOWN) {
(*player_commands[PLAYER_MOVE])(SOUTH, 1);
} else if (event.key.keysym.sym == SDLK_RIGHT) {
(*player_commands[PLAYER_MOVE])(EAST, 1);
} else if (event.key.keysym.sym == SDLK_LEFT) {
(*player_commands[PLAYER_MOVE])(WEST, 1);
}
break;
}
}
}
void interfaceDraw() {
// TODO: instead of redrawing whole screen, redraw last positions of tiles
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
SDL_Rect player_rect = {player.x*16, player.y*16, 16, 16};
SDL_FillRect(screen, &player_rect, SDL_MapRGB(screen->format, 255, 255, 255));
SDL_Flip(screen); // redraw!
}
void interfaceClose() {
SDL_Quit();
}