timesynk/interface/sdl.c

77 lines
2.1 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, 384, 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));
int step_x = player.x - 12;
int step_y = player.y - 6;
int end_x = player.x + 12;
int end_y = player.y + 6;
while (step_x < end_x) {
step_y = player.y - 6;
while (step_y < end_y) {
if (visible_matrix[step_x][step_y] & TILE_VISIBLE) {
SDL_Rect wall_rect = {step_x*8, step_y*16, 8, 16};
SDL_FillRect(screen, &wall_rect, SDL_MapRGB(screen->format, 128, 128, 128));
}
step_y++; // move down
}
step_x++; // move right
}
SDL_Rect player_rect = {player.x*8, player.y*16, 8, 16};
SDL_FillRect(screen, &player_rect, SDL_MapRGB(screen->format, 255, 255, 255));
SDL_Flip(screen); // redraw!
}
void interfaceClose() {
SDL_Quit();
}