Last active
August 29, 2015 14:20
-
-
Save a3f/c677bd9d7de1d1dc29e2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// engine.h | |
typedef struct{ | |
unsigned height; | |
unsigned width; | |
char *buffer; | |
}screen_t[1]; | |
screen_t screen_new(unsigned x, unsigned y); | |
void screen_delete(screen_t screen); | |
void screen_fill(screen_t screen, char ch); | |
void screen_print(screen_t screen) | |
// engine.cc | |
#include "engine.h" | |
screen_t screen_new(unsigned x, unsigned y) | |
{ | |
screen_t screen; | |
screen->height = x | |
screen->width = y | |
screen->buffer = new char[x*(y+1)]; | |
for (int i = 0; i < x-1; ++i) | |
screen->buffer[i*y+y] = '\n'; | |
screen->buffer[x*(y+1) - 1] = '\0'; | |
return screen; | |
} | |
void screen_delete(screen_t screen) | |
{ | |
screen->x = 0; | |
screen->y = 0; | |
delete[] screen->buffer; | |
} | |
void screen_print(screen_t screen) | |
{ | |
fputs(screen->buffer, stdout); | |
} | |
void screen_fill(screen_t screen, char ch) | |
{ | |
// tb2a tst5dm memset! | |
unsigned i_max = screen->height, | |
j_max = screen->width; | |
for (int i = 0; i < i_max; ++i) | |
for (int j = 0; j < j_max; ++j) | |
screen->buffer[i*i_max + j] = ch; | |
// game.c | |
#include "engine.h" | |
int main() | |
{ | |
screen_t screen = screen_new(80, 25); | |
screen_fill(screen, ' '); | |
screen_print(screen); | |
screen_delete(screen); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment