From 787d78c070891e08dd48c506b431dc6e129bd743 Mon Sep 17 00:00:00 2001 From: Raimon Zamora Date: Sat, 16 May 2026 08:01:27 +0200 Subject: [PATCH] - [NEW] Passat a SDL3 --- ascii.cpp | 218 +++++++++++++++++++++++++++++--------------------- ascii.h | 20 ++--- lagueirtofile | 2 +- lua.cpp | 2 + main.cpp | 45 ++++++----- 5 files changed, 164 insertions(+), 123 deletions(-) diff --git a/ascii.cpp b/ascii.cpp index 352ee37..ba44272 100644 --- a/ascii.cpp +++ b/ascii.cpp @@ -7,12 +7,25 @@ #include #include +#include #define swap(a, b) {auto tmp=a;a=b;b=tmp;} #define AUDIO_NONE 0 #define AUDIO_SOUND 1 #define AUDIO_PLAY 2 +uint8_t audio_state = AUDIO_NONE; +int audio_freq = 0; +uint32_t audio_len = 0; + +SDL_AudioDeviceID mini_audio_device; +SDL_AudioStream* audio_stream; +uint8_t mix_buffer[4096]; // tamaño configurable + +int play_len = 0; +uint8_t* play_pos = nullptr; +uint8_t play_buffer[2048]; + char lua_filename[1024]; char window_title[256]; @@ -30,9 +43,6 @@ uint8_t current_border = 0; uint8_t current_mode = 1; uint8_t cursor_x = 0; uint8_t cursor_y = 0; -uint8_t audio_state = AUDIO_NONE; -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] @@ -43,20 +53,19 @@ uint32_t audio_len = 0; SDL_Window *mini_win; SDL_Renderer *mini_ren; SDL_Texture *mini_bak = NULL; -SDL_AudioDeviceID mini_audio_device; Uint32 *pixels; int pitch; Uint32 counter=0; -uint32_t palette[16] = { 0x00000000, 0x000000AA, 0x0000AA00, 0x0000AAAA, 0x00AA0000, 0x00AA00AA, 0x00AA5500, 0x00AAAAAA, - 0x00555555, 0x005555FF, 0x0055FF55, 0x0055FFFF, 0x00FF5555, 0x00FF55FF, 0x00FFFF55, 0x00FFFFFF }; +uint32_t palette[16] = { 0xFF000000, 0xFF0000AA, 0xFF00AA00, 0xFF00AAAA, 0xFFAA0000, 0xFFAA00AA, 0xFFAA5500, 0xFFAAAAAA, + 0xFF555555, 0xFF5555FF, 0xFF55FF55, 0xFF55FFFF, 0xFFFF5555, 0xFFFF55FF, 0xFFFFFF55, 0xFFFFFFFF }; int debug_prompt = -1; int debug_cursor_blink = 30; std::vector cmd_list; bool should_reset = false; -const Uint8 *keys; +const bool *keys; Uint8 key_just_pressed = 0; int mouse_x, mouse_y, mouse_wheel; @@ -137,47 +146,72 @@ void reinit() { mini_bak = SDL_CreateTexture(mini_ren, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, 640, 240); break; } + SDL_SetTextureScaleMode(mini_bak, SDL_SCALEMODE_NEAREST); } -static Uint8* play_pos; -static Uint32 play_len; -static Uint8 play_buffer[132300]; +void pump_audio() { + if (audio_state == AUDIO_NONE) return; -void audioCallback(void * userdata, uint8_t * stream, int len) { - if (audio_state == AUDIO_SOUND) { - if (audio_len <= 0) audio_state = AUDIO_NONE; - 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=audio_freq) {period = 0; v=-v;} + const int TARGET_MS = 20; // latencia deseada + const int BYTES_PER_MS = 44; // 44100 Hz, 8-bit mono + const int TARGET_BYTES = TARGET_MS * BYTES_PER_MS; + + int queued = SDL_GetAudioStreamAvailable(audio_stream); + + // Si ya hay suficiente audio en cola, no generamos nada + if (queued >= TARGET_BYTES) + return; + + // Cuánto falta para llegar al objetivo + int missing = TARGET_BYTES - queued; + if (missing > sizeof(mix_buffer)) + missing = sizeof(mix_buffer); + + // Generar exactamente "missing" bytes + uint8_t* out = mix_buffer; + int remaining = missing; + + while (remaining > 0) { + + if (audio_state == AUDIO_SOUND) { + static int period = 0; + static int v = 16; + + for (int i = 0; i < remaining; ++i) { + out[i] = v; + period++; + if (period >= audio_freq) { + period = 0; + v = -v; + } + } + + remaining = 0; } - } else if (audio_state == AUDIO_PLAY) { - while( len > 0 ) { - while( play_len == 0 ) { - play_len = interpret_next_token(play_buffer); - if (play_len == -1) { audio_state=AUDIO_NONE; SDL_memset( stream, 0, len ); return; } - play_pos = play_buffer; - } - const int actual_len = ( len > play_len ? play_len : len ); - //SDL_memset( stream, 0, actual_len ); - //SDL_MixAudio( stream, audio_pos, actual_len, SDL_MIX_MAXVOLUME ); - SDL_memcpy(stream, play_pos, actual_len); - stream += actual_len; - play_pos += actual_len; - play_len -= actual_len; - len -= actual_len; - } - } else { - SDL_memset(stream, 0, len); + else if (audio_state == AUDIO_PLAY) { + + while (play_len == 0) { + play_len = interpret_next_token(play_buffer); + if (play_len == -1) { + audio_state = AUDIO_NONE; + return; + } + play_pos = play_buffer; + } + + int n = (remaining < play_len ? remaining : play_len); + SDL_memcpy(out, play_pos, n); + + out += n; + play_pos += n; + play_len -= n; + remaining -= n; + } } -} -uint8_t old_mode = 0; + SDL_PutAudioStreamData(audio_stream, mix_buffer, missing); +} void romcpy() { SDL_memcpy(&mem[2560], rom, rom_size); @@ -191,18 +225,23 @@ int main(int argc,char*argv[]) { //[TODEL]for (int i=0; i=hborder && my>=vborder && mx<(v_screen_w+hborder) && my<(v_screen_h+vborder)) { mouse_x = (mx-hborder) / (v_screen_w/screen_width); @@ -340,8 +378,8 @@ 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, 255, 0, 0, 0); SDL_RenderClear(mini_ren); - SDL_Rect rect = {hborder, vborder, v_screen_w, v_screen_h}; - SDL_RenderCopy(mini_ren, mini_bak, NULL, &rect); + SDL_FRect rect = {(float)hborder, (float)vborder, (float)v_screen_w, (float)v_screen_h}; + SDL_RenderTexture(mini_ren, mini_bak, NULL, &rect); //SDL_RenderCopy(mini_ren, mini_bak, NULL, NULL); SDL_RenderPresent(mini_ren); counter++; @@ -429,7 +467,7 @@ int mousewheel() { } bool mousebutton(uint8_t i) { - return mouse_buttons & SDL_BUTTON(i); + return mouse_buttons & SDL_BUTTON_MASK(i); } int time() { @@ -448,31 +486,31 @@ float sgn(float x) { return x >= 0 ? 1 : -1; } -#ifndef __LINUX__ -#ifndef __APPLE__ - -float ceil(float x) { - return SDL_ceilf(x); -} - -float sin(float x) { - return SDL_sinf(x); -} - -float cos(float x) { - return SDL_cosf(x); -} - -float atan2(float dx, float dy) { - return SDL_atan2f(dx, dy); -} - -float sqrt(float x) { - return SDL_sqrtf(x); -} - -#endif -#endif +//#ifndef __LINUX__ +//#ifndef __APPLE__ +// +//float ceil(float x) { +// return SDL_ceilf(x); +//} +// +//float sin(float x) { +// return SDL_sinf(x); +//} +// +//float cos(float x) { +// return SDL_cosf(x); +//} +// +//float atan2(float dx, float dy) { +// return SDL_atan2f(dx, dy); +//} +// +//float sqrt(float x) { +// return SDL_sqrtf(x); +//} +// +//#endif +//#endif float max(float x, float y) { return SDL_max(x, y); diff --git a/ascii.h b/ascii.h index 7539a4f..320a0f8 100644 --- a/ascii.h +++ b/ascii.h @@ -1,6 +1,6 @@ #pragma once -#include +#include #define KEY_UNKNOWN 0 #define KEY_A 4 @@ -158,15 +158,15 @@ int time(); 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 +//#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); diff --git a/lagueirtofile b/lagueirtofile index c93af87..2878225 100644 --- a/lagueirtofile +++ b/lagueirtofile @@ -1,5 +1,5 @@ [linux_debug] default -libs = -lSDL2 +libs = -lSDL3 cppflags = -D DEBUG -g executable = ascii sourcepath = . lua diff --git a/lua.cpp b/lua.cpp index 76de67c..db02521 100644 --- a/lua.cpp +++ b/lua.cpp @@ -1,6 +1,8 @@ #include "lua.h" #include "lua/lua.hpp" #include "ascii.h" +#include +#include void reverse(char* str, int len) { int i = 0, j = len - 1, temp; diff --git a/main.cpp b/main.cpp index dd73566..ac12501 100644 --- a/main.cpp +++ b/main.cpp @@ -2,6 +2,7 @@ #include #include #include +#include std::list code; @@ -44,34 +45,34 @@ uint8_t get_char(uint8_t key) { SDL_Keymod mods = SDL_GetModState(); if (key != KEY_UNKNOWN) { if (key < 30) { - if ((mods & KMOD_SHIFT) || (mods & KMOD_CAPS)) { + if ((mods & SDL_KMOD_SHIFT) || (mods & SDL_KMOD_CAPS)) { return key+61; } else { return key+93; } } - else if (key == KEY_0) { if (mods & KMOD_SHIFT) { return '='; } else { return '0'; } } - else if (key == KEY_1) { if (mods & KMOD_SHIFT) { return '!'; } else if (mods & KMOD_RALT) { return '|'; } else { return '1'; } } - else if (key == KEY_2) { if (mods & KMOD_SHIFT) { return '"'; } else if (mods & KMOD_RALT) { return '@'; } else { return '2'; } } - else if (key == KEY_3) { if (mods & KMOD_SHIFT) { return 144; } else if (mods & KMOD_RALT) { return '#'; } else { return '3'; } } - else if (key == KEY_4) { if (mods & KMOD_SHIFT) { return '$'; } else if (mods & KMOD_RALT) { return '~'; } else { return '4'; } } - else if (key == KEY_5) { if (mods & KMOD_SHIFT) { return '%'; } else if (mods & KMOD_RALT) { return 180; } else { return '5'; } } - else if (key == KEY_6) { if (mods & KMOD_SHIFT) { return '&'; } else if (mods & KMOD_RALT) { return 173; } else { return '6'; } } - else if (key ==KEY_7) { if (mods & KMOD_SHIFT) { return '/'; } else { return '7'; } } - else if (key == KEY_8) { if (mods & KMOD_SHIFT) { return '('; } else { return '8'; } } - else if (key == KEY_9) { if (mods & KMOD_SHIFT) { return ')'; } else { return '9'; } } + else if (key == KEY_0) { if (mods & SDL_KMOD_SHIFT) { return '='; } else { return '0'; } } + else if (key == KEY_1) { if (mods & SDL_KMOD_SHIFT) { return '!'; } else if (mods & SDL_KMOD_RALT) { return '|'; } else { return '1'; } } + else if (key == KEY_2) { if (mods & SDL_KMOD_SHIFT) { return '"'; } else if (mods & SDL_KMOD_RALT) { return '@'; } else { return '2'; } } + else if (key == KEY_3) { if (mods & SDL_KMOD_SHIFT) { return 144; } else if (mods & SDL_KMOD_RALT) { return '#'; } else { return '3'; } } + else if (key == KEY_4) { if (mods & SDL_KMOD_SHIFT) { return '$'; } else if (mods & SDL_KMOD_RALT) { return '~'; } else { return '4'; } } + else if (key == KEY_5) { if (mods & SDL_KMOD_SHIFT) { return '%'; } else if (mods & SDL_KMOD_RALT) { return 180; } else { return '5'; } } + else if (key == KEY_6) { if (mods & SDL_KMOD_SHIFT) { return '&'; } else if (mods & SDL_KMOD_RALT) { return 173; } else { return '6'; } } + else if (key == KEY_7) { if (mods & SDL_KMOD_SHIFT) { return '/'; } else { return '7'; } } + else if (key == KEY_8) { if (mods & SDL_KMOD_SHIFT) { return '('; } else { return '8'; } } + else if (key == KEY_9) { if (mods & SDL_KMOD_SHIFT) { return ')'; } else { return '9'; } } else if (key == KEY_SPACE) return 32; - else if (key == KEY_MINUS) { if (mods & KMOD_SHIFT) { return '?'; } else { return '\''; } } - else if (key == KEY_EQUALS) { if (mods & KMOD_SHIFT) { return 174; } else { return 175; } } - else if (key == KEY_COMMA) { if (mods & KMOD_SHIFT) { return ';'; } else { return ','; } } - else if (key == KEY_PERIOD) { if (mods & KMOD_SHIFT) { return ':'; } else { return '.'; } } - else if (key == KEY_SLASH) { if (mods & KMOD_SHIFT) { return '_'; } else { return '-'; } } - else if (key == KEY_LEFTBRACKET) { if (mods & KMOD_SHIFT) { return 160; } else if (mods & KMOD_RALT) { return '['; } else { return 96; } } - else if (key == KEY_RIGHTBRACKET) { if (mods & KMOD_SHIFT) { return '*'; } else if (mods & KMOD_RALT) { return ']'; } else { return '+'; } } - else if (key == KEY_APOSTROPHE) { if (mods & KMOD_SHIFT) { return 162; } else if (mods & KMOD_RALT) { return '{'; } else { return 161; } } - else if (key == KEY_BACKSLASH) { if (mods & KMOD_SHIFT) { return 'C'; } else if (mods & KMOD_RALT) { return '}'; } else { return 'c'; } } - else if (key == KEY_GRAVE) { if (mods & KMOD_SHIFT) { return '>'; } else { return '<'; } } - else if (key == KEY_NONUSBACKSLASH) { if (mods & KMOD_SHIFT) { return 164; } else if (mods & KMOD_RALT) { return '\\'; } else { return 163; } } + else if (key == KEY_MINUS) { if (mods & SDL_KMOD_SHIFT) { return '?'; } else { return '\''; } } + else if (key == KEY_EQUALS) { if (mods & SDL_KMOD_SHIFT) { return 174; } else { return 175; } } + else if (key == KEY_COMMA) { if (mods & SDL_KMOD_SHIFT) { return ';'; } else { return ','; } } + else if (key == KEY_PERIOD) { if (mods & SDL_KMOD_SHIFT) { return ':'; } else { return '.'; } } + else if (key == KEY_SLASH) { if (mods & SDL_KMOD_SHIFT) { return '_'; } else { return '-'; } } + else if (key == KEY_LEFTBRACKET) { if (mods & SDL_KMOD_SHIFT) { return 160; } else if (mods & SDL_KMOD_RALT) { return '['; } else { return 96; } } + else if (key == KEY_RIGHTBRACKET) { if (mods & SDL_KMOD_SHIFT) { return '*'; } else if (mods & SDL_KMOD_RALT) { return ']'; } else { return '+'; } } + else if (key == KEY_APOSTROPHE) { if (mods & SDL_KMOD_SHIFT) { return 162; } else if (mods & SDL_KMOD_RALT) { return '{'; } else { return 161; } } + else if (key == KEY_BACKSLASH) { if (mods & SDL_KMOD_SHIFT) { return 'C'; } else if (mods & SDL_KMOD_RALT) { return '}'; } else { return 'c'; } } + else if (key == KEY_GRAVE) { if (mods & SDL_KMOD_SHIFT) { return '>'; } else { return '<'; } } + else if (key == KEY_NONUSBACKSLASH) { if (mods & SDL_KMOD_SHIFT) { return 164; } else if (mods & SDL_KMOD_RALT) { return '\\'; } else { return 163; } } else return 0; } else { return 0;