133 lines
3.9 KiB
C
133 lines
3.9 KiB
C
#include "ktkMap.h"
|
|
#include "ktk_parse.h"
|
|
#include "ktkProgram.h"
|
|
#include <SDL/SDL.h>
|
|
#include <time.h>
|
|
#include <math.h>
|
|
|
|
SDL_Surface *screen;
|
|
|
|
int drawMap(struct ktkMap *map) {
|
|
int x, y;
|
|
int scale_x = screen->w / (map->w);
|
|
int scale_y = screen->h / (map->h);
|
|
for (y = 0; y < screen->h; y++) {
|
|
int c_y = y / scale_y;
|
|
if (c_y >= map->h) continue;
|
|
for (x = 0; x < screen->w; x++) {
|
|
long color;
|
|
long red = 0, green = 0, blue = 0;
|
|
int c_x = x / scale_x;
|
|
if (c_x >= map->w) continue;
|
|
if (map->cell[c_x][c_y].flags & ktk_CELL_EMPTY) {
|
|
color = 128.0f;
|
|
} else {
|
|
color = lround(32.0f + map->cell[c_x][c_y].id_1 * 32.0f);
|
|
}
|
|
if (color < 0) color = 0;
|
|
if (color > 255) color = 255;
|
|
if ((color % 3) == 0) {
|
|
red = color;
|
|
blue = color/2;
|
|
green = color/4;
|
|
} else if ((color %3) == 1) {
|
|
green = color;
|
|
red = color/2;
|
|
blue = color/4;
|
|
} else {
|
|
blue = color;
|
|
green = color/2;
|
|
red = color/4;
|
|
}
|
|
|
|
SDL_Rect rect = {x, y, scale_x, scale_y};
|
|
SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, red, green, blue));
|
|
}
|
|
}
|
|
}
|
|
|
|
#if defined(__APPLE__) && defined(__MACH__)
|
|
int SDL_main(int argc, char ** argv) {
|
|
#else
|
|
int main(int argc, char **argv) {
|
|
#endif
|
|
ktk_randomizeSeed();
|
|
if (argc < 3) {
|
|
printf("usage: %s structure_file structure_name\n", argv[0]);
|
|
return 0;
|
|
}
|
|
|
|
struct ktkProgram my_program = ktk_PROGRAM_DEFAULT;
|
|
if (ktk_parseSFile(&my_program, argv[1]) != 0) {
|
|
printf("could not open %s for reading\n", argv[1]);
|
|
return 1;
|
|
}
|
|
|
|
SDL_Init(SDL_INIT_VIDEO);
|
|
if ((screen = SDL_SetVideoMode(800, 600, 32, SDL_HWSURFACE|SDL_DOUBLEBUF|SDL_RESIZABLE)) == NULL) {
|
|
printf("could not initialize SDL screen!\n");
|
|
return 1;
|
|
}
|
|
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
|
|
SDL_Flip(screen);
|
|
|
|
struct ktkLive my_live = ktk_LIVE_DEFAULT;
|
|
|
|
struct ktkMap my_map = ktk_MAP_DEFAULT;
|
|
my_map.flags |= ktk_MAP_RESIZE;
|
|
ktk_buildStructure(&my_program, &my_live, ktk_getStructure(&my_program, argv[2]), &my_map);
|
|
|
|
struct ktkLive new_live = ktk_LIVE_DEFAULT;
|
|
ktk_linkStructures(&my_program, &my_live, &new_live, &my_map);
|
|
|
|
drawMap(&my_map);
|
|
|
|
int is_running = 1;
|
|
while(is_running) {
|
|
SDL_Event event;
|
|
if (SDL_WaitEvent(&event)) {
|
|
switch(event.type) {
|
|
case SDL_QUIT:
|
|
is_running = 0;
|
|
break;
|
|
case SDL_KEYDOWN:
|
|
printf("%d\n", event.key.keysym.sym);
|
|
if (event.key.keysym.sym == 113) {
|
|
is_running = 0;
|
|
// space/enter = roll and link structure
|
|
} else if (event.key.keysym.sym == 32 || event.key.keysym.sym == 13) {
|
|
ktk_deleteMap(&my_map);
|
|
ktk_deleteLive(&my_live);
|
|
ktk_deleteLive(&new_live);
|
|
ktk_buildStructure(&my_program, &my_live, ktk_getStructure(&my_program, argv[2]), &my_map);
|
|
ktk_linkStructures(&my_program, &my_live, &new_live, &my_map);
|
|
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
|
|
drawMap(&my_map);
|
|
// z = roll structure
|
|
} else if (event.key.keysym.sym == 122) { // roll structure(s)
|
|
ktk_deleteMap(&my_map);
|
|
ktk_deleteLive(&my_live);
|
|
ktk_deleteLive(&new_live);
|
|
ktk_buildStructure(&my_program, &my_live, ktk_getStructure(&my_program, argv[2]), &my_map);
|
|
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
|
|
drawMap(&my_map);
|
|
// x = link live structures
|
|
} else if (event.key.keysym.sym == 120) { // link structure(s)
|
|
ktk_linkStructures(&my_program, &my_live, &new_live, &my_map);
|
|
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
|
|
drawMap(&my_map);
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
SDL_Flip(screen);
|
|
}
|
|
|
|
|
|
ktk_deleteMap(&my_map);
|
|
|
|
ktk_freeProgram(&my_program);
|
|
|
|
return 0;
|
|
}
|