Skip to content

Instantly share code, notes, and snippets.

@justinmeiners
Created June 16, 2019 05:32
Show Gist options
  • Save justinmeiners/d768ad294b79f9d7041e19a94a69205c to your computer and use it in GitHub Desktop.
Save justinmeiners/d768ad294b79f9d7041e19a94a69205c to your computer and use it in GitHub Desktop.
/*
After making my ASM rogue, I thought it might be fun to make a C one.
Termbox looks awesome.
gcc rogue.c -I /usr/local/include -L /usr/local/lib -l termbox -o rogue
*/
#include <stdio.h>
#include <stdlib.h>
#include <termbox.h>
#define MAX(a,b) ((a) > (b) ? a : b)
#define MIN(a,b) ((a) < (b) ? a : b)
#define ROGUE_W_MAX 32
#define ROGUE_H_MAX 32
#define ROGUE_ACTOR_MAX 128
static const char actor_symbols[] = "@";
static const char map_symbols[] = ".#";
enum
{
ACTOR_PLAYER = 0,
};
typedef struct
{
int type;
short x;
short y;
} actor;
typedef struct
{
char* map;
char* actor_map;
actor* actors;
} rogue;
void rogue_init(rogue* rogue)
{
rogue->map = malloc(ROGUE_W_MAX * ROGUE_H_MAX * sizeof(char));
rogue->actor_map = malloc(ROGUE_W_MAX * ROGUE_H_MAX * sizeof(char));
rogue->actors = malloc(ROGUE_ACTOR_MAX * sizeof(actor));
}
void rogue_map_gen(rogue* rogue)
{
for (int y = 0; y < ROGUE_H_MAX; ++y)
{
for (int x = 0; x < ROGUE_W_MAX; ++x)
{
rogue->actor_map[x + y * ROGUE_W_MAX] = -1;
rogue->map[x + y * ROGUE_W_MAX] = 0;
}
}
rogue->actors[0].type = ACTOR_PLAYER;
rogue->actors[0].x = 0;
rogue->actors[0].y = 0;
rogue->actor_map[0] = 0;
}
void print_rogue(rogue* rogue)
{
tb_clear();
int w = MIN(ROGUE_W_MAX, tb_width());
int h = MIN(ROGUE_H_MAX, tb_height());
for (int y = 0; y < h; ++y)
{
for (int x = 0; x < w; ++x)
{
char tile = rogue->map[x + y * ROGUE_W_MAX];
int actorIndex = rogue->actor_map[x + y * ROGUE_W_MAX];
uint32_t unicode_symbol;
uint16_t bg, fg;
if (actorIndex == -1)
{
tb_utf8_char_to_unicode(&unicode_symbol, map_symbols + tile);
bg = TB_DEFAULT;
fg = TB_WHITE;
}
else
{
const actor* act = rogue->actors + actorIndex;
tb_utf8_char_to_unicode(&unicode_symbol, actor_symbols + act->type);
fg = TB_YELLOW | TB_BOLD;
bg = TB_DEFAULT;
}
tb_change_cell(x, y, unicode_symbol, fg, bg);
}
}
tb_present();
}
void update_rogue(rogue* rogue, int key)
{
actor* player = rogue->actors;
int x = player->x;
int y = player->y;
switch (key)
{
case TB_KEY_ARROW_UP:
y -= 1;
break;
case TB_KEY_ARROW_DOWN:
y += 1;
break;
case TB_KEY_ARROW_LEFT:
x -= 1;
break;
case TB_KEY_ARROW_RIGHT:
x += 1;
break;
}
rogue->actor_map[player->x + player->y * ROGUE_W_MAX] = -1;
rogue->actor_map[x + y * ROGUE_W_MAX] = 0;
player->x = x;
player->y = y;
}
int main(int argc, const char * argv[])
{
tb_init();
rogue r;
rogue_init(&r);
rogue_map_gen(&r);
int quit = 0;
while (quit != 1)
{
struct tb_event event;
tb_poll_event(&event);
if (event.type == TB_EVENT_KEY && event.ch == 'q')
{
quit = 1;
}
if (event.type == TB_EVENT_KEY)
{
update_rogue(&r, event.key);
}
print_rogue(&r);
}
tb_shutdown();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment