- [NEW] Passat a SDL3

This commit is contained in:
2026-05-16 08:01:27 +02:00
parent 7d513a3d0b
commit 787d78c070
5 changed files with 164 additions and 123 deletions
+128 -90
View File
@@ -7,12 +7,25 @@
#include <vector>
#include <string>
#include <SDL3/SDL.h>
#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<std::string> 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<flen; ++i) {
stream[i] = v;
period++; if (period>=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<debug_total_size;++i) debug_text[i] = 32;
SDL_Init(49);
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_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_EVENTS);
mini_win = SDL_CreateWindow(window_title, 640+80, 480+80, 0);
mini_ren = SDL_CreateRenderer(mini_win, NULL);
SDL_SetRenderVSync(mini_ren, 1);
bool exit = false;
SDL_Event mini_eve;
SDL_AudioSpec audioSpec = {22050, 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);
SDL_AudioSpec spec = {};
spec.freq = 22050;
spec.format = SDL_AUDIO_S8;
spec.channels = 1;
mini_audio_device = SDL_OpenAudioDevice(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &spec);
audio_stream = SDL_CreateAudioStream(&spec, &spec);
SDL_BindAudioStream(mini_audio_device, audio_stream);
SDL_ResumeAudioDevice(mini_audio_device);
romcpy();
reinit();
@@ -210,7 +249,7 @@ int main(int argc,char*argv[]) {
lua_init(NULL);
lua_call_init();
SDL_EventState(SDL_DROPFILE, SDL_ENABLE);
SDL_SetEventEnabled(SDL_EVENT_DROP_FILE, true);
while(!exit) {
if (should_reset) {
@@ -220,18 +259,17 @@ int main(int argc,char*argv[]) {
lua_init(lua_filename);
lua_call_init();
}
pump_audio();
key_just_pressed = 0;
mouse_wheel = 0;
while(SDL_PollEvent(&mini_eve)) {
if (mini_eve.type == SDL_QUIT) { exit=true; break; }
if (mini_eve.type == SDL_DROPFILE) {
char* dropped_filedir = mini_eve.drop.file;
if (mini_eve.type == SDL_EVENT_QUIT) { exit=true; break; }
if (mini_eve.type == SDL_EVENT_DROP_FILE) {
const char* dropped_filedir = mini_eve.drop.data;
load(dropped_filedir);
//SDL_ShowSimpleMessageBox( SDL_MESSAGEBOX_INFORMATION, "File dropped on window", dropped_filedir, mini_win );
SDL_free(dropped_filedir);
}
if (mini_eve.type == SDL_KEYDOWN) {
if (mini_eve.key.keysym.scancode == SDL_SCANCODE_ESCAPE) {
if (mini_eve.type == SDL_EVENT_KEY_DOWN) {
if (mini_eve.key.scancode == SDL_SCANCODE_ESCAPE) {
if (lua_is_playing()) {
lua_pause();
//lua_quit();
@@ -246,22 +284,22 @@ int main(int argc,char*argv[]) {
//lua_init(lua_filename);
//lua_call_init();
}
} else if (mini_eve.key.keysym.scancode == SDL_SCANCODE_F5) {
} else if (mini_eve.key.scancode == SDL_SCANCODE_F5) {
execute_run();
lua_quit();
reinit();
lua_init(lua_filename);
lua_call_init();
} else {
key_just_pressed = mini_eve.key.keysym.scancode;
key_just_pressed = mini_eve.key.scancode;
}
}
if (mini_eve.type == SDL_MOUSEWHEEL) {
if (mini_eve.type == SDL_EVENT_MOUSE_WHEEL) {
mouse_wheel = mini_eve.wheel.y;
}
}
keys = SDL_GetKeyboardState(NULL);
int mx, my;
float mx, my;
mouse_buttons = SDL_GetMouseState(&mx, &my);
if (mx>=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);
+10 -10
View File
@@ -1,6 +1,6 @@
#pragma once
#include <SDL2/SDL.h>
#include <stdint.h>
#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);
+1 -1
View File
@@ -1,5 +1,5 @@
[linux_debug] default
libs = -lSDL2
libs = -lSDL3
cppflags = -D DEBUG -g
executable = ascii
sourcepath = . lua
+2
View File
@@ -1,6 +1,8 @@
#include "lua.h"
#include "lua/lua.hpp"
#include "ascii.h"
#include <SDL3/SDL.h>
#include <math.h>
void reverse(char* str, int len) {
int i = 0, j = len - 1, temp;
+23 -22
View File
@@ -2,6 +2,7 @@
#include <stdio.h>
#include <string>
#include <list>
#include <SDL3/SDL.h>
std::list<std::string> 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;