17 Commits

Author SHA1 Message Date
66bbdea85f [BUG] memcpy minor bug
[FEAT] boot sequence
2021-12-08 09:18:42 +01:00
1a73164990 [CHANGE] Nou model de memòria 2021-12-08 07:47:39 +01:00
290d50e32f [FEAT] memcpy
[CHANGE] New default color 0x0f
2021-12-08 07:34:54 +01:00
963958e596 Merge branch 'master' of https://gitea.sustancia.synology.me/JailDoctor/ascii 2021-12-08 07:11:58 +01:00
806eeb04b5 Working on boot logo 2021-12-08 07:10:06 +01:00
6131607778 canvi de comp 2021-12-07 12:44:15 +01:00
21052edd3d [FEAT] Peek & Poke
[BUG] Mode not restored after ESC
2021-12-06 12:23:36 +01:00
69976ee9f6 [BUG]Mouse coordinates where incorrect 2021-12-05 20:26:22 +01:00
cf608cea78 Still working on the console... 2021-12-05 16:34:05 +01:00
fe9220bb39 Working on the terminal... 2021-12-05 10:16:37 +01:00
9e547e7292 algun fallo raro? 2021-12-04 17:45:46 +01:00
528261395b Working on console interaction... 2021-12-04 14:52:15 +01:00
8f06ad8dab [BUG] No compila en MacOS 2021-12-04 14:21:46 +01:00
7552136845 [FEAT] All screen modes implemented 2021-12-04 10:52:08 +01:00
0482467037 [FEAT] Audio is working 2021-12-04 09:42:33 +01:00
280ca2ceca [FEAT] setchar. Redefinition of characters 2021-12-03 20:10:38 +01:00
72412a01bd [BUG]only using last defined colors
[BUG]print(str,x,y) never reached 0
2021-12-03 20:07:46 +01:00
11 changed files with 673 additions and 82 deletions

1
.gitignore vendored
View File

@@ -2,3 +2,4 @@
ascii
.vscode/*
*.dll
wiki/*

298
ascii.cpp
View File

@@ -2,36 +2,49 @@
#include <stdio.h>
#include <string.h>
#include "lua.h"
#include "font.c"
#include "rom.c"
#define swap(a, b) {auto tmp=a;a=b;b=tmp;}
char lua_filename[1024];
char window_title[256];
uint8_t char_screen[1200];
uint8_t color_screen[1200];
uint8_t mem[8192]; //2400
uint8_t *char_screen = NULL;
uint8_t *color_screen = NULL;
uint8_t screen_width = 40;
uint8_t screen_height = 30;
uint8_t current_color = 0x1e;
uint8_t current_mode = 1;
uint8_t cursor_x = 0;
uint8_t cursor_y = 0;
bool sounding = false;
int audio_freq = 0;
uint32_t audio_len = 0;
#define CHRSCR(x, y) char_screen[x+y*screen_width]
#define COLSCR(x, y) color_screen[x+y*screen_width]
#define MEM_CHAR_OFFSET 2560
#define MEM_BOOT_OFFSET 4608
SDL_Window *mini_win;
SDL_Renderer *mini_ren;
SDL_Texture *mini_bak;
SDL_Texture *mini_bak = NULL;
SDL_AudioDeviceID mini_audio_device;
Uint32 *pixels;
int pitch;
uint32_t palette[16] = { 0x00000000, 0x000000AA, 0x0000AA00, 0x0000AAAA, 0x00AA0000, 0x00AA00AA, 0x00AA5500, 0x00AAAAAA,
0x00555555, 0x005555FF, 0x0055FF55, 0x0055FFFF, 0x00FF5555, 0x00FF55FF, 0x00FFFF55, 0x00FFFFFF };
#define debug_line_size 40
#define debug_line_size 80
#define debug_num_lines 30
#define debug_total_size 1200 //debug_line_size*debug_num_lines
#define debug_total_size 2400 //debug_line_size*debug_num_lines
char debug_text[debug_total_size];
int debug_cursor = 0;
int debug_prompt = 0;
int debug_cursor_blink = 30;
bool should_reset = false;
//Uint8 keymapping[6] = { SDL_SCANCODE_LEFT, SDL_SCANCODE_RIGHT, SDL_SCANCODE_UP, SDL_SCANCODE_DOWN, SDL_SCANCODE_Z, SDL_SCANCODE_X };
const Uint8 *keys;
@@ -41,32 +54,96 @@ int mouse_x, mouse_y, mouse_wheel;
Uint32 mouse_buttons;
void reinit() {
screen_width = 40;
screen_height = 30;
current_color = 0x1e;
cursor_x = 0;
cursor_y = 0;
if (mini_bak != NULL) SDL_DestroyTexture(mini_bak);
switch (current_mode) {
case 0:
screen_width = 80;
screen_height = 30;
current_color = 0x07;
cursor_x = 0;
cursor_y = 0;
char_screen = &mem[0];
color_screen = &mem[1200];
//SDL_RenderSetLogicalSize(mini_ren, 640, 480);
mini_bak = SDL_CreateTexture(mini_ren, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, 640, 240);
break;
case 1:
screen_width = 40;
screen_height = 30;
current_color = 0x07;
cursor_x = 0;
cursor_y = 0;
char_screen = &mem[0];
color_screen = &mem[1200];
//SDL_RenderSetLogicalSize(mini_ren, 320, 240);
mini_bak = SDL_CreateTexture(mini_ren, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, 320, 240);
break;
case 2:
screen_width = 20;
screen_height = 15;
current_color = 0x07;
cursor_x = 0;
cursor_y = 0;
char_screen = &mem[0];
color_screen = &mem[300];
//SDL_RenderSetLogicalSize(mini_ren, 160, 120);
mini_bak = SDL_CreateTexture(mini_ren, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, 160, 120);
break;
}
}
void audioCallback(void * userdata, uint8_t * stream, int len) {
//if (audio_len <= 0) nosound();
static int period=0;
static int v = 16;
SDL_memset(stream, 0, len);
const int flen = min(audio_len, len);
audio_len -= flen;
for (int i=0; i<flen; ++i) {
stream[i] = v;
period++; if (period>=audio_freq) {period = 0; v=-v;}
}
}
uint8_t old_mode = 0;
void romcpy() {
SDL_memcpy(&mem[2560], rom, rom_size);
}
int main(int argc,char*argv[]) {
SDL_strlcpy(lua_filename, "game.lua", 9);
if (argc > 1) SDL_strlcpy(lua_filename, argv[1], 1023);
for (int i=0; i<debug_total_size;++i) debug_text[i] = 32;
SDL_Init(49);
mini_win = SDL_CreateWindow(window_title, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_SHOWN);
mini_win = SDL_CreateWindow(window_title, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640+80, 480+80, SDL_WINDOW_SHOWN);
mini_ren = SDL_CreateRenderer(mini_win, -1, SDL_RENDERER_PRESENTVSYNC);
//SDL_CreateWindowAndRenderer(512,512,0,&mini_win,&mini_ren);
SDL_RenderSetLogicalSize(mini_ren, 320, 240);
mini_bak = SDL_CreateTexture(mini_ren, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, 320, 240);
//SDL_RenderSetLogicalSize(mini_ren, 320, 240);
//mini_bak = SDL_CreateTexture(mini_ren, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, 320, 240);
bool exit = false;
SDL_Event mini_eve;
SDL_AudioSpec audioSpec = {44100, AUDIO_S8, 1, 0, 512, 0, 0, audioCallback, NULL};
mini_audio_device = SDL_OpenAudioDevice(NULL, 0, &audioSpec, NULL, 0);
SDL_PauseAudioDevice(mini_audio_device, 0);
romcpy();
reinit();
debug("ASCII SYSTEM BOOTING...");
lua_init();
lua_init(NULL);
lua_call_init();
while(!exit) {
if (should_reset) {
should_reset = false;
setmode(1);
reinit();
lua_init(lua_filename);
lua_call_init();
}
key_just_pressed = 0;
mouse_wheel = 0;
while(SDL_PollEvent(&mini_eve)) {
@@ -74,16 +151,20 @@ int main(int argc,char*argv[]) {
if (mini_eve.type == SDL_KEYDOWN) {
if (mini_eve.key.keysym.scancode == SDL_SCANCODE_ESCAPE) {
if (lua_is_playing()) {
lua_quit();
reinit();
lua_pause();
//lua_quit();
old_mode = current_mode;
setmode(0);
} else {
lua_init();
lua_call_init();
setmode(old_mode);
lua_resume();
//lua_init();
//lua_call_init();
}
} else if (mini_eve.key.keysym.scancode == SDL_SCANCODE_F5) {
lua_quit();
reinit();
lua_init();
lua_init(lua_filename);
lua_call_init();
} else {
key_just_pressed = mini_eve.key.keysym.scancode;
@@ -95,37 +176,74 @@ int main(int argc,char*argv[]) {
}
keys = SDL_GetKeyboardState(NULL);
mouse_buttons = SDL_GetMouseState(&mouse_x, &mouse_y);
mouse_x /= 4; mouse_y /= 4;
mouse_x = (mouse_x-40) / (640/screen_width);
mouse_y = (mouse_y-40) / (480/screen_height);
if (lua_is_playing()) {
lua_call_update();
} else {
debug_cursor_blink--;
if (debug_cursor_blink == 0) {
debug_cursor_blink = 30;
debug_text[debug_cursor] = debug_text[debug_cursor]==32 ? 95 : 32;
}
loop();
}
SDL_LockTexture(mini_bak, NULL, (void**)&pixels, &pitch);
for (int y=0; y<screen_height; ++y) {
for (int x=0; x<screen_width; ++x) {
const uint32_t ink_color = palette[current_color & 0x0f];
const uint32_t paper_color = palette[current_color >> 4];
const uint8_t chr = CHRSCR(x,y);
for (int l=0; l<8; ++l) {
const uint8_t line = font[chr*8+l];
for (int b=0; b<8; ++b) {
if ((line >> (7-b)) & 0x01) {
pixels[b+(x*8)+((y*8)+l)*(screen_width*8)] = ink_color;
} else {
pixels[b+(x*8)+((y*8)+l)*(screen_width*8)] = paper_color;
switch (current_mode) {
case 0:
for (int y=0; y<screen_height; ++y) {
for (int x=0; x<screen_width; ++x) {
const uint8_t chr_color = current_color; //COLSCR(x,y);
const uint32_t ink_color = palette[chr_color & 0x0f];
const uint32_t paper_color = palette[chr_color >> 4];
const uint8_t chr = CHRSCR(x,y);
for (int l=0; l<8; ++l) {
const uint8_t line = mem[MEM_CHAR_OFFSET+chr*8+l];
for (int b=0; b<8; ++b) {
if ((line >> (7-b)) & 0x01) {
pixels[b+(x*8)+((y*8)+l)*(screen_width*8)] = ink_color;
} else {
pixels[b+(x*8)+((y*8)+l)*(screen_width*8)] = paper_color;
}
}
}
}
}
}
break;
case 1:
case 2:
for (int y=0; y<screen_height; ++y) {
for (int x=0; x<screen_width; ++x) {
const uint8_t chr_color = COLSCR(x,y);
const uint32_t ink_color = palette[chr_color & 0x0f];
const uint32_t paper_color = palette[chr_color >> 4];
const uint8_t chr = CHRSCR(x,y);
for (int l=0; l<8; ++l) {
const uint8_t line = mem[MEM_CHAR_OFFSET+chr*8+l];
for (int b=0; b<8; ++b) {
if ((line >> (7-b)) & 0x01) {
pixels[b+(x*8)+((y*8)+l)*(screen_width*8)] = ink_color;
} else {
pixels[b+(x*8)+((y*8)+l)*(screen_width*8)] = paper_color;
}
}
}
}
}
break;
}
//for (int i=0;i<screen_surface->size;++i) pixels[i] = palette[screen_surface->p[i]];
SDL_UnlockTexture(mini_bak);
SDL_RenderCopy(mini_ren, mini_bak, NULL, NULL);
SDL_SetRenderDrawColor(mini_ren, (palette[0] >> 16)&0xff, (palette[0] >> 8)&0xff, palette[0]&0xff, 0);
//SDL_SetRenderDrawColor(mini_ren, 255, 0, 0, 0);
SDL_RenderClear(mini_ren);
SDL_Rect rect = {40, 40, 640, 480};
SDL_RenderCopy(mini_ren, mini_bak, NULL, &rect);
//SDL_RenderCopy(mini_ren, mini_bak, NULL, NULL);
SDL_RenderPresent(mini_ren);
}
lua_quit();
@@ -135,7 +253,13 @@ int main(int argc,char*argv[]) {
void cls(uint8_t value) {
SDL_memset(char_screen, value, screen_width*screen_height);
SDL_memset(color_screen, current_color, screen_width*screen_height);
if (current_mode != 0) SDL_memset(color_screen, current_color, screen_width*screen_height);
if (!lua_is_playing()) {
SDL_memset(debug_text, 32, debug_total_size);
debug_cursor = 1;
debug_prompt = 0;
debug_text[debug_prompt] = '>';
}
}
void ink(uint8_t value) {
@@ -156,14 +280,18 @@ void locate(uint8_t x, uint8_t y) {
}
void print(const char *str, int x, int y) {
if (x > 0) cursor_x = min(x, screen_width-1);
if (y > 0) cursor_y = min(y, screen_height-1);
if (x >= 0) cursor_x = min(x, screen_width-1);
if (y >= 0) cursor_y = min(y, screen_height-1);
int len = SDL_strlen(str);
if ((cursor_x+len) > screen_width) len -= ((cursor_x+len) - screen_width);
//int offset = x+y*screen_width;
for (int i=0; i < len; ++i) {
CHRSCR(cursor_x+i, cursor_y) = str[i];
COLSCR(cursor_x+i, cursor_y) = current_color;
if (current_mode != 0) COLSCR(cursor_x+i, cursor_y) = current_color;
//char_screen[offset+i] = str[i];
//if (current_mode != 0) color_screen[offset+i] = current_color;
}
cursor_x += len;
}
bool btn(uint8_t i) {
@@ -174,6 +302,10 @@ bool btnp(uint8_t i) {
return key_just_pressed == i;
}
uint8_t whichbtn() {
return key_just_pressed;
}
int mousex() {
return mouse_x;
}
@@ -207,6 +339,7 @@ float sgn(float x) {
}
#ifndef __LINUX__
#ifndef __APPLE__
float ceil(float x) {
return SDL_ceilf(x);
@@ -228,6 +361,7 @@ float sqrt(float x) {
return SDL_sqrtf(x);
}
#endif
#endif
float max(float x, float y) {
@@ -251,14 +385,32 @@ int rnd(int x) {
}*/
char tostr_tmp[256];
const char* tostr(int val) {
const char* tostr(float val) {
return SDL_itoa(val, tostr_tmp, 10);
}
void debug_set_prompt() {
debug_prompt = debug_cursor++;
debug_text[debug_prompt] = '>';
}
void debug_one_line_up() {
for (int i=0; i<debug_total_size-debug_line_size;++i) debug_text[i] = debug_text[i+debug_line_size];
for (int i=debug_total_size-debug_line_size; i<debug_total_size;++i) debug_text[i] = 32;
debug_cursor = debug_total_size-debug_line_size;
debug_set_prompt();
}
void debugchr(const uint8_t chr) {
if (chr == 8) {
if (debug_cursor>(debug_prompt+1)) {
debug_text[debug_cursor--] = 32;
debug_text[debug_cursor] = 32;
}
} else {
debug_text[debug_cursor++] = chr;
if (debug_cursor >= debug_total_size) debug_one_line_up();
}
}
void debug(const char *str) {
@@ -269,6 +421,7 @@ void debug(const char *str) {
}
debug_cursor = (int(debug_cursor/debug_line_size)+1)*debug_line_size;
if (debug_cursor >= debug_total_size) debug_one_line_up();
debug_set_prompt();
}
void pdebug() {
@@ -276,32 +429,85 @@ void pdebug() {
for (int y=0; y<debug_num_lines;++y) {
for (int x=0; x<debug_line_size;++x) {
CHRSCR(x, y) = debug_text[i++];
COLSCR(x, y) = 0x1e;
//COLSCR(x, y) = 0x1e;
}
}
}
void debug_get_cmd() {
debug_text[debug_cursor] = 0;
lua_call_cmd(&debug_text[debug_prompt+1]);
}
uint8_t ascii(const char *str, uint8_t index) {
return str[index];
}
char chr_trans[2] = "X";
const char* chr(uint8_t ascii) {
uint8_t* peiv = (uint8_t*)chr_trans;
*peiv = ascii;
return chr_trans;
}
void setchar(uint8_t index, uint8_t b0, uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4, uint8_t b5, uint8_t b6, uint8_t b7) {
// [TODO]
mem[MEM_CHAR_OFFSET+index*8] = b0;
mem[MEM_CHAR_OFFSET+index*8+1] = b1;
mem[MEM_CHAR_OFFSET+index*8+2] = b2;
mem[MEM_CHAR_OFFSET+index*8+3] = b3;
mem[MEM_CHAR_OFFSET+index*8+4] = b4;
mem[MEM_CHAR_OFFSET+index*8+5] = b5;
mem[MEM_CHAR_OFFSET+index*8+6] = b6;
mem[MEM_CHAR_OFFSET+index*8+7] = b7;
}
uint8_t peek(uint16_t addr) {
// [TODO]
return 0;
if (addr >= 0x2000) return 0;
return mem[addr];
}
void poke(uint16_t addr, uint8_t val) {
// [TODO]
if (addr >= 0x2000) return;
mem[addr] = val;
}
void memcpy(uint16_t dst, uint16_t src, uint16_t size) {
if ((dst<=src) && (dst+size>=src)) return;
if ((dst>=src) && (src+size>=dst)) return;
SDL_memcpy(&mem[dst], &mem[src], size);
}
void sound(float freq, uint32_t len) {
// [TODO]
audio_len = len*44.1f;
audio_freq = 44100.0f/freq/2.0f;
sounding = true;
}
void nosound() {
// [TODO]
//SDL_PauseAudioDevice(mini_audio_device, 1);
audio_len = 0;
sounding = false;
}
void setmode(const uint8_t mode) {
current_mode = mode;
reinit();
}
void load(const char* str) {
if (str!=NULL) SDL_strlcpy(lua_filename, str, SDL_strlen(str)+1);
should_reset = true;
}
void fileout(const char* str, uint16_t addr, uint16_t size) {
FILE* f = fopen(str, "wb");
fwrite(&mem[addr], size, 1, f);
fclose(f);
}
void filein(const char* str, uint16_t addr, uint16_t size) {
FILE* f = fopen(str, "rb");
fread(&mem[addr], size, 1, f);
fclose(f);
}

15
ascii.h
View File

@@ -122,6 +122,7 @@ void print(const char *str, int x = -1, int y = -1);
bool btn(uint8_t i);
bool btnp(uint8_t i);
uint8_t whichbtn();
int mousex();
int mousey();
@@ -136,12 +137,14 @@ float flr(float x);
float sgn(float x);
#ifndef __LINUX__
#ifndef __APPLE__
float ceil(float x);
float sin(float x);
float cos(float x);
float atan2(float dx, float dy);
float sqrt(float x);
#endif
#endif
float max(float x, float y);
float mid(float x, float y, float z);
@@ -150,16 +153,26 @@ float min(float x, float y);
int rnd(int x);
//void srand(int x);
const char* tostr(int val);
const char* tostr(float val);
void debugchr(const uint8_t chr);
void debug(const char *str);
void pdebug();
void debug_get_cmd();
uint8_t ascii(const char *str, uint8_t index);
const char* chr(uint8_t ascii);
void setchar(uint8_t index, uint8_t b0, uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4, uint8_t b5, uint8_t b6, uint8_t b7);
uint8_t peek(uint16_t addr);
void poke(uint16_t addr, uint8_t val);
void memcpy(uint16_t dst, uint16_t src, uint16_t size);
void sound(float freq, uint32_t len);
void nosound();
void setmode(const uint8_t mode);
void load(const char* str);
void fileout(const char* str, uint16_t addr, uint16_t size);
void filein(const char* str, uint16_t addr, uint16_t size);

View File

@@ -1,19 +1,17 @@
function init()
x, y, dx, dy = 0, 0, 1, 1
paper(COLOR_BLACK)
ink(COLOR_RED)
setmode(0)
end
function update()
cls()
x = x + dx
y = y + dy
if x == 39 or x == 0 then
dx = -dx
locate(0,0)
if mousebutton(1) then
print("Has pulsat el boto esquerre")
elseif mousebutton(2) then
print("Has pulsat el boto del mig")
elseif mousebutton(3) then
print("Has pulsat el boto dret")
else
print("No has pulsat cap boto")
end
if y == 29 or y == 0 then
dy = -dy
end
locate(x, y)
print("\224")
end

137
lua.cpp
View File

@@ -152,7 +152,7 @@ extern "C" {
return 0;
}
static int cpp_tostr(lua_State *L) {
int val = luaL_checknumber(L, 1);
float val = luaL_checknumber(L, 1);
lua_pushstring(L, tostr(val));
return 1;
}
@@ -162,12 +162,17 @@ extern "C" {
lua_pushinteger(L, ascii(str, index));
return 1;
}
static int cpp_chr(lua_State *L) {
int val = luaL_checkinteger(L, 1);
lua_pushstring(L, chr(val));
return 1;
}
static int cpp_strlen(lua_State *L) {
const char* str = luaL_checkstring(L, 1);
lua_pushinteger(L, strlen(str));
return 1;
}
static int cpp_setchar(lua_State *L) {
int index = luaL_checkinteger(L, 1);
int b0 = luaL_checkinteger(L, 2);
@@ -195,6 +200,14 @@ extern "C" {
return 0;
}
static int cpp_memcpy(lua_State *L) {
int dst = luaL_checkinteger(L, 1);
int src = luaL_checkinteger(L, 2);
int size = luaL_checkinteger(L, 3);
memcpy(dst, src, size);
return 0;
}
static int cpp_sound(lua_State *L) {
float freq = luaL_checknumber(L, 1);
int len = luaL_checkinteger(L, 2);
@@ -206,25 +219,79 @@ extern "C" {
nosound();
return 0;
}
static int cpp_setmode(lua_State *L) {
int val = luaL_checkinteger(L, 1);
setmode(val);
return 0;
}
static int cpp_load(lua_State *L) {
const char* str = luaL_optstring(L, 1, NULL);
load(str);
return 0;
}
static int cpp_log(lua_State *L) {
const char* str = luaL_checkstring(L, 1);
debug(str);
return 0;
}
static int cpp_fileout(lua_State *L) {
const char* str = luaL_checkstring(L, 1);
const uint16_t addr = luaL_checknumber(L, 2);
const uint16_t size = luaL_checknumber(L, 3);
fileout(str, addr, size);
return 0;
}
static int cpp_filein(lua_State *L) {
const char* str = luaL_checkstring(L, 1);
const uint16_t addr = luaL_checknumber(L, 2);
const uint16_t size = luaL_checknumber(L, 3);
filein(str, addr, size);
return 0;
}
}
#define STATE_STOPPED 0
#define STATE_PAUSED 1
#define STATE_PLAYING 2
lua_State *L;
bool is_playing = false;
uint8_t lua_state = STATE_STOPPED;
bool init_exists = false;
bool update_exists = false;
bool lua_is_playing() {
return is_playing;
return lua_state == STATE_PLAYING;
}
void lua_init() {
L = luaL_newstate();
const char boot[] = "function init()setmode(1)cls()memcpy(360,4608,240)memcpy(1560,4848,240)ink(1)print('G A M E',8,16)ink(4)print('S Y S T E M',20,16)ink(8)print('v0.5.1',26,8)w=0 end function update()w=w+1 if w>90 then cls()load()end end";
if (luaL_loadfile(L, "game.lua")) {
debug("error loading game");
debug(lua_tostring(L, -1));
lua_pop(L,1);
return;
void lua_init(const char* filename, const bool start_playing) {
if (lua_state != STATE_STOPPED) lua_quit();
L = luaL_newstate();
init_exists = update_exists = false;
bool file_loaded = true;
if (filename == NULL) {
if (luaL_loadstring(L, boot)) {
debug("BOOT ERROR");
debug(lua_tostring(L, -1));
lua_pop(L,1);
setmode(0);
file_loaded = false;
}
} else {
if (luaL_loadfile(L, filename)) {
debug("ERROR LOADING GAME");
debug(lua_tostring(L, -1));
lua_pop(L,1);
setmode(0);
file_loaded = false;
}
}
lua_pushcfunction(L,cpp_cls); lua_setglobal(L, "cls");
@@ -254,17 +321,26 @@ void lua_init() {
lua_pushcfunction(L,cpp_max); lua_setglobal(L, "max");
lua_pushcfunction(L,cpp_mid); lua_setglobal(L, "mid");
lua_pushcfunction(L,cpp_min); lua_setglobal(L, "min");
lua_pushcfunction(L,cpp_rnd); lua_setglobal(L, "rnd");
lua_pushcfunction(L,cpp_srand); lua_setglobal(L, "srand");
lua_pushcfunction(L,cpp_tostr); lua_setglobal(L, "tostr");
lua_pushcfunction(L,cpp_ascii); lua_setglobal(L, "ascii");
lua_pushcfunction(L,cpp_chr); lua_setglobal(L, "chr");
lua_pushcfunction(L,cpp_strlen); lua_setglobal(L, "strlen");
lua_pushcfunction(L,cpp_setchar); lua_setglobal(L, "setchar");
lua_pushcfunction(L,cpp_peek); lua_setglobal(L, "peek");
lua_pushcfunction(L,cpp_poke); lua_setglobal(L, "poke");
lua_pushcfunction(L,cpp_memcpy); lua_setglobal(L, "memcpy");
lua_pushcfunction(L,cpp_sound); lua_setglobal(L, "sound");
lua_pushcfunction(L,cpp_nosound); lua_setglobal(L, "nosound");
lua_pushcfunction(L,cpp_setmode); lua_setglobal(L, "setmode");
lua_pushcfunction(L,cpp_load); lua_setglobal(L, "load");
lua_pushcfunction(L,cpp_log); lua_setglobal(L, "log");
lua_pushcfunction(L,cpp_fileout); lua_setglobal(L, "fileout");
lua_pushcfunction(L,cpp_filein); lua_setglobal(L, "filein");
lua_pushinteger(L, 0); lua_setglobal(L, "KEY_UNKNOWN");
lua_pushinteger(L, 4); lua_setglobal(L, "KEY_A");
@@ -391,10 +467,13 @@ void lua_init() {
lua_pushinteger(L, 14); lua_setglobal(L, "COLOR_YELLOW");
lua_pushinteger(L, 15); lua_setglobal(L, "COLOR_WHITE");
if (!file_loaded) return;
if (lua_pcall(L,0, LUA_MULTRET, 0)) {
debug("runtime error");
debug("RUNTIME ERROR");
debug(lua_tostring(L, -1));
lua_pop(L,1);
setmode(0);
return;
}
@@ -406,17 +485,28 @@ void lua_init() {
if (lua_isfunction(L,-1)) update_exists = true;
lua_pop(L,1);
is_playing = true;
lua_state = start_playing ? STATE_PLAYING : STATE_PAUSED;
}
void lua_call_cmd(const char* str) {
if (luaL_dostring(L, str)) {
debug(" ");
debug("ERROR");
debug(lua_tostring(L, -1));
lua_pop(L,1);
}
debug(" ");
}
void lua_call_init() {
if (!init_exists) return;
lua_getglobal(L, "init");
if (lua_pcall(L, 0, 0, 0)) {
debug("runtime error");
debug("RUNTIME ERROR");
debug(lua_tostring(L, -1));
lua_pop(L,1);
is_playing = false;
setmode(0);
lua_state = STATE_STOPPED;
}
}
@@ -424,15 +514,24 @@ void lua_call_update() {
if (!update_exists) return;
lua_getglobal(L, "update");
if (lua_pcall(L, 0, 0, 0)) {
debug("runtime error");
debug("RUNTIME ERROR");
debug(lua_tostring(L, -1));
lua_pop(L,1);
is_playing = false;
setmode(0);
lua_state = STATE_STOPPED;
}
}
void lua_quit() {
if (!is_playing) return;
is_playing = false;
if (lua_state == STATE_STOPPED) return;
lua_state = STATE_STOPPED;
lua_close(L);
}
void lua_pause() {
lua_state = STATE_PAUSED;
}
void lua_resume() {
lua_state = STATE_PLAYING;
}

5
lua.h
View File

@@ -1,7 +1,10 @@
#pragma once
bool lua_is_playing();
void lua_init();
void lua_init(const char* filename, const bool start_playing=true);
void lua_call_init();
void lua_call_update();
void lua_call_cmd(const char* str);
void lua_quit();
void lua_pause();
void lua_resume();

View File

@@ -3,7 +3,37 @@
int current_editor = 0;
void do_terminal() {
cls(0);
SDL_Keymod mods = SDL_GetModState();
const uint8_t key = whichbtn();
if (key != KEY_UNKNOWN) {
if (key < 30) {
if ((mods & KMOD_SHIFT) || (mods & KMOD_CAPS)) {
debugchr(key+61);
} else {
debugchr(key+93);
}
}
//else if (key < 39) debugchr(key+19);
else if (key == KEY_0) { if (mods & KMOD_SHIFT) { debugchr('='); } else { debugchr('0'); } }
else if (key == KEY_1) { if (mods & KMOD_SHIFT) { debugchr('!'); } else if (mods & KMOD_RALT) { debugchr('|'); } else { debugchr('1'); } }
else if (key == KEY_2) { if (mods & KMOD_SHIFT) { debugchr('"'); } else if (mods & KMOD_RALT) { debugchr('@'); } else { debugchr('2'); } }
else if (key == KEY_3) { if (mods & KMOD_SHIFT) { debugchr(144); } else if (mods & KMOD_RALT) { debugchr('#'); } else { debugchr('3'); } }
else if (key == KEY_4) { if (mods & KMOD_SHIFT) { debugchr('$'); } else if (mods & KMOD_RALT) { debugchr('~'); } else { debugchr('4'); } }
else if (key == KEY_5) { if (mods & KMOD_SHIFT) { debugchr('%'); } else if (mods & KMOD_RALT) { debugchr(180); } else { debugchr('5'); } }
else if (key == KEY_6) { if (mods & KMOD_SHIFT) { debugchr('&'); } else if (mods & KMOD_RALT) { debugchr(173); } else { debugchr('6'); } }
else if (key == KEY_7) { if (mods & KMOD_SHIFT) { debugchr('/'); } else { debugchr('7'); } }
else if (key == KEY_8) { if (mods & KMOD_SHIFT) { debugchr('('); } else { debugchr('8'); } }
else if (key == KEY_9) { if (mods & KMOD_SHIFT) { debugchr(')'); } else { debugchr('9'); } }
else if (key == KEY_RETURN) debug_get_cmd();
else if (key == KEY_SPACE) debugchr(32);
else if (key == KEY_BACKSPACE) debugchr(8);
else if (key == KEY_MINUS) { if (mods & KMOD_SHIFT) { debugchr('?'); } else { debugchr('\''); } }
else if (key == KEY_EQUALS) { if (mods & KMOD_SHIFT) { debugchr(174); } else { debugchr(175); } }
else if (key == KEY_COMMA) { if (mods & KMOD_SHIFT) { debugchr(';'); } else { debugchr(','); } }
else if (key == KEY_PERIOD) { if (mods & KMOD_SHIFT) { debugchr(':'); } else { debugchr('.'); } }
else if (key == KEY_SLASH) { if (mods & KMOD_SHIFT) { debugchr('_'); } else { debugchr('-'); } }
}
//cls(0);
pdebug();
}

91
mapedit.lua Normal file
View File

@@ -0,0 +1,91 @@
ind = 22
function init()
setmode(2)
map = {}
col = {}
for i=0,299 do
map[i] = 32
col[i] = 0x0f
end
map[ind] = 65
sel_col = 0x0f
sel_chr = 65
end
blink=30
function update()
if btn(KEY_TAB) then
draw_picker()
update_picker()
else
draw_map()
--update_map()
end
end
function draw_picker()
for i=0,255 do
poke(i, i)
poke(300+i, sel_col)
end
for i=0,15 do
poke(580+i, i<<4)
poke(560+i, i<<4)
poke(280+i, 32)
poke(260+i, 32)
end
poke(580, 15) poke(560, 15)
poke(260+(sel_col&0xf), 203)
poke(280+(sel_col>>4), 203)
local mx, my = mousex(), mousey()
if my<13 then
poke(300+(mx+my*20), 0x4e)
print(mx+my*20, 16, 13)
elseif my>=13 then
poke(mx+my*20, 144)
end
end
function update_picker()
local mx, my = mousex(), mousey()
if mousebutton(1) then
if my<13 then
sel_chr = mx+my*20
else
if mx < 16 then
if my == 13 then
sel_col = (sel_col & 0xf0) + mx
else
sel_col = (sel_col & 0x0f) + (mx << 4)
end
end
end
end
end
function draw_map()
for i=0,299 do
poke(i, map[i])
poke(300+i, col[i])
end
local mx, my = mousex(), mousey()
if mousebutton(1) then
map[mx+my*20] = sel_chr
col[mx+my*20] = sel_col
end
if btn(KEY_SPACE) then
sel_chr = peek(mx+my*20)
sel_col = peek(300+mx+my*20)
end
blink = blink - 1
if blink < 30 then
if blink==0 then blink = 60 end
local mx, my = mousex(), mousey()
local c = peek(300+(mx+my*20))
poke(300+(mx+my*20), c~0xff)
end
end

View File

@@ -1,6 +1,8 @@
#pragma once
unsigned char font[2048] = {
const int rom_size = 2528;
unsigned char rom[rom_size] = {
0xFF, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xFF, 0xFF, 0xC0, 0xC0, 0xC0,
0xC0, 0xC0, 0xC0, 0xC0, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0xFF,
0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0xFF, 0x0C, 0x18, 0x30, 0x7E,
@@ -171,5 +173,46 @@ unsigned char font[2048] = {
0x90, 0x28, 0x24, 0x22, 0x38, 0x38, 0x90, 0x7C, 0x12, 0x28, 0x48, 0x88,
0x00, 0x3C, 0x18, 0x3C, 0x3C, 0x3C, 0x18, 0x00, 0x3C, 0xFF, 0xFF, 0x18,
0x0C, 0x18, 0x30, 0x18, 0x18, 0x3C, 0x7E, 0x18, 0x18, 0x7E, 0x3C, 0x18,
0x00, 0x24, 0x66, 0xFF, 0x66, 0x24, 0x00, 0x00
0x00, 0x24, 0x66, 0xFF, 0x66, 0x24, 0x00, 0x00,
// BOOT LOGO
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0xD6, 0x8F, 0x8F, 0x8F,
0x8F, 0xD4, 0xD6, 0x8F, 0x8F, 0x8F, 0x8F, 0xD4, 0xD6, 0x8F, 0x8F, 0x8F,
0x8F, 0xD4, 0xD6, 0x8F, 0xD4, 0xD6, 0x8F, 0xD4, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0xD4,
0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0xD4, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F,
0xD4, 0x8F, 0x8F, 0xD4, 0x8F, 0x8F, 0x8F, 0x20, 0x20, 0x20, 0x8F, 0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x08, 0x0C, 0x0C, 0x04, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F
};

1
scr_min.bin Normal file
View File

@@ -0,0 +1 @@
֏<><D68F><EFBFBD><EFBFBD>֏<EFBFBD><D68F><EFBFBD><EFBFBD>֏<EFBFBD><D68F><EFBFBD><EFBFBD>֏<EFBFBD>֏<EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ԏ<EFBFBD><D48F><EFBFBD><EFBFBD>ԏ<EFBFBD><D48F><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ԏ<EFBFBD>ԏ<EFBFBD><D48F> <20><>ԏ<EFBFBD><D48F> <20><>ԏ<EFBFBD><D48F><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֏<EFBFBD><D68F><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ԏ<EFBFBD>ԏ<EFBFBD><D48F><EFBFBD><EFBFBD><EFBFBD> <20><>ԏ<EFBFBD>ԏ<EFBFBD><D48F><EFBFBD><EFBFBD>ԏ<EFBFBD><D48F><EFBFBD><EFBFBD>ԏ<EFBFBD>ԏ<EFBFBD><D48F> <07><07><07><01><07>x<08><><EFBFBD>xxxx<07>x<0F><><EFBFBD>xxxxxx<0F>xxx<07>x <0C> 

106
scredit.lua Normal file
View File

@@ -0,0 +1,106 @@
ind = 22
function init()
setmode(1)
map = {}
col = {}
for i=0,1199 do
map[i] = 32
col[i] = 0x0f
end
map[ind] = 65
sel_col = 0x0f
sel_chr = 65
end
blink=30
function update()
if btn(KEY_TAB) then
draw_picker()
update_picker()
else
draw_map()
--update_map()
end
end
function draw_picker()
for y=0,15 do
for x=0,15 do
poke(x+y*40, x+y*16)
poke(1200+x+y*40, sel_col)
end
end
for i=0,15 do
poke(1840+i, i<<4)
poke(1880+i, i<<4)
poke(640+i, 32)
poke(680+i, 32)
end
poke(1840, 15) poke(1880, 15)
poke(640+(sel_col&0xf), 203)
poke(680+(sel_col>>4), 203)
local mx, my = mousex(), mousey()
if mx<16 then
if my<16 then
poke(1200+(mx+my*40), 0x4e)
print(mx+my*40, 16, 13)
elseif my>=16 then
poke(mx+my*40, 144)
end
end
end
function update_picker()
local mx, my = mousex(), mousey()
if mousebutton(1) then
if mx<16 then
if my<16 then
sel_chr = mx+my*16
else
if my == 16 then
sel_col = (sel_col & 0xf0) + mx
elseif my == 17 then
sel_col = (sel_col & 0x0f) + (mx << 4)
end
end
end
end
end
function draw_map()
for i=0,1199 do
poke(i, map[i])
poke(1200+i, col[i])
end
local mx, my = mousex(), mousey()
if mousebutton(1) then
map[mx+my*40] = sel_chr
col[mx+my*40] = sel_col
end
if btn(KEY_SPACE) then
sel_chr = peek(mx+my*40)
sel_col = peek(1200+mx+my*40)
end
blink = blink - 1
if blink < 30 then
if blink==0 then blink = 60 end
local mx, my = mousex(), mousey()
local c = peek(1200+(mx+my*40))
poke(1200+(mx+my*40), c~0xff)
end
if btn(KEY_S) then
fileout("scr.bin", 0, 2400);
elseif btn(KEY_L) then
filein("scr.bin", 0, 2400);
for i=0,1199 do
map[i] = peek(i)
col[i] = peek(1200+i)
end
end
end