- [NEW] jshader (shader fan cosa rara en fullscreen)

- [NEW] jfile convertit
- [NEW] jinput te en compte el ratio per a les coordenades en pantalla (falla en fullscreen, falta afegir offset)
- [NEW] F1 escala avall la finestra
- [NEW] F2 escala amunt la finestra
- [NEW] F3 togglecha la pantalla completa
- [NEW] F4 togglecha el shader
This commit is contained in:
2025-07-01 13:46:58 +02:00
parent 063016cf55
commit bc59b74f15
17 changed files with 759 additions and 230 deletions

View File

@@ -2,6 +2,7 @@
#include "SDL3/SDL.h"
#include "gif.h"
#include "jfile.h"
#include "jshader.h"
namespace draw
{
@@ -13,9 +14,13 @@ namespace draw
SDL_Window *sdl_window {nullptr}; // La finestra de SDL
SDL_Renderer *sdl_renderer {nullptr}; // El renderer de SDL
SDL_Texture *sdl_texture {nullptr}; // La textura de SDL a la que pintarem la nostra superficie "screen" i que despres volcarem a pantalla
SDL_Texture *sdl_shadertex {nullptr}; // La textura de SDL per al shader
static int screen_zoom = 1;
static bool screen_fullscreen = false;
static bool screen_cursor = true;
static char* screen_shader = nullptr;
static bool shader_enabled = false;
static float window_ratio = 1;
static int canvas_width;
static int canvas_height;
@@ -51,13 +56,17 @@ namespace draw
// Ajustem el tamany de la finestra, segons el zoom i el ratio
window_width = canvas_width*screen_zoom;
window_height = window_ratio != 1 ? canvas_width*window_ratio*screen_zoom : canvas_height*screen_zoom;
window_height = window_ratio != 1 ? int(float(canvas_width)*window_ratio*float(screen_zoom)) : canvas_height*screen_zoom;
// Mentres no càpiga en la pantalla, reduïm el zoom
while (window_width > desktop_width || window_height > desktop_height) {
screen_zoom--;
window_width = canvas_width*screen_zoom;
window_height = window_ratio != 1 ? canvas_width*window_ratio*screen_zoom : canvas_height*screen_zoom;
window_height = window_ratio != 1 ? int(float(canvas_width)*window_ratio*float(screen_zoom)) : canvas_height*screen_zoom;
}
if (screen_fullscreen) {
window_width = desktop_width;
window_height = desktop_height;
}
sdl_window = SDL_CreateWindow(window_title, window_width, window_height, SDL_WINDOW_OPENGL|(screen_fullscreen?SDL_WINDOW_FULLSCREEN:0));
@@ -66,6 +75,25 @@ namespace draw
exit(1);
}
sdl_renderer = SDL_CreateRenderer(sdl_window, NULL);
if (!sdl_renderer) {
SDL_LogError(SDL_LOG_CATEGORY_VIDEO, "ERROR (draw::init): Failed to initialize renderer!\n");
exit(1);
}
if (screen_cursor) SDL_ShowCursor(); else SDL_HideCursor();
sdl_texture = SDL_CreateTexture(sdl_renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, canvas_width, canvas_height);
SDL_SetTextureScaleMode(sdl_texture, SDL_SCALEMODE_NEAREST);
if (!sdl_texture) {
SDL_LogError(SDL_LOG_CATEGORY_VIDEO, "ERROR (draw::init): Failed to initialize texture!\n");
exit(1);
}
sdl_shadertex = SDL_CreateTexture(sdl_renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, window_width, window_height);
SDL_SetTextureScaleMode(sdl_shadertex, SDL_SCALEMODE_NEAREST);
loadShader();
}
void destroyDisplay()
@@ -93,7 +121,9 @@ namespace draw
desktop_width = dm->w;
desktop_height = dm->h;
createDisplay();
// Inicialització de les estructures de SDL
/*
sdl_window = SDL_CreateWindow(titol, width * zoom, height * zoom, 0);
if (!sdl_window) {
SDL_LogCritical(SDL_LOG_CATEGORY_VIDEO, "ERROR (draw::init): Failed to initialize window!\n");
@@ -110,6 +140,7 @@ namespace draw
SDL_LogError(SDL_LOG_CATEGORY_VIDEO, "ERROR (draw::init): Failed to initialize texture!\n");
exit(1);
}
*/
// Creem la superficie "screen" i la establim com a superficie destinació
screen = createSurface(width, height);
@@ -120,7 +151,7 @@ namespace draw
sel_color = transparent = 0;
for (int i=0;i<256;++i) color_indices[i] = i;
SDL_HideCursor();
//SDL_HideCursor();
//textsurf = loadSurface("font.gif");
}
@@ -136,9 +167,7 @@ namespace draw
}
// Destruim tot el relacionat amb SDL
SDL_DestroyTexture(sdl_texture);
SDL_DestroyRenderer(sdl_renderer);
SDL_DestroyWindow(sdl_window);
destroyDisplay();
// Fiquem tots els punters a nullptr, per si de cas no estem eixint del programa
// i anem a tornar a inicialitzar el sistema
@@ -148,11 +177,81 @@ namespace draw
screen = destination = source = nullptr;
}
void setZoom(const int value) {
screen_zoom = value;
destroyDisplay();
createDisplay();
char strzoom[3];
file::setConfigValue("zoom", SDL_itoa(screen_zoom, strzoom, 10));
}
const int getZoom()
{
return screen_zoom;
}
const float getScaleX()
{
return float(window_width) / float(canvas_width);
}
const float getScaleY()
{
return float(window_height) / float(canvas_height);
}
bool getFullscreen() {
return screen_fullscreen;
}
void setFullscreen(const bool value) {
screen_fullscreen=value;
destroyDisplay();
createDisplay();
file::setConfigValue("fullscreen", screen_fullscreen?"true":"false");
}
void loadShader()
{
char *buffer = nullptr;
if (screen_shader) {
int size;
buffer = file::getFileBuffer(screen_shader, size, true);
}
shader::setAspectRatio(3.0f/4.0f);
shader::init(sdl_window, sdl_shadertex, buffer);
if (buffer) free(buffer);
}
void setShader(const char* shader_file)
{
if (screen_shader) free(screen_shader);
screen_shader = (char*)malloc(strlen(shader_file)+1);
strcpy(screen_shader, shader_file);
loadShader();
}
void enableShader()
{
shader_enabled = true;
shader::enable();
//destroyDisplay();
//createDisplay();
}
void disableShader()
{
shader_enabled = false;
shader::disable();
//destroyDisplay();
//createDisplay();
}
void toggleShader()
{
shader_enabled ? disableShader() : enableShader();
}
// Crea una superficie i torna un punter a ella
surface *createSurface(const uint16_t w, const uint16_t h)
{
@@ -176,7 +275,7 @@ namespace draw
// Agafem un buffer de bytes de l'arxiu especificat
// getFileBuffer() simplement ens torna el arxiu sencer dins de un array de char
int size;
uint8_t *buffer = (uint8_t *)file_getfilebuffer(filename, size);
uint8_t *buffer = (uint8_t *)file::getFileBuffer(filename, size);
// Si ens ha tornat nullptr, es que no l'ha trobat, tornem nosaltres també nullptr ja que no s'ha pogut crear la superficie
if (buffer == nullptr)
@@ -292,7 +391,7 @@ namespace draw
// Agafem un buffer de bytes de l'arxiu especificat
// getFileBuffer() simplement ens torna el arxiu sencer dins de un array de char
int size;
uint8_t *buffer = (uint8_t *)file_getfilebuffer(filename, size);
uint8_t *buffer = (uint8_t *)file::getFileBuffer(filename, size);
// Li passem el array del arxiu a LoadPalette. Ell ens torna un array de uint32_t amb la paleta
// Van a ser 256 entrades de 32 bits, cada entrada es un color, amb el format 0xAARRGGBB
@@ -565,11 +664,13 @@ namespace draw
// Desbloquejem la textura
SDL_UnlockTexture(sdl_texture);
SDL_SetRenderTarget(sdl_renderer, sdl_shadertex);
// Pintem la textura a pantalla
SDL_RenderTexture(sdl_renderer, sdl_texture, NULL, NULL);
// I ho presentem
SDL_RenderPresent(sdl_renderer);
shader::render();
//SDL_RenderPresent(sdl_renderer);
}
}