First commit, old repository

This commit is contained in:
j4nk 2023-01-06 19:12:24 -05:00
commit 43da2edb0f
16 changed files with 3799 additions and 0 deletions

6
.gitignore vendored Normal file
View File

@ -0,0 +1,6 @@
#*
*~
main
*.o
vgcore*
*.core

10
Makefile Normal file
View File

@ -0,0 +1,10 @@
CC=cc
CLIB=-L/usr/local/lib -llua5.3 -lncurses -ltermbox
INC=-I/usr/local/include
BIN=main
DEBUG=-g
all:
$(CC) $(INC) $(CLIB) main.c -o $(BIN) $(DEBUG)
test:
$(CC) $(INC) $(CLIB) -D_BSD_SOURCE test.c -o test $(DEBUG)

1
ball.txt Normal file
View File

@ -0,0 +1 @@
O

4
card.txt Normal file
View File

@ -0,0 +1,4 @@
|--|
| |
| |
|--|

6
card_deck_demo.lua Normal file
View File

@ -0,0 +1,6 @@
require("oct_utils")
function oct_init()
local sprite0 = load_termbox_sprite("card.txt");
oct_set_termbox_sprite(0, 10, 10, 1, 8, sprite0);
end

27
demo2.lua Normal file
View File

@ -0,0 +1,27 @@
require("oct_utils")
require("termbox_defs")
card1 = oct_tb_sprite_new();
card2 = oct_tb_sprite_new();
dx = 1;
function oct_init()
card1["shape"] = load_termbox_sprite("card.txt");
card2["shape"] = load_termbox_sprite("card.txt");
card1["x"] = 50;
card1["y"] = 0;
card2["x"] = 50;
card2["y"] = 50;
end
function oct_loop(key)
if (key == TB_KEY_ARROW_RIGHT)
then
card1["x"] = card1["x"] + 1;
end
if (key == TB_KEY_ARROW_LEFT)
then
card1["x"] = card1["x"] - 1;
end
end

62
main.c Normal file
View File

@ -0,0 +1,62 @@
#include <stdio.h>
#include <lua-5.3/lualib.h>
#include <lua-5.3/lauxlib.h>
#include "termbox_render.h"
#include "oct_termbox_sprite.h"
#define TB_IMPL
lua_State *L;
int initialize_everything();
int deinitialize_everything();
int main(int argc, char* argv[]) {
if (!initialize_everything()) {
fprintf(stderr, "Couldn't initialize Open Card Table");
}
struct tb_event ev;
int finish = 0;
while (!finish) {
tb_clear();
tb_peek_event(&ev, 10);
if (ev.key == TB_KEY_ESC) {
finish = 1;
}
lua_getglobal(L, "oct_loop");
lua_pushinteger(L, ev.key);
lua_pushinteger(L, ev.ch);
lua_call(L, 2, 0);
for (uint32_t i=0; i < oct_tb_sprite_list.new_index; i++) {
oct_render_termbox_sprite(oct_tb_sprite_list.sprite_list[i]);
}
tb_present();
}
deinitialize_everything();
return EXIT_SUCCESS;
}
int initialize_everything() {
if (!oct_tb_sprite_list_initialize()) {
return 0;
}
L = luaL_newstate();
if (L == NULL) {
fprintf(stderr, "Can't initialize Lua\n");
return 0;
}
luaL_openlibs(L);
oct_tb_initialize_lua(L);
luaL_dofile(L, "pong.lua");
lua_getglobal(L, "oct_init");
lua_call(L, 0, 0);
tb_init();
return 1;
}
int deinitialize_everything() {
tb_shutdown();
lua_close(L);
oct_tb_sprite_list_deinitialize();
return 1;
}

159
oct_termbox_sprite.h Normal file
View File

@ -0,0 +1,159 @@
#ifndef OCT_TERMBOX_SPRITE_H
#define OCT_TERMBOX_SPRITE_H
#include <string.h>
#include <stdint.h>
#include <stdio.h>
#include <lua-5.3/lualib.h>
#include <lua-5.3/lauxlib.h>
#include "termbox.h"
#define OCT_MAX_SPRITE_SIZE 101
#define OCT_INITIAL_NUM_SPRITES 100
#define OCT_SPRITE_LIST_REALLOC_LENGTH 100
#define OCT_SPRITE_MEMBER_NAME_MAXLENGTH 10
#define OCT_SPRITE_PRINT_BUFFER_SIZE 1024
#define OCT_OUT_OF_SCREEN 1000
struct oct_tb_sprite {
int x;
int y;
uintattr_t fg;
uintattr_t bg;
char shape[OCT_MAX_SPRITE_SIZE];
};
struct {
// Array of pointers to oct_tb_sprites
struct oct_tb_sprite** sprite_list;
uint32_t new_index;
uint32_t size;
uint8_t initialized;
} oct_tb_sprite_list;
int oct_tb_sprite_list_initialize() {
oct_tb_sprite_list.sprite_list = calloc(OCT_INITIAL_NUM_SPRITES, sizeof(struct oct_tb_sprite*));
if (!oct_tb_sprite_list.sprite_list) {
perror("Could not allocate memory for sprite list");
return 0;
}
oct_tb_sprite_list.new_index = 0;
oct_tb_sprite_list.size = OCT_INITIAL_NUM_SPRITES;
oct_tb_sprite_list.initialized = 1;
return 1;
}
int oct_tb_sprite_list_deinitialize() {
free(oct_tb_sprite_list.sprite_list);
oct_tb_sprite_list.sprite_list = NULL;
oct_tb_sprite_list.new_index = 0;
oct_tb_sprite_list.size = 0;
oct_tb_sprite_list.initialized = 0;
return 1;
}
// No functionality to remove sprites (just draw offscreen)
int oct_tb_sprite__index(lua_State *L) {
char index[OCT_SPRITE_MEMBER_NAME_MAXLENGTH];
struct oct_tb_sprite* self = (struct oct_tb_sprite*)lua_touserdata(L, -2);
strncpy(index, luaL_checkstring(L, -1), OCT_SPRITE_MEMBER_NAME_MAXLENGTH);
if (!strncmp(index, "x", OCT_SPRITE_MEMBER_NAME_MAXLENGTH)) {
lua_pushinteger(L, self->x);
}
else if (!strncmp(index, "y", OCT_SPRITE_MEMBER_NAME_MAXLENGTH)) {
lua_pushinteger(L, self->y);
}
else if (!strncmp(index, "fg", OCT_SPRITE_MEMBER_NAME_MAXLENGTH)) {
lua_pushinteger(L, self->fg);
}
else if (!strncmp(index, "bg", OCT_SPRITE_MEMBER_NAME_MAXLENGTH)) {
lua_pushinteger(L, self->bg);
}
else if (!strncmp(index, "shape", OCT_SPRITE_MEMBER_NAME_MAXLENGTH)) {
lua_pushstring(L, self->shape);
}
else {
lua_pushnil(L);
}
return 1;
}
int oct_tb_sprite__newindex(lua_State *L) {
char index[OCT_SPRITE_MEMBER_NAME_MAXLENGTH];
struct oct_tb_sprite* self = (struct oct_tb_sprite*)lua_touserdata(L, -3);
strncpy(index, luaL_checkstring(L, -2), OCT_SPRITE_MEMBER_NAME_MAXLENGTH);
if (!strncmp(index, "x", OCT_SPRITE_MEMBER_NAME_MAXLENGTH)) {
self->x = luaL_checkinteger(L, -1);
}
else if (!strncmp(index, "y", OCT_SPRITE_MEMBER_NAME_MAXLENGTH)) {
self->y = luaL_checkinteger(L, -1);
}
else if (!strncmp(index, "fg", OCT_SPRITE_MEMBER_NAME_MAXLENGTH)) {
self->fg = (uintattr_t)luaL_checkinteger(L, -1);
}
else if (!strncmp(index, "bg", OCT_SPRITE_MEMBER_NAME_MAXLENGTH)) {
self->bg = (uintattr_t)luaL_checkinteger(L, -1);
}
else if (!strncmp(index, "shape", OCT_SPRITE_MEMBER_NAME_MAXLENGTH)) {
strncpy(self->shape, luaL_checkstring(L, -1), OCT_MAX_SPRITE_SIZE);
}
return 1;
}
int oct_tb_sprite__tostring(lua_State *L) {
struct oct_tb_sprite* self = (struct oct_tb_sprite*)lua_touserdata(L, -1);
char buffer[OCT_SPRITE_PRINT_BUFFER_SIZE];
snprintf(buffer,
OCT_SPRITE_PRINT_BUFFER_SIZE,
"{\n"
"\tx: %d\n"
"\ty: %d\n"
"\tfg: %d\n"
"\tbg: %d\n"
"\tshape: %s\n"
"}",
self->x, self->y, self->bg, self->fg, self->shape);
lua_pushstring(L, buffer);
return 1;
}
struct luaL_Reg oct_tb_sprite_metamethods[] =
{
{"__index", oct_tb_sprite__index},
{"__newindex", oct_tb_sprite__newindex},
{"__tostring", oct_tb_sprite__tostring},
{NULL, NULL}
};
int oct_tb_sprite_new(lua_State *L) {
if (oct_tb_sprite_list.size == oct_tb_sprite_list.new_index) {
if (!reallocarray(oct_tb_sprite_list.sprite_list,
sizeof(struct oct_tb_sprite*),
oct_tb_sprite_list.size+OCT_SPRITE_LIST_REALLOC_LENGTH)) {
perror("Could not reallocate memory for sprite list");
}
}
struct oct_tb_sprite* new_sprite = (struct oct_tb_sprite*)lua_newuserdata(L, sizeof(struct oct_tb_sprite));
new_sprite->x = OCT_OUT_OF_SCREEN;
new_sprite->y = OCT_OUT_OF_SCREEN;
new_sprite->fg = 0;
new_sprite->bg = 1;
strncpy(new_sprite->shape, "", OCT_MAX_SPRITE_SIZE);
oct_tb_sprite_list.sprite_list[oct_tb_sprite_list.new_index] = new_sprite;
lua_pushstring(L, "oct_tb_sprite_metatable");
lua_gettable(L, LUA_REGISTRYINDEX);
lua_setmetatable(L, -2);
oct_tb_sprite_list.new_index++;
return 1;
}
void oct_tb_initialize_lua(lua_State *L) {
lua_pushcfunction(L, oct_tb_sprite_new);
lua_setglobal(L, "oct_tb_sprite_new");
if (luaL_newmetatable(L, "oct_tb_sprite_metatable")) {
luaL_setfuncs(L, oct_tb_sprite_metamethods, 0);
}
}
#endif

8
oct_utils.lua Normal file
View File

@ -0,0 +1,8 @@
-- Load a sprite stored in a .txt file
function load_termbox_sprite(file_name)
file = io.open(file_name, "r");
io.input(file);
local contents = io.read("*a");
io.close(file);
return contents;
end

5
paddle.txt Normal file
View File

@ -0,0 +1,5 @@
||
||
||
||
||

151
pong.lua Normal file
View File

@ -0,0 +1,151 @@
require("oct_utils")
require("termbox_defs")
math.randomseed(os.time())
width = 74;
height = 25;
paddle_left = oct_tb_sprite_new();
paddle_right = oct_tb_sprite_new();
ball = oct_tb_sprite_new();
top_border = oct_tb_sprite_new();
bottom_border = oct_tb_sprite_new();
left_border = oct_tb_sprite_new();
right_border = oct_tb_sprite_new();
score_p1 = oct_tb_sprite_new();
score_p2 = oct_tb_sprite_new();
init_text = oct_tb_sprite_new();
border_topbottom = "";
dx = 0;
dy = 0;
delay = 10;
counter = 0;
for i=1,width,1
do
border_topbottom = border_topbottom .. "-"
end
border_leftright = "";
for i=1,height-1,1
do
border_leftright = border_leftright .. "|\n";
end
-- Get rid of the last \n
border_leftright = border_leftright:sub(1,-2);
pause_game = 1;
function pause()
pause_game = 1;
init_text["x"] = math.floor(width/2) - math.floor(string.len(init_text["shape"])/2);
init_text["y"] = math.floor(height/2)+2;
ball["x"] = math.floor(width/2);
ball["y"] = math.floor(height/2);
end
function unpause()
pause_game = 0; -- unpause game
init_text["x"] = 1000; -- draw offscreen
init_text["y"] = 1000; -- draw offscreen
end
function oct_init()
paddle_left["shape"] = load_termbox_sprite("paddle.txt");
paddle_right["shape"] = load_termbox_sprite("paddle.txt");
ball["shape"] = load_termbox_sprite("ball.txt");
paddle_left["x"] = 1;
paddle_left["y"] = math.floor(height/2)-2;
paddle_left["fg"] = TB_RED;
paddle_right["x"] = width-3;
paddle_right["y"] = math.floor(height/2)-2;
paddle_right["fg"] = TB_GREEN;
top_border["shape"] = border_topbottom;
bottom_border["shape"] = border_topbottom;
top_border["x"] = 0;
bottom_border["x"] = 0;
top_border["y"] = 0;
bottom_border["y"] = height;
left_border["shape"] = border_leftright;
left_border["x"] = 0;
left_border["y"] = 1;
right_border["shape"] = border_leftright;
right_border["x"] = width-1;
right_border["y"] = 1;
score_p1["x"] = 0;
score_p2["x"] = 0;
score_p1["y"] = height+1;
score_p2["y"] = height+2;
score_p1["shape"] = "Player 1: 0";
score_p2["shape"] = "Player 2: 0";
init_text["shape"] = "Press SPACE to begin";
pause();
end
function oct_loop(key, ch)
if (pause_game == 0)
then
-- handle keys
if (key == TB_KEY_ARROW_UP and paddle_right["y"] >= 2)
then
paddle_right["y"] = paddle_right["y"] - 1;
end
if (key == TB_KEY_ARROW_DOWN and paddle_right["y"] <= height-5-1)
then
paddle_right["y"] = paddle_right["y"] + 1;
end
if (ch == string.byte("w") and paddle_left["y"] >= 2)
then
paddle_left["y"] = paddle_left["y"] - 1;
end
if (ch == string.byte("s") and paddle_left["y"] <= height-5-1)
then
paddle_left["y"] = paddle_left["y"] + 1;
end
counter = counter + 1;
if (counter == delay)
then
counter = 0;
-- update ball
ball["x"] = ball["x"] + dx;
ball["y"] = ball["y"] + dy;
if (ball["y"] <= 1 or ball["y"] >= height-1)
then
dy = -dy;
end
if (ball["x"] <= 3)
then
if (ball["y"] >= paddle_left["y"] and ball["y"] <= paddle_left["y"] + 5)
then
dx = -dx;
else
pause();
end
end
if (ball["x"] >= width-4)
then
if (ball["y"] >= paddle_right["y"] and ball["y"] <= paddle_right["y"] + 5)
then
dx = -dx;
else
pause();
end
end
end
else
if (ch == string.byte(" "))
then
unpause();
-- ensure the ball is always started moving in the x direction or game will stall
dx = math.random(0, 1);
if (dx == 0)
then
dx = -1
end
dy = math.random(-1, 1);
end
end
end

3214
termbox.h Normal file

File diff suppressed because it is too large Load Diff

93
termbox_defs.lua Normal file
View File

@ -0,0 +1,93 @@
TB_KEY_CTRL_TILDE = 0x00;
TB_KEY_CTRL_2 = 0x00;
TB_KEY_CTRL_A = 0x01;
TB_KEY_CTRL_B = 0x02;
TB_KEY_CTRL_C = 0x03;
TB_KEY_CTRL_D = 0x04;
TB_KEY_CTRL_E = 0x05;
TB_KEY_CTRL_F = 0x06;
TB_KEY_CTRL_G = 0x07;
TB_KEY_BACKSPACE = 0x08;
TB_KEY_CTRL_H = 0x08;
TB_KEY_TAB = 0x09;
TB_KEY_CTRL_I = 0x09;
TB_KEY_CTRL_J = 0x0a;
TB_KEY_CTRL_K = 0x0b;
TB_KEY_CTRL_L = 0x0c;
TB_KEY_ENTER = 0x0d;
TB_KEY_CTRL_M = 0x0d;
TB_KEY_CTRL_N = 0x0e;
TB_KEY_CTRL_O = 0x0f;
TB_KEY_CTRL_P = 0x10;
TB_KEY_CTRL_Q = 0x11;
TB_KEY_CTRL_R = 0x12;
TB_KEY_CTRL_S = 0x13;
TB_KEY_CTRL_T = 0x14;
TB_KEY_CTRL_U = 0x15;
TB_KEY_CTRL_V = 0x16;
TB_KEY_CTRL_W = 0x17;
TB_KEY_CTRL_X = 0x18;
TB_KEY_CTRL_Y = 0x19;
TB_KEY_CTRL_Z = 0x1a;
TB_KEY_ESC = 0x1b;
TB_KEY_CTRL_LSQ_BRACKET = 0x1b;
TB_KEY_CTRL_3 = 0x1b;
TB_KEY_CTRL_4 = 0x1c;
TB_KEY_CTRL_BACKSLASH = 0x1c;
TB_KEY_CTRL_5 = 0x1d;
TB_KEY_CTRL_RSQ_BRACKET = 0x1d;
TB_KEY_CTRL_6 = 0x1e;
TB_KEY_CTRL_7 = 0x1f;
TB_KEY_CTRL_SLASH = 0x1f;
TB_KEY_CTRL_UNDERSCORE = 0x1f;
TB_KEY_SPACE = 0x20;
TB_KEY_BACKSPACE2 = 0x7f;
TB_KEY_CTRL_8 = 0x7f;
TB_KEY_F1 = (0xffff - 0);
TB_KEY_F2 = (0xffff - 1);
TB_KEY_F3 = (0xffff - 2);
TB_KEY_F4 = (0xffff - 3);
TB_KEY_F5 = (0xffff - 4);
TB_KEY_F6 = (0xffff - 5);
TB_KEY_F7 = (0xffff - 6);
TB_KEY_F8 = (0xffff - 7);
TB_KEY_F9 = (0xffff - 8);
TB_KEY_F10 = (0xffff - 9 );
TB_KEY_F11 = (0xffff - 10);
TB_KEY_F12 = (0xffff - 11);
TB_KEY_INSERT = (0xffff - 12);
TB_KEY_DELETE = (0xffff - 13);
TB_KEY_HOME = (0xffff - 14);
TB_KEY_END = (0xffff - 15);
TB_KEY_PGUP = (0xffff - 16);
TB_KEY_PGDN = (0xffff - 17);
TB_KEY_ARROW_UP = (0xffff - 18);
TB_KEY_ARROW_DOWN = (0xffff - 19);
TB_KEY_ARROW_LEFT = (0xffff - 20);
TB_KEY_ARROW_RIGHT = (0xffff - 21);
TB_KEY_BACK_TAB = (0xffff - 22);
TB_KEY_MOUSE_LEFT = (0xffff - 23);
TB_KEY_MOUSE_RIGHT = (0xffff - 24);
TB_KEY_MOUSE_MIDDLE = (0xffff - 25);
TB_KEY_MOUSE_RELEASE = (0xffff - 26);
TB_KEY_MOUSE_WHEEL_UP = (0xffff - 27);
TB_KEY_MOUSE_WHEEL_DOWN = (0xffff - 28);
TB_DEFAULT = 0x0000;
TB_BLACK = 0x0001;
TB_RED = 0x0002;
TB_GREEN = 0x0003;
TB_YELLOW = 0x0004;
TB_BLUE = 0x0005;
TB_MAGENTA = 0x0006;
TB_CYAN = 0x0007;
TB_WHITE = 0x0008;
TB_BOLD = 0x0100;
TB_UNDERLINE = 0x0200;
TB_REVERSE = 0x0400;
TB_ITALIC = 0x0800;
TB_BLINK = 0x1000;
TB_TRUECOLOR_BOLD = 0x01000000;
TB_TRUECOLOR_UNDERLINE = 0x02000000;
TB_TRUECOLOR_REVERSE = 0x04000000;
TB_TRUECOLOR_ITALIC = 0x08000000;
TB_TRUECOLOR_BLINK = 0x10000000;

26
termbox_render.h Normal file
View File

@ -0,0 +1,26 @@
#ifndef TERMBOX_RENDER_H
#define TERMBOX_RENDER_H
#include <string.h>
#include <stdint.h>
#include <lua-5.3/lualib.h>
#include <lua-5.3/lauxlib.h>
#include "termbox.h"
#include "oct_termbox_sprite.h"
#define OCT_MAX_SPRITE_SIZE 101
#define OCT_NUM_SPRITES 100
void oct_render_termbox_sprite(struct oct_tb_sprite* ots) {
char shape_copy[OCT_MAX_SPRITE_SIZE];
strncpy(shape_copy, ots->shape, OCT_MAX_SPRITE_SIZE-1);
char* line = NULL;
line = strtok(shape_copy, "\n\r");
int x = ots->x;
int y = ots->y;
while (line) {
tb_print(x, y++, ots->fg, ots->bg, line);
line = strtok(NULL, "\n\r");
}
}
#endif

BIN
test Executable file

Binary file not shown.

27
test.c Normal file
View File

@ -0,0 +1,27 @@
#define TB_IMPL
#include "termbox.h"
#include <stdio.h>
int main(int argc, char **argv) {
struct tb_event ev;
int y = 0;
tb_init();
tb_printf(0, y++, TB_GREEN, 0, "hello from termbox");
tb_printf(0, y++, 0, 0, "width=%d height=%d", tb_width(), tb_height());
tb_printf(0, y++, 0, 0, "press any key...");
tb_present();
tb_poll_event(&ev);
y++;
tb_printf(0, y++, 0, 0, "event type=%d key=%d ch=%c", ev.type, ev.key, ev.ch);
tb_printf(0, y++, 0, 0, "press any key to quit...");
tb_present();
tb_poll_event(&ev);
tb_shutdown();
return 0;
}