130 lines
4.7 KiB
C
130 lines
4.7 KiB
C
//#if !defined (__APPLE__)
|
|
#include <SDL/SDL.h>
|
|
#include <SDL/SDL_image.h>
|
|
//#endif
|
|
|
|
#include "sdl.h"
|
|
#include "../main.h"
|
|
#include "../common.h"
|
|
#include "../player.h"
|
|
#include "../game.h"
|
|
#include "../context.h"
|
|
#include "../tiles/tiles.h"
|
|
#include "../tile.h"
|
|
|
|
int interfaceInit() {
|
|
// Load it up!
|
|
SDL_Init(SDL_INIT_VIDEO);
|
|
// Set up our SDL Window
|
|
if ((screen = SDL_SetVideoMode(800, 600, 32, SDL_SWSURFACE)) == NULL) {
|
|
return ERROR;
|
|
}
|
|
SDL_WM_SetCaption(NAME, NULL);
|
|
|
|
camera_surface = SDL_CreateRGBSurface(screen->flags, 512, 512, screen->format->BitsPerPixel, screen->format->Rmask, screen->format->Gmask, screen->format->Bmask, screen->format->Amask);
|
|
player_spritesheet = IMG_Load_RW(SDL_RWFromMem(&player_images, player_images_length), 1);
|
|
wall_spritesheet = IMG_Load_RW(SDL_RWFromMem(&wall_images, wall_images_length), 1);
|
|
floor_spritesheet = IMG_Load_RW(SDL_RWFromMem(&floor_images, floor_images_length), 1);
|
|
shadow_spritesheet = IMG_Load_RW(SDL_RWFromMem(&shadow_images, shadow_images_length), 1);
|
|
int race, class;
|
|
SDL_Rect sprite_offset = {0, 0, TILE_WIDTH, TILE_HEIGHT};
|
|
for (race = 0; race < TOTAL_RACES;race++) {
|
|
for(class = 0; class < TOTAL_CLASSES;class++) {
|
|
player_sprites[race][class] = SDL_CreateRGBSurface(screen->flags, 16, 32, screen->format->BitsPerPixel, screen->format->Rmask, screen->format->Gmask, screen->format->Bmask, screen->format->Amask);
|
|
SDL_BlitSurface(player_spritesheet, &sprite_offset, player_sprites[race][class], NULL);
|
|
}
|
|
}
|
|
// Fill our screen w/ black
|
|
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 32, 128, 64));
|
|
// Update!
|
|
SDL_Flip(screen);
|
|
|
|
return SUCCESS;
|
|
}
|
|
|
|
/* draw the player's current view */
|
|
void cameraDraw() {
|
|
SDL_FillRect(camera_surface, NULL, SDL_MapRGB(camera_surface->format, 0, 0, 0));
|
|
SDL_Rect camera_rect = {0, 0, 512, 512};
|
|
int step_x = player.x - 12;
|
|
int step_y = player.y - 12;
|
|
int end_x = player.x + 12;
|
|
int end_y = player.y + 12;
|
|
while (step_x < end_x) {
|
|
step_y = player.y - 6;
|
|
while (step_y < end_y) {
|
|
// TODO: draw the layer immediately in front of the player at partial translucency
|
|
if (step_x >= 0 && step_y >= 0 && step_x < current_map->width && step_y < current_map->height) {
|
|
struct Tile *current_tile;
|
|
current_tile = &(current_map)->matrix[step_x][step_y];
|
|
while(current_tile) {
|
|
int x_offset = current_tile->id / 16; // 16 tiles across in spritesheet
|
|
int y_offset = current_tile->id - (x_offset*16);
|
|
SDL_Rect sprite_offset = { x_offset*TILE_WIDTH, y_offset*TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT};
|
|
SDL_Rect tile_rect = {step_x*TILE_WIDTH, step_y*TILE_WIDTH, TILE_WIDTH, TILE_HEIGHT};
|
|
switch (current_tile->tid) {
|
|
case WALL:
|
|
SDL_BlitSurface(wall_spritesheet, &sprite_offset, camera_surface, &tile_rect);
|
|
break;
|
|
case FLOOR:
|
|
SDL_BlitSurface(floor_spritesheet, &sprite_offset, camera_surface, &tile_rect);
|
|
break;
|
|
case PLAYER:
|
|
y_offset = current_tile->id / TOTAL_CLASSES;
|
|
x_offset = current_tile->id - (y_offset*TOTAL_CLASSES);
|
|
sprite_offset.x = x_offset*TILE_WIDTH;
|
|
sprite_offset.y = y_offset*TILE_HEIGHT;
|
|
SDL_BlitSurface(shadow_spritesheet, NULL, camera_surface, &tile_rect);
|
|
SDL_BlitSurface(player_spritesheet, &sprite_offset, camera_surface, &tile_rect);
|
|
break;
|
|
}
|
|
current_tile = current_tile->next;
|
|
}
|
|
}
|
|
step_y++; // move down
|
|
}
|
|
step_x++; // move right
|
|
}
|
|
SDL_BlitSurface(camera_surface, NULL, screen, &camera_rect);
|
|
}
|
|
|
|
void interfaceLoop() {
|
|
while (SDL_PollEvent(&event)) {
|
|
switch(event.type) {
|
|
case SDL_QUIT:
|
|
is_running = 0;
|
|
break;
|
|
case SDL_KEYDOWN:
|
|
(*current_context)(event.key.keysym.sym);
|
|
/*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);
|
|
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));
|
|
cameraDraw(); // draw our current view
|
|
SDL_Flip(screen); // redraw!
|
|
}
|
|
|
|
void interfacePrint(const char *string) {
|
|
|
|
}
|
|
|
|
void interfaceClose() {
|
|
SDL_Quit();
|
|
}
|