12 Commits

Author SHA1 Message Date
a349cf5c0c v0.6.0
[FEAT] New mode(3) with 32x24 characters
2022-02-21 16:38:26 +01:00
f889faa472 Tools updated 2022-02-18 17:23:22 +01:00
743fd2b8cc Sokoban progress 2021-12-20 19:07:25 +01:00
841f4f66f3 [FEAT] cnt() i rst() 2021-12-20 18:15:39 +01:00
82bebe3eeb Added sokoban WIP to demos 2021-12-20 10:46:18 +01:00
c52687f0eb [FIX] If mouse outside window or in border, mouse_x & mouse_y no longer update 2021-12-17 16:50:32 +01:00
97daca8097 [FIX] Some 'setmode's remained in C code 2021-12-17 16:45:27 +01:00
64bcedd82f [CHANGED] 'setmode' renamed to 'mode' 2021-12-17 16:43:06 +01:00
fe9f4fed9c Afegida la demo del efecte matrix i el tweetcart equivalent 2021-12-17 16:40:09 +01:00
56f2e202e8 [WIP] Defender clone 2021-12-15 19:58:58 +01:00
9b1573be9d Code editor WIP 2021-12-15 19:24:56 +01:00
11bb1b5283 Code Editor WIP 2021-12-15 12:16:36 +01:00
17 changed files with 536 additions and 229 deletions

3
.gitignore vendored
View File

@@ -3,3 +3,6 @@ ascii
.vscode/* .vscode/*
*.dll *.dll
wiki/* wiki/*
scr_min.c
tests.lua
fake_editor.lua

View File

@@ -21,6 +21,10 @@ uint8_t *char_screen = NULL;
uint8_t *color_screen = NULL; uint8_t *color_screen = NULL;
uint8_t screen_width = 40; uint8_t screen_width = 40;
uint8_t screen_height = 30; uint8_t screen_height = 30;
int v_screen_w = 640;
int v_screen_h = 480;
uint8_t hborder = 40;
uint8_t vborder = 40;
uint8_t current_color = 0x1e; uint8_t current_color = 0x1e;
uint8_t current_border = 0; uint8_t current_border = 0;
uint8_t current_mode = 1; uint8_t current_mode = 1;
@@ -42,6 +46,7 @@ SDL_Texture *mini_bak = NULL;
SDL_AudioDeviceID mini_audio_device; SDL_AudioDeviceID mini_audio_device;
Uint32 *pixels; Uint32 *pixels;
int pitch; int pitch;
Uint32 counter=0;
uint32_t palette[16] = { 0x00000000, 0x000000AA, 0x0000AA00, 0x0000AAAA, 0x00AA0000, 0x00AA00AA, 0x00AA5500, 0x00AAAAAA, uint32_t palette[16] = { 0x00000000, 0x000000AA, 0x0000AA00, 0x0000AAAA, 0x00AA0000, 0x00AA00AA, 0x00AA5500, 0x00AAAAAA,
0x00555555, 0x005555FF, 0x0055FF55, 0x0055FFFF, 0x00FF5555, 0x00FF55FF, 0x00FFFF55, 0x00FFFFFF }; 0x00555555, 0x005555FF, 0x0055FF55, 0x0055FFFF, 0x00FF5555, 0x00FF55FF, 0x00FFFF55, 0x00FFFFFF };
@@ -63,6 +68,9 @@ const char* get_filename() {
void reinit() { void reinit() {
if (mini_bak != NULL) SDL_DestroyTexture(mini_bak); if (mini_bak != NULL) SDL_DestroyTexture(mini_bak);
counter=0;
hborder = vborder = 40;
v_screen_w = 640; v_screen_h = 480;
switch (current_mode) { switch (current_mode) {
case 0: case 0:
screen_width = 80; screen_width = 80;
@@ -100,17 +108,21 @@ void reinit() {
//SDL_RenderSetLogicalSize(mini_ren, 160, 120); //SDL_RenderSetLogicalSize(mini_ren, 160, 120);
mini_bak = SDL_CreateTexture(mini_ren, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, 160, 120); mini_bak = SDL_CreateTexture(mini_ren, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, 160, 120);
break; break;
case 3: // SUPERSECRET MODE FOR THE EDITOR!!! case 3:
screen_width = 80; hborder = 104;
screen_height = 30; vborder = 88;
current_color = 0x1f; v_screen_w = 512;
current_border = 9; v_screen_h = 384;
screen_width = 32;
screen_height = 24;
current_color = 0x07;
current_border = 0;
cursor_x = 0; cursor_x = 0;
cursor_y = 0; cursor_y = 0;
char_screen = &mem[0]; char_screen = &mem[0];
color_screen = &mem[0x1200]; color_screen = &mem[1200];
//SDL_RenderSetLogicalSize(mini_ren, 640, 480); //SDL_RenderSetLogicalSize(mini_ren, 640, 480);
mini_bak = SDL_CreateTexture(mini_ren, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, 640, 240); mini_bak = SDL_CreateTexture(mini_ren, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, 256, 192);
break; break;
} }
} }
@@ -189,7 +201,7 @@ int main(int argc,char*argv[]) {
while(!exit) { while(!exit) {
if (should_reset) { if (should_reset) {
should_reset = false; should_reset = false;
setmode(1); mode(1);
reinit(); reinit();
lua_init(lua_filename); lua_init(lua_filename);
lua_call_init(); lua_call_init();
@@ -215,6 +227,7 @@ int main(int argc,char*argv[]) {
//lua_call_init(); //lua_call_init();
} }
} else if (mini_eve.key.keysym.scancode == SDL_SCANCODE_F5) { } else if (mini_eve.key.keysym.scancode == SDL_SCANCODE_F5) {
execute_run();
lua_quit(); lua_quit();
reinit(); reinit();
lua_init(lua_filename); lua_init(lua_filename);
@@ -228,9 +241,12 @@ int main(int argc,char*argv[]) {
} }
} }
keys = SDL_GetKeyboardState(NULL); keys = SDL_GetKeyboardState(NULL);
mouse_buttons = SDL_GetMouseState(&mouse_x, &mouse_y); int mx, my;
mouse_x = (mouse_x-40) / (640/screen_width); mouse_buttons = SDL_GetMouseState(&mx, &my);
mouse_y = (mouse_y-40) / (480/screen_height); if (mx>=hborder && my>=vborder && mx<(v_screen_w+hborder) && my<(v_screen_h+vborder)) {
mouse_x = (mx-hborder) / (v_screen_w/screen_width);
mouse_y = (my-vborder) / (v_screen_h/screen_height);
}
if (lua_is_playing()) { if (lua_is_playing()) {
lua_call_update(); lua_call_update();
@@ -297,10 +313,11 @@ int main(int argc,char*argv[]) {
SDL_SetRenderDrawColor(mini_ren, (palette[current_border] >> 16)&0xff, (palette[current_border] >> 8)&0xff, palette[current_border]&0xff, 0); SDL_SetRenderDrawColor(mini_ren, (palette[current_border] >> 16)&0xff, (palette[current_border] >> 8)&0xff, palette[current_border]&0xff, 0);
//SDL_SetRenderDrawColor(mini_ren, 255, 0, 0, 0); //SDL_SetRenderDrawColor(mini_ren, 255, 0, 0, 0);
SDL_RenderClear(mini_ren); SDL_RenderClear(mini_ren);
SDL_Rect rect = {40, 40, 640, 480}; SDL_Rect rect = {hborder, vborder, v_screen_w, v_screen_h};
SDL_RenderCopy(mini_ren, mini_bak, NULL, &rect); SDL_RenderCopy(mini_ren, mini_bak, NULL, &rect);
//SDL_RenderCopy(mini_ren, mini_bak, NULL, NULL); //SDL_RenderCopy(mini_ren, mini_bak, NULL, NULL);
SDL_RenderPresent(mini_ren); SDL_RenderPresent(mini_ren);
counter++;
} }
lua_quit(); lua_quit();
SDL_Quit(); SDL_Quit();
@@ -628,8 +645,8 @@ void play(const char* str) {
audio_state = AUDIO_PLAY; audio_state = AUDIO_PLAY;
} }
void setmode(const uint8_t mode) { void mode(const uint8_t val) {
current_mode = mode; current_mode = val;
reinit(); reinit();
cls(); cls();
} }
@@ -669,3 +686,10 @@ const char* fromclipboard() {
return str_tmp; return str_tmp;
} }
uint32_t cnt() {
return counter;
}
void rst() {
counter = 0;
}

View File

@@ -128,6 +128,8 @@
#define COLOR_WHITE 15 #define COLOR_WHITE 15
void loop(); void loop();
void execute_run();
const char* get_filename(); const char* get_filename();
void cls(uint8_t value=32); void cls(uint8_t value=32);
@@ -195,7 +197,7 @@ void sound(float freq, uint32_t len);
void nosound(); void nosound();
void play(const char* str); void play(const char* str);
void setmode(const uint8_t mode); void mode(const uint8_t val);
void load(const char* str); void load(const char* str);
void fileout(const char* str, uint16_t addr, uint16_t size); void fileout(const char* str, uint16_t addr, uint16_t size);
@@ -203,3 +205,6 @@ void filein(const char* str, uint16_t addr, uint16_t size);
void toclipboard(const char* str); void toclipboard(const char* str);
const char* fromclipboard(); const char* fromclipboard();
uint32_t cnt();
void rst();

View File

@@ -1,5 +1,5 @@
function init() function init()
setmode(1) mode(1)
reset() reset()
end end

38
demos/defender.lua Normal file
View File

@@ -0,0 +1,38 @@
function init()
mode(1)
mountains = {}
local val = 2
for i=0,99 do
val = mid(1,val+rnd(3)-1,4)
mountains[i] = val
end
ox=0
end
function update()
cls()
draw_mountains()
if btn(KEY_RIGHT) then ox=(ox+1)%100 end
if btn(KEY_LEFT) then ox=(ox-1)%100 end
end
function draw_mountains()
color(COLOR_RED,COLOR_BLACK)
for i=0,39 do
local x=(ox+i)%100
for j=29-mountains[x],29 do
local chr="\143"
if j==29-mountains[x] then
if mountains[(x+1)%100] > mountains[x] then
print("\214",i,j-1) --chr="\214"
elseif mountains[(x+1)%100] < mountains[x] then
chr="\215"
end
end
print(chr,i,j)
end
end
end

53
demos/matrix.lua Normal file
View File

@@ -0,0 +1,53 @@
--[[
===============================
MATRIX CODE DEMO
by JailDoctor
===============================
--]]
function init()
mode(1) -- fiquem el mode multicolor de 40x30 caracters
color(0,0) -- tinta i fondo a negre
cls() -- borrem pantalla
-- La tabla 'col' conté, per a cada columna de la pantalla: la coordenada 'y' actual, la llargaria 's' i la espera 'w'
col = {}
for i=0,39 do
col[i] = {
y=rnd(30), -- la coordenada y valdrà entre 0 i 29, ningún misteri...
s=rnd(5)+2, -- la 's' valdrà entre 2 i 6. S'usarà tant per a definir la llargaria de la estela com per a especificar la velocitat
w=1 -- 'w' es un contador de espera: Fiquem 'w' a un valor, i a cada cicle se resta 1. Quan arribe a 0 updatem la columna i se reseteja 'w' al valor anterior.
}
end
-- Omplim la tabla 'ma' amb els caracters que volem usar
ma = {}
for i=176,191 do ma[1+#ma] = i end
for i=97,122 do ma[1+#ma] = i end
-- Omplim la pantalla de caracters aleatoris (dins de la selecció que havem deixa a la tabla 'ma').
-- No es veuen perque havem ficat el color de tinta i fondo a negre.
for i=0,1199 do poke(i,ma[rnd(#ma)+1]) end
end
function update()
for i=0,39 do
col[i].w=col[i].w-1
if col[i].w == 0 then
col[i].w = col[i].s
col[i].y=col[i].y+1
if (col[i].y >= 30) then col[i].y = 0 end
local pos = 1200+i+col[i].y*40
poke(pos,15) pos=dec(pos)
poke(pos,14) pos=dec(pos)
for i=0,col[i].s do poke(pos,10) pos=dec(pos) end
for i=0,col[i].s do poke(pos,2) pos=dec(pos) end
for i=0,max(1,col[i].s-3) do poke(pos,8) pos=dec(pos) end
poke(pos,0)
end
end
end
-- Aquesta funció resta 40 a la adreça (per a pujar una linea). Si detecta que se'n va fora per dalt (menys de 1200), suma 1200 per a que aparega per baix
function dec(value) return value-40 < 1200 and value+1160 or value-40 end

View File

@@ -1,5 +1,5 @@
function init() function init()
setmode(1) mode(1)
setchar(94, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00) setchar(94, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00)
piano = { "_____", "\143\143\143\154\154", " ", "\143\143\143\154\154", "^^^^^", "_____", piano = { "_____", "\143\143\143\154\154", " ", "\143\143\143\154\154", "^^^^^", "_____",
"\143\143\143\154\154", " ", "\143\143\143\154\154", " ", "\143\143\143\154\154", "^^^^^" } "\143\143\143\154\154", " ", "\143\143\143\154\154", " ", "\143\143\143\154\154", "^^^^^" }

184
demos/sokoban.lua Normal file
View File

@@ -0,0 +1,184 @@
FLOOR = 0
GOAL = 1
BOX = 2
PLAYER = 4
WALL = 7
function init()
mode(2)
setchar(127,0xee,0xee,0xee,0x00,0xbb,0xbb,0xbb,0x00)
setchar(16,0xff,0xc3,0xa5,0x99,0x99,0xa5,0xc3,0xff)
menu.init()
end
-- ====================================================================================================
-- MENU (game state)
-- ====================================================================================================
menu={
init = function()
rst()
update=menu.update
end,
update = function()
color(15,0) cls()
ink(1) print("\x20\x20\x20\x87\x81\x87\x85\x85\x85\x87\x85\x87\x85\x87\x85\x87\x84\x20\x20\x20",0,2)
ink(9) print("\x20\x20\x20\x83\x85\x85\x85\x8D\x81\x85\x85\x87\x84\x8D\x85\x85\x85\x20\x20\x20",0,3)
ink(11) print("\x20\x20\x20\x83\x81\x83\x81\x81\x81\x83\x81\x83\x81\x81\x81\x81\x81\x20\x20\x20",0,4)
ink(15*flr((cnt()%60)/30)) print("PRESS SPACE TO PLAY",1,9)
if btnp(KEY_SPACE) then
level.start()
game.init()
end
end
}
-- ====================================================================================================
-- GAME (game state)
-- ====================================================================================================
game={
init = function()
update=game.update
level.load()
end,
update = function()
border(0)
color(1,0)
cls()
level.draw()
if btnp(KEY_UP) then level.try_move(0,-1)
elseif btnp(KEY_DOWN) then level.try_move(0,1)
elseif btnp(KEY_LEFT) then level.try_move(-1,0)
elseif btnp(KEY_RIGHT) then level.try_move(1,0)
end
end
}
-- ====================================================================================================
-- LOADING (game state)
-- ====================================================================================================
loading = {
init = function()
border(15)
update=loading.update
rst()
end,
update = function()
color(15,0)
cls()
print("LEVEL "..num_level,6,5)
print("GOOD JOB!",5,8)
if (cnt()%60)<15 then print("\248",9,10)
elseif (cnt()%60)<30 then print("\250",9,10)
elseif (cnt()%60)<45 then print("\248",9,10)
else print("\251",9,10) end
if cnt()==120 then
level.next()
game.init()
end
end
}
-- ====================================================================================================
-- LEVEL
-- ====================================================================================================
levels= {
"####|# .#|# ###|#*@ #|# $ #|# ###|####",
"######|# #|# #@ #|# $* #|# .* #|# #|######",
" ####|### ####|# $ #|# # #$ #|# . .#@ #|#########",
"########|# #|# .**$@#|# #|##### #| ####",
" #######| # #| # .$. #|## $@$ #|# .$. #|# #|########",
"###### #####|# ### #|# $$ #@#|# $ #... #|# ########|#####",
"#######|# .$. #|# $.$ #|# .$. #|# $.$ #|# @ #|#######",
"#####|#. ##|#@$$ #|## #| ## #| ##.#| ###"
}
level = {
start = function() num_level=1 end,
next = function() num_level=num_level+1 end,
load = function()
local str = levels[num_level]
player={}
map={}
local pos=0
local y,x=1,1
local maxx=0
map[y] = {}
while pos<#str do
local chr = ascii(str,pos)
if chr == 35 then map[y][x] = WALL end
if chr == 64 then player.x,player.y=x,y map[y][x] = FLOOR end
if chr == 43 then player.x,player.y=x,y map[y][x] = GOAL end
if chr == 36 then map[y][x] = BOX end
if chr == 42 then map[y][x] = GOAL+BOX end
if chr == 46 then map[y][x] = GOAL end
if chr == 32 then map[y][x] = FLOOR end
if chr == 124 then
y=y+1
map[y] = {}
if maxx < x then maxx=x end
x=0
end
pos=pos+1
x=x+1
end
ox = flr((20-maxx)/2)
oy = flr((15-y)/2)
end,
draw = function()
for y=1,#map do
for x=1,#map[y] do
if map[y][x] == WALL then color(4,0) print("\127",ox+x-1,oy+y-1)
elseif map[y][x] == BOX then color(8,7) print("\016",ox+x-1,oy+y-1)
elseif map[y][x] == BOX+GOAL then color(3,11) print("\016",ox+x-1,oy+y-1)
elseif map[y][x] == GOAL then color(3,0) print("\144",ox+x-1,oy+y-1)
end
end
end
color(15,0) print("\248",ox+player.x-1,oy+player.y-1)
end,
try_move = function(x,y)
if level.is_empty(x,y) then
level.move_player(x,y)
elseif level.is_box(x,y) and level.is_empty(x*2,y*2) then
level.move_box(x,y)
end
end,
is_empty = function(x,y)
return map[player.y+y][player.x+x] < 2
end,
is_box = function(x,y)
return map[player.y+y][player.x+x] < 4
end,
move_player = function(x,y)
player.x=player.x+x
player.y=player.y+y
end,
move_box = function(x,y)
map[player.y+y*2][player.x+x*2] = map[player.y+y*2][player.x+x*2] + BOX
map[player.y+y][player.x+x] = map[player.y+y][player.x+x] - BOX
level.move_player(x,y)
level.check_finished()
end,
check_finished = function()
for y=1,#map do
for x=1,#map[y] do
if map[y][x] == GOAL then return end
end
end
loading.init()
end
}

View File

@@ -111,7 +111,7 @@ void print(int x, int y, const char* text, Uint8 color) {
--]] --]]
function init() function init()
setmode(1) mode(1)
current_piece = starting[rnd(7)+1] current_piece = starting[rnd(7)+1]
next_piece = starting[rnd(7)+1] next_piece = starting[rnd(7)+1]

26
lua.cpp
View File

@@ -348,9 +348,9 @@ extern "C" {
return 0; return 0;
} }
static int cpp_setmode(lua_State *L) { static int cpp_mode(lua_State *L) {
int val = luaL_checkinteger(L, 1); int val = luaL_checkinteger(L, 1);
setmode(mid(0,val,3)); mode(mid(0,val,3));
return 0; return 0;
} }
@@ -392,6 +392,16 @@ extern "C" {
lua_pushstring(L, fromclipboard()); lua_pushstring(L, fromclipboard());
return 1; return 1;
} }
static int cpp_cnt(lua_State *L) {
lua_pushinteger(L, cnt());
return 1;
}
static int cpp_rst(lua_State *L) {
rst();
return 0;
}
} }
#define STATE_STOPPED 0 #define STATE_STOPPED 0
@@ -407,7 +417,7 @@ bool lua_is_playing() {
return lua_state == STATE_PLAYING; return lua_state == STATE_PLAYING;
} }
const char boot[] = "function init()setmode(1)cls()play('o5l0v5cegv4cegv3cegv2cegv1ceg')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(7)print('mini',9,8)ink(8)print('v0.5.4',34,29)w=0 end function update()w=w+1 if w>90 then cls()load()end end"; const char boot[] = "function init()mode(1)cls()play('o5l0v5cegv4cegv3cegv2cegv1ceg')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(7)print('mini',9,8)ink(8)print('v0.5.4',34,29)w=0 end function update()w=w+1 if w>90 then cls()load()end end";
void lua_init(const char* filename, const bool start_playing) { void lua_init(const char* filename, const bool start_playing) {
if (lua_state != STATE_STOPPED) lua_quit(); if (lua_state != STATE_STOPPED) lua_quit();
@@ -420,7 +430,6 @@ void lua_init(const char* filename, const bool start_playing) {
debug("BOOT ERROR:", false); debug("BOOT ERROR:", false);
debug(lua_tostring(L, -1)); debug(lua_tostring(L, -1));
lua_pop(L,1); lua_pop(L,1);
setmode(0);
file_loaded = false; file_loaded = false;
} }
} else { } else {
@@ -428,7 +437,6 @@ void lua_init(const char* filename, const bool start_playing) {
debug("ERROR LOADING GAME:",false); debug("ERROR LOADING GAME:",false);
debug(lua_tostring(L, -1)); debug(lua_tostring(L, -1));
lua_pop(L,1); lua_pop(L,1);
setmode(0);
file_loaded = false; file_loaded = false;
} }
} }
@@ -478,7 +486,7 @@ void lua_init(const char* filename, const bool start_playing) {
lua_pushcfunction(L,cpp_sound); lua_setglobal(L, "sound"); lua_pushcfunction(L,cpp_sound); lua_setglobal(L, "sound");
lua_pushcfunction(L,cpp_nosound); lua_setglobal(L, "nosound"); lua_pushcfunction(L,cpp_nosound); lua_setglobal(L, "nosound");
lua_pushcfunction(L,cpp_play); lua_setglobal(L, "play"); lua_pushcfunction(L,cpp_play); lua_setglobal(L, "play");
lua_pushcfunction(L,cpp_setmode); lua_setglobal(L, "setmode"); lua_pushcfunction(L,cpp_mode); lua_setglobal(L, "mode");
lua_pushcfunction(L,cpp_load); lua_setglobal(L, "load"); lua_pushcfunction(L,cpp_load); lua_setglobal(L, "load");
lua_pushcfunction(L,cpp_log); lua_setglobal(L, "log"); lua_pushcfunction(L,cpp_log); lua_setglobal(L, "log");
@@ -487,6 +495,9 @@ void lua_init(const char* filename, const bool start_playing) {
lua_pushcfunction(L,cpp_toclipboard); lua_setglobal(L, "toclipboard"); lua_pushcfunction(L,cpp_toclipboard); lua_setglobal(L, "toclipboard");
lua_pushcfunction(L,cpp_fromclipboard); lua_setglobal(L, "fromclipboard"); lua_pushcfunction(L,cpp_fromclipboard); lua_setglobal(L, "fromclipboard");
lua_pushcfunction(L,cpp_cnt); lua_setglobal(L, "cnt");
lua_pushcfunction(L,cpp_rst); lua_setglobal(L, "rst");
lua_pushinteger(L, 0); lua_setglobal(L, "KEY_UNKNOWN"); lua_pushinteger(L, 0); lua_setglobal(L, "KEY_UNKNOWN");
lua_pushinteger(L, 4); lua_setglobal(L, "KEY_A"); lua_pushinteger(L, 4); lua_setglobal(L, "KEY_A");
lua_pushinteger(L, 5); lua_setglobal(L, "KEY_B"); lua_pushinteger(L, 5); lua_setglobal(L, "KEY_B");
@@ -618,7 +629,6 @@ void lua_init(const char* filename, const bool start_playing) {
debug("RUNTIME ERROR:", false); debug("RUNTIME ERROR:", false);
debug(lua_tostring(L, -1)); debug(lua_tostring(L, -1));
lua_pop(L,1); lua_pop(L,1);
setmode(0);
return; return;
} }
@@ -663,7 +673,6 @@ void lua_call_init() {
debug("RUNTIME ERROR:",false); debug("RUNTIME ERROR:",false);
debug(lua_tostring(L, -1)); debug(lua_tostring(L, -1));
lua_pop(L,1); lua_pop(L,1);
setmode(0);
lua_state = STATE_STOPPED; lua_state = STATE_STOPPED;
} }
} }
@@ -676,7 +685,6 @@ void lua_call_update() {
debug("RUNTIME ERROR:",false); debug("RUNTIME ERROR:",false);
debug(lua_tostring(L, -1)); debug(lua_tostring(L, -1));
lua_pop(L,1); lua_pop(L,1);
setmode(0);
lua_state = STATE_STOPPED; lua_state = STATE_STOPPED;
} }
} else { } else {

201
main.cpp
View File

@@ -3,36 +3,15 @@
#include <string> #include <string>
#include <list> #include <list>
std::list<std::string> code;
int current_editor = 0;
void init_terminal(); void init_terminal();
void do_terminal(); void do_terminal();
void init_code_editor();
void do_code_editor();
void loop() { void loop() {
if (btnp(KEY_TAB)) { do_terminal();
current_editor = (++current_editor)%2; }
switch(current_editor) {
case 0: void execute_run() {
init_terminal(); //if (current_editor == 1) save_code();
break;
case 1:
init_code_editor();
break;
}
}
switch(current_editor) {
case 0:
do_terminal();
break;
case 1:
do_code_editor();
break;
}
} }
uint8_t get_char(uint8_t key) { uint8_t get_char(uint8_t key) {
@@ -74,7 +53,7 @@ uint8_t get_char(uint8_t key) {
} }
void init_terminal() { void init_terminal() {
setmode(0); mode(0);
cls(); cls();
} }
@@ -92,171 +71,3 @@ void do_terminal() {
} }
} }
} }
void load_code() {
FILE *f = fopen(get_filename(), "rb");
fseek(f, 0, SEEK_END);
long fsize = ftell(f);
fseek(f, 0, SEEK_SET); /* same as rewind(f); */
char *buffer = (char*)malloc(fsize + 1);
fread(buffer, 1, fsize, f);
fclose(f);
buffer[fsize] = 0;
int start = 0;
for (int pos=0;pos<fsize;++pos) {
if (buffer[pos] == '\r') {
buffer[pos]='\0';
code.push_back(&buffer[start]);
start=pos+2;
}
}
free(buffer);
}
std::list<std::string>::iterator ls[28];
static int col = 0;
static int line = 0;
bool cursor_inverted = false;
int blink_wait = 30;
void refresh_code_editor() {
color(COLOR_WHITE, COLOR_BLUE);
cls();
color(COLOR_BLACK, COLOR_LIGHT_GRAY);
print(" File Edit Run ",0,0);
print(" ",0,29);
color(COLOR_WHITE, COLOR_BLUE);
std::list<std::string>::iterator it = code.begin();
int i=0;
while (it != code.end() && i<28) {
ls[i] = it;
print((*it).c_str(), 0, i+1);
++i; ++it;
}
}
void init_code_editor() {
setmode(3);
load_code();
refresh_code_editor();
}
std::string get_current_line() {
return *ls[line];
}
void invert_cursor() {
cursor_inverted = !cursor_inverted;
uint16_t pos = 0x1200 + col + (line+1)*80;
uint8_t col = peek(pos);
poke(pos, ((col >> 4) & 0x0f) + ((col << 4) & 0xf0) );
}
void move_cursor_up() {
if (line > 0) {
if (cursor_inverted) invert_cursor();
line--;
if (get_current_line().size() < col) col = get_current_line().size();
blink_wait=1;
}
}
void move_cursor_down() {
if (line < code.size()-1) {
if (cursor_inverted) invert_cursor();
line++;
if (get_current_line().size() < col) col = get_current_line().size();
blink_wait=1;
}
}
void move_cursor_right() {
if ( col < get_current_line().size() ) {
if (cursor_inverted) invert_cursor();
col++;
blink_wait=1;
} else {
if (line < code.size()-1) {
col = 0;
move_cursor_down();
}
}
}
void move_cursor_left() {
if (col > 0) {
if (cursor_inverted) invert_cursor();
col--;
blink_wait=1;
} else {
if (line > 0) {
move_cursor_up();
col = get_current_line().size();
}
}
}
void split_line() {
std::string str = get_current_line();
*ls[line] = str.substr(col);
std::list<std::string>::iterator newline = ls[line]++;
code.insert(newline, str.substr(0, col));
if (cursor_inverted) invert_cursor();
line++; col=0;
blink_wait=1;
refresh_code_editor();
}
void join_lines() {
if (cursor_inverted) invert_cursor();
std::string str = get_current_line();
//std::list<std::string>::iterator prevline = ls[line]--;
code.erase(ls[line]);
line--;
const int line_size = (*ls[line]).size();
*ls[line] += str;
col = line_size;
blink_wait=1;
refresh_code_editor();
}
void delete_char() {
if (col == 0) {
if (line > 0) join_lines();
} else {
if (cursor_inverted) invert_cursor();
std::string str = get_current_line();
std::string a = str.substr(0,col-1);
std::string b = str.substr(col);
*ls[line] = a+b;
col--;
blink_wait=1;
refresh_code_editor();
}
}
void do_code_editor() {
const uint8_t key = whichbtn();
if (key != KEY_UNKNOWN) {
if (key == KEY_RETURN or key == KEY_KP_ENTER) {
split_line();
}
else if (key == KEY_UP) move_cursor_up();
else if (key == KEY_DOWN) move_cursor_down();
else if (key == KEY_LEFT) move_cursor_left();
else if (key == KEY_RIGHT) move_cursor_right();
else if (key == KEY_BACKSPACE) delete_char();
else {
uint8_t chr = get_char(key);
if (chr != 0) debugchr(chr);
}
}
blink_wait--;
if (blink_wait == 0) {
blink_wait = 30;
invert_cursor();
}
}

65
tools/fontedit.lua Normal file
View File

@@ -0,0 +1,65 @@
function init()
mode(1)
sel = 127
end
clicked=false
mx,my=0,0
function update()
color(0,7)
cls()
color(8,0)
for y=0,15 do
for x=0,15 do
print(chr(x+y*16),x+1,y+1)
end
end
poke(1200+((sel%16)+1+(flr(sel/16)+1)*40),15)
drawchar(sel,19,1)
if mousebutton(1) then
if mousex()>=1 and mousey()>=1 and mousex()<=16 and mousey()<=16 then
sel = mousex()-1+(mousey()-1)*16
elseif mousex()>=19 and mousey()>=1 and mousex()<=26 and mousey()<=8 then
if not clicked or mousex()~=mx or mousey()~=my then
clicked,mx,my=true,mousex(),mousey()
local pos = 2560+sel*8+(mousey()-1)
local bit = 1 << (7-(mousex()-19))
local value = peek(pos)
poke(pos, value ~ bit)
end
end
else
clicked = false
end
if btnp(KEY_S) then
local pos = 2560+sel*8
local string = "setchar("..sel..","..peek(pos)..","..peek(pos+1)..","..peek(pos+2)..","..peek(pos+3)..","..peek(pos+4)..","..peek(pos+5)..","..peek(pos+6)..","..peek(pos+7)..")"
toclipboard(string)
end
ink(4)
print("\143",mousex(), mousey())
end
function drawchar(char,x,y)
color(15,0)
local pos = 2560+char*8
for i=1,8 do
local val = peek(pos)
print(" ",x,y)
if val&1==1 then print("\143",x+7,y) end
if val&2==2 then print("\143",x+6,y) end
if val&4==4 then print("\143",x+5,y) end
if val&8==8 then print("\143",x+4,y) end
if val&16==16 then print("\143",x+3,y) end
if val&32==32 then print("\143",x+2,y) end
if val&64==64 then print("\143",x+1,y) end
if val&128==128 then print("\143",x,y) end
y=y+1
pos=pos+1
end
end

View File

@@ -1,13 +1,11 @@
ind = 22
function init() function init()
setmode(2) mode(2)
map = {} map = {}
col = {} col = {}
for i=0,299 do for i=0,299 do
map[i] = 32 map[i] = 32
col[i] = 0x0f col[i] = 0x0f
end end
map[ind] = 65
sel_col = 0x0f sel_col = 0x0f
sel_chr = 65 sel_chr = 65
end end
@@ -76,10 +74,17 @@ function draw_map()
map[mx+my*20] = sel_chr map[mx+my*20] = sel_chr
col[mx+my*20] = sel_col col[mx+my*20] = sel_col
end end
if mousebutton(3) then
col[mx+my*20] = sel_col
end
if btn(KEY_SPACE) then if btn(KEY_SPACE) then
sel_chr = peek(mx+my*20) sel_chr = peek(mx+my*20)
sel_col = peek(300+mx+my*20) sel_col = peek(300+mx+my*20)
end end
if btnp(KEY_UP) then for i=20,299 do map[i-20]=map[i]col[i-20]=col[i] end end
if btnp(KEY_DOWN) then for i=279,0,-1 do map[i+20]=map[i]col[i+20]=col[i] end end
if btnp(KEY_LEFT) then for i=0,298 do map[i]=map[i+1]col[i]=col[i+1] end end
if btnp(KEY_RIGHT) then for i=299,1,-1 do map[i]=map[i-1]col[i]=col[i-1] end end
blink = blink - 1 blink = blink - 1
if blink < 30 then if blink < 30 then
@@ -88,4 +93,15 @@ function draw_map()
local c = peek(300+(mx+my*20)) local c = peek(300+(mx+my*20))
poke(300+(mx+my*20), c~0xff) poke(300+(mx+my*20), c~0xff)
end end
if btn(KEY_S) then
fileout("map.bin", 0, 600);
elseif btn(KEY_L) then
filein("map.bin", 0, 600);
for i=0,299 do
map[i] = peek(i)
col[i] = peek(300+i)
end
end
end end

View File

@@ -1,5 +1,5 @@
function init() function init()
setmode(1) mode(1)
setchar(94, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00) setchar(94, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00)
piano = { "_____", "\143\143\143\154\154", " ", "\143\143\143\154\154", "^^^^^", "_____", piano = { "_____", "\143\143\143\154\154", " ", "\143\143\143\154\154", "^^^^^", "_____",
"\143\143\143\154\154", " ", "\143\143\143\154\154", " ", "\143\143\143\154\154", "^^^^^" } "\143\143\143\154\154", " ", "\143\143\143\154\154", " ", "\143\143\143\154\154", "^^^^^" }

View File

@@ -1,6 +1,6 @@
ind = 22 ind = 22
function init() function init()
setmode(1) mode(1)
map = {} map = {}
col = {} col = {}
for i=0,1199 do for i=0,1199 do
@@ -85,6 +85,11 @@ function draw_map()
sel_col = peek(1200+mx+my*40) sel_col = peek(1200+mx+my*40)
end end
if btnp(KEY_UP) then for i=40,1199 do map[i-40]=map[i]col[i-40]=col[i] end end
if btnp(KEY_DOWN) then for i=1159,0,-1 do map[i+40]=map[i]col[i+40]=col[i] end end
if btnp(KEY_LEFT) then for i=0,1198 do map[i]=map[i+1]col[i]=col[i+1] end end
if btnp(KEY_RIGHT) then for i=1199,1,-1 do map[i]=map[i-1]col[i]=col[i-1] end end
blink = blink - 1 blink = blink - 1
if blink < 30 then if blink < 30 then
if blink==0 then blink = 60 end if blink==0 then blink = 60 end

84
tools/spritedit.lua Normal file
View File

@@ -0,0 +1,84 @@
function init()
ink_color = 15
paper_color = 0
setchar(0,0xff,0x81,0x81,0x81,0x81,0x81,0x81,0xff)
sprite={0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03}
end
function update()
color(15,1)
cls()
color(ink_color,paper_color)
drawbigspr(sprite,1,1)
drawspr(sprite,65)
print("AB",18,1) print("CD",18,2)
for i=0,15 do
color(0,i)
poke(721+i,0) poke(1921+i,i<<4)
poke(761+i,0) poke(1961+i,i<<4)
end
poke(1921+ink_color, peek(1921+ink_color)+4)
poke(1961+paper_color, peek(1961+paper_color)+4)
if mousebutton(1) and mousex()>=1 and mousey()>=18 and mousex()<=16 and mousey()<=19 then
if mousey()==18 then
ink_color = mousex()-1
else
paper_color = mousex()-1
end
end
handlesprmouse()
end
mousepressed = false
mousepos = {x=-1,y=-1}
function handlesprmouse()
local mx,my=mousex(),mousey()
if mx>=1 and my>=1 and mx<=16 and my<=16 then
if mousebutton(1) then
if (not mousepressed or (mousepos.x ~= mx or mousepos.y ~= my)) then
mousepressed = true mousepos.x = mx mousepos.y = my
local quadrant = flr((mx-1)/8)+flr((my-1)/8)*2
local x=(mx-1)%8
local y=(my-1)%8
local val = sprite[quadrant*8+y+1]
val = val ~ (1<<(7-x))
sprite[quadrant*8+y+1] = val
end
else
mousepressed = false
end
end
end
function drawbigspr(spr,x,y)
drawbigsprchr(spr,0,x,y)
drawbigsprchr(spr,1,x+8,y)
drawbigsprchr(spr,2,x,y+8)
drawbigsprchr(spr,3,x+8,y+8)
end
function drawbigsprchr(spr,chr,x,y)
local pos = 1+chr*8
for i=1,8 do
local val = spr[pos]
print(" ",x,y)
if val&1==1 then print("\143",x+7,y) end
if val&2==2 then print("\143",x+6,y) end
if val&4==4 then print("\143",x+5,y) end
if val&8==8 then print("\143",x+4,y) end
if val&16==16 then print("\143",x+3,y) end
if val&32==32 then print("\143",x+2,y) end
if val&64==64 then print("\143",x+1,y) end
if val&128==128 then print("\143",x,y) end
y=y+1
pos=pos+1
end
end
function drawspr(spr,chr)
for i=0,31 do poke(i+2560+chr*8,spr[i+1]) end
end

11
tweetcarts/matrix.lua Normal file
View File

@@ -0,0 +1,11 @@
q=1200
h={}for i=0,q do poke(i,rnd(99))end
function t(a)poke(q+p,a)p=(p-40)%q end
function update()srand(1)for i=0,39 do
h[i]=(h[i] or rnd(30))+(1/(rnd(5)+2))p=i+(flr(h[i])%30)*40
t(15)t(14)
for j=0,5 do t(10)end
for j=0,5 do t(2)end
t(8)t(0)
end
end