forked from jaildesigner-jailgames/jaildoctors_dilemma
Redistribuits els .cpp en carpetes
Actualitzat cmake Modificats els include de SDL2 a SDL3
This commit is contained in:
2
source/external/.clang-format
vendored
Normal file
2
source/external/.clang-format
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
DisableFormat: true
|
||||
SortIncludes: Never
|
||||
4
source/external/.clang-tidy
vendored
Normal file
4
source/external/.clang-tidy
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
# source/external/.clang-tidy
|
||||
Checks: '-*'
|
||||
WarningsAsErrors: ''
|
||||
HeaderFilterRegex: ''
|
||||
417
source/external/jail_audio.cpp
vendored
Normal file
417
source/external/jail_audio.cpp
vendored
Normal file
@@ -0,0 +1,417 @@
|
||||
#include "jail_audio.h"
|
||||
|
||||
#include <SDL3/SDL_rwops.h> // Para SDL_RWFromMem
|
||||
#include <SDL3/SDL_timer.h> // Para SDL_GetTicks
|
||||
#include <stdint.h> // Para uint8_t, uint32_t
|
||||
#include <stdio.h> // Para NULL, fseek, fclose, fopen, fread, ftell
|
||||
#include <stdlib.h> // Para free, malloc
|
||||
|
||||
#include "stb_vorbis.c" // Para stb_vorbis_decode_memory
|
||||
|
||||
constexpr int JA_MAX_SIMULTANEOUS_CHANNELS = 20;
|
||||
|
||||
struct JA_Sound_t {
|
||||
Uint32 length{0};
|
||||
Uint8* buffer{NULL};
|
||||
};
|
||||
|
||||
struct JA_Channel_t {
|
||||
JA_Sound_t* sound;
|
||||
int pos{0};
|
||||
int times{0};
|
||||
JA_Channel_state state{JA_CHANNEL_FREE};
|
||||
};
|
||||
|
||||
struct JA_Music_t {
|
||||
int samples{0};
|
||||
Uint32 length{0};
|
||||
int pos{0};
|
||||
int times{0};
|
||||
short* output{NULL};
|
||||
JA_Music_state state{JA_MUSIC_INVALID};
|
||||
};
|
||||
|
||||
JA_Music_t* current_music{NULL};
|
||||
JA_Channel_t channels[JA_MAX_SIMULTANEOUS_CHANNELS];
|
||||
|
||||
int JA_freq{48000};
|
||||
SDL_AudioFormat JA_format{AUDIO_S16};
|
||||
Uint8 JA_channels{2};
|
||||
int JA_musicVolume = 128;
|
||||
int JA_soundVolume = 64;
|
||||
bool JA_musicEnabled = true;
|
||||
bool JA_soundEnabled = true;
|
||||
SDL_AudioDeviceID sdlAudioDevice = 0;
|
||||
|
||||
bool fading = false;
|
||||
int fade_start_time;
|
||||
int fade_duration;
|
||||
int fade_initial_volume;
|
||||
|
||||
void audioCallback(void* userdata, uint8_t* stream, int len) {
|
||||
SDL_memset(stream, 0, len);
|
||||
if (current_music != NULL && current_music->state == JA_MUSIC_PLAYING) {
|
||||
int volume = JA_musicVolume;
|
||||
if (fading) {
|
||||
int time = SDL_GetTicks();
|
||||
if (time > (fade_start_time + fade_duration)) {
|
||||
fading = false;
|
||||
current_music->pos = 0;
|
||||
current_music->state = JA_MUSIC_STOPPED;
|
||||
volume = 0;
|
||||
} else {
|
||||
const int time_passed = time - fade_start_time;
|
||||
const float percent = (float)time_passed / (float)fade_duration;
|
||||
volume = JA_musicVolume * (1.0 - percent);
|
||||
}
|
||||
}
|
||||
const int size = SDL_min(len, current_music->length - current_music->pos);
|
||||
SDL_MixAudioFormat(stream, (Uint8*)(current_music->output) + current_music->pos, AUDIO_S16, size, volume);
|
||||
current_music->pos += size;
|
||||
if (size < len) {
|
||||
if (current_music->times != 0) {
|
||||
SDL_MixAudioFormat(stream + size, (Uint8*)current_music->output, AUDIO_S16, len - size, volume);
|
||||
current_music->pos = len - size;
|
||||
if (current_music->times > 0)
|
||||
current_music->times--;
|
||||
} else {
|
||||
current_music->pos = 0;
|
||||
current_music->state = JA_MUSIC_STOPPED;
|
||||
}
|
||||
}
|
||||
}
|
||||
// Mixar els channels mi amol
|
||||
for (int i = 0; i < JA_MAX_SIMULTANEOUS_CHANNELS; i++) {
|
||||
if (channels[i].state == JA_CHANNEL_PLAYING) {
|
||||
const int size = SDL_min(len, channels[i].sound->length - channels[i].pos);
|
||||
SDL_MixAudioFormat(stream, channels[i].sound->buffer + channels[i].pos, AUDIO_S16, size, JA_soundVolume);
|
||||
channels[i].pos += size;
|
||||
if (size < len) {
|
||||
if (channels[i].times != 0) {
|
||||
SDL_MixAudioFormat(stream + size, channels[i].sound->buffer, AUDIO_S16, len - size, JA_soundVolume);
|
||||
channels[i].pos = len - size;
|
||||
if (channels[i].times > 0)
|
||||
channels[i].times--;
|
||||
} else {
|
||||
JA_StopChannel(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void JA_Init(const int freq, const SDL_AudioFormat format, const int channels) {
|
||||
JA_freq = freq;
|
||||
JA_format = format;
|
||||
JA_channels = channels;
|
||||
SDL_AudioSpec audioSpec{JA_freq, JA_format, JA_channels, 0, 1024, 0, 0, audioCallback, NULL};
|
||||
if (sdlAudioDevice != 0)
|
||||
SDL_CloseAudioDevice(sdlAudioDevice);
|
||||
sdlAudioDevice = SDL_OpenAudioDevice(NULL, 0, &audioSpec, NULL, 0);
|
||||
SDL_PauseAudioDevice(sdlAudioDevice, 0);
|
||||
}
|
||||
|
||||
void JA_Quit() {
|
||||
SDL_PauseAudioDevice(sdlAudioDevice, 1);
|
||||
if (sdlAudioDevice != 0)
|
||||
SDL_CloseAudioDevice(sdlAudioDevice);
|
||||
sdlAudioDevice = 0;
|
||||
}
|
||||
|
||||
JA_Music_t* JA_LoadMusic(Uint8* buffer, Uint32 length) {
|
||||
int chan, samplerate;
|
||||
JA_Music_t* music = new JA_Music_t();
|
||||
|
||||
music->samples = stb_vorbis_decode_memory(buffer, length, &chan, &samplerate, &music->output);
|
||||
|
||||
SDL_AudioCVT cvt;
|
||||
SDL_BuildAudioCVT(&cvt, AUDIO_S16, chan, samplerate, JA_format, JA_channels, JA_freq);
|
||||
if (cvt.needed) {
|
||||
cvt.len = music->samples * chan * 2;
|
||||
music->length = cvt.len;
|
||||
cvt.buf = (Uint8*)SDL_malloc(cvt.len * cvt.len_mult);
|
||||
SDL_memcpy(cvt.buf, music->output, cvt.len);
|
||||
SDL_ConvertAudio(&cvt);
|
||||
free(music->output);
|
||||
music->output = (short*)cvt.buf;
|
||||
}
|
||||
music->length = music->samples * chan * 2;
|
||||
music->pos = 0;
|
||||
music->state = JA_MUSIC_STOPPED;
|
||||
|
||||
return music;
|
||||
}
|
||||
|
||||
JA_Music_t* JA_LoadMusic(const char* filename) {
|
||||
// [RZC 28/08/22] Carreguem primer el arxiu en memòria i després el descomprimim. Es algo més rapid.
|
||||
FILE* f = fopen(filename, "rb");
|
||||
fseek(f, 0, SEEK_END);
|
||||
long fsize = ftell(f);
|
||||
fseek(f, 0, SEEK_SET);
|
||||
Uint8* buffer = (Uint8*)malloc(fsize + 1);
|
||||
if (fread(buffer, fsize, 1, f) != 1)
|
||||
return NULL;
|
||||
fclose(f);
|
||||
|
||||
JA_Music_t* music = JA_LoadMusic(buffer, fsize);
|
||||
|
||||
free(buffer);
|
||||
|
||||
return music;
|
||||
}
|
||||
|
||||
void JA_PlayMusic(JA_Music_t* music, const int loop) {
|
||||
if (!JA_musicEnabled)
|
||||
return;
|
||||
|
||||
if (current_music != NULL) {
|
||||
current_music->pos = 0;
|
||||
current_music->state = JA_MUSIC_STOPPED;
|
||||
}
|
||||
current_music = music;
|
||||
current_music->pos = 0;
|
||||
current_music->state = JA_MUSIC_PLAYING;
|
||||
current_music->times = loop;
|
||||
}
|
||||
|
||||
void JA_PauseMusic() {
|
||||
if (!JA_musicEnabled)
|
||||
return;
|
||||
|
||||
if (current_music == NULL || current_music->state == JA_MUSIC_INVALID)
|
||||
return;
|
||||
current_music->state = JA_MUSIC_PAUSED;
|
||||
}
|
||||
|
||||
void JA_ResumeMusic() {
|
||||
if (!JA_musicEnabled)
|
||||
return;
|
||||
|
||||
if (current_music == NULL || current_music->state == JA_MUSIC_INVALID)
|
||||
return;
|
||||
current_music->state = JA_MUSIC_PLAYING;
|
||||
}
|
||||
|
||||
void JA_StopMusic() {
|
||||
if (!JA_musicEnabled)
|
||||
return;
|
||||
|
||||
if (current_music == NULL || current_music->state == JA_MUSIC_INVALID)
|
||||
return;
|
||||
current_music->pos = 0;
|
||||
current_music->state = JA_MUSIC_STOPPED;
|
||||
}
|
||||
|
||||
void JA_FadeOutMusic(const int milliseconds) {
|
||||
if (!JA_musicEnabled)
|
||||
return;
|
||||
if (current_music == NULL || current_music->state == JA_MUSIC_INVALID)
|
||||
return;
|
||||
|
||||
fading = true;
|
||||
fade_start_time = SDL_GetTicks();
|
||||
fade_duration = milliseconds;
|
||||
fade_initial_volume = JA_musicVolume;
|
||||
}
|
||||
|
||||
JA_Music_state JA_GetMusicState() {
|
||||
if (!JA_musicEnabled)
|
||||
return JA_MUSIC_DISABLED;
|
||||
|
||||
if (current_music == NULL)
|
||||
return JA_MUSIC_INVALID;
|
||||
return current_music->state;
|
||||
}
|
||||
|
||||
void JA_DeleteMusic(JA_Music_t* music) {
|
||||
if (current_music == music)
|
||||
current_music = NULL;
|
||||
free(music->output);
|
||||
delete music;
|
||||
}
|
||||
|
||||
int JA_SetMusicVolume(int volume) {
|
||||
JA_musicVolume = volume > 128 ? 128 : volume < 0 ? 0
|
||||
: volume;
|
||||
return JA_musicVolume;
|
||||
}
|
||||
|
||||
void JA_SetMusicPosition(float value) {
|
||||
if (!current_music)
|
||||
return;
|
||||
current_music->pos = value * JA_freq;
|
||||
}
|
||||
|
||||
float JA_GetMusicPosition() {
|
||||
if (!current_music)
|
||||
return 0;
|
||||
return float(current_music->pos) / float(JA_freq);
|
||||
}
|
||||
|
||||
void JA_EnableMusic(const bool value) {
|
||||
if (!value && current_music != NULL && current_music->state == JA_MUSIC_PLAYING)
|
||||
JA_StopMusic();
|
||||
|
||||
JA_musicEnabled = value;
|
||||
}
|
||||
|
||||
JA_Sound_t* JA_NewSound(Uint8* buffer, Uint32 length) {
|
||||
JA_Sound_t* sound = new JA_Sound_t();
|
||||
sound->buffer = buffer;
|
||||
sound->length = length;
|
||||
return sound;
|
||||
}
|
||||
|
||||
JA_Sound_t* JA_LoadSound(uint8_t* buffer, uint32_t size) {
|
||||
JA_Sound_t* sound = new JA_Sound_t();
|
||||
SDL_AudioSpec wavSpec;
|
||||
SDL_LoadWAV_RW(SDL_RWFromMem(buffer, size), 1, &wavSpec, &sound->buffer, &sound->length);
|
||||
|
||||
SDL_AudioCVT cvt;
|
||||
SDL_BuildAudioCVT(&cvt, wavSpec.format, wavSpec.channels, wavSpec.freq, JA_format, JA_channels, JA_freq);
|
||||
cvt.len = sound->length;
|
||||
cvt.buf = (Uint8*)SDL_malloc(cvt.len * cvt.len_mult);
|
||||
SDL_memcpy(cvt.buf, sound->buffer, sound->length);
|
||||
SDL_ConvertAudio(&cvt);
|
||||
SDL_FreeWAV(sound->buffer);
|
||||
sound->buffer = cvt.buf;
|
||||
sound->length = cvt.len_cvt;
|
||||
|
||||
return sound;
|
||||
}
|
||||
|
||||
JA_Sound_t* JA_LoadSound(const char* filename) {
|
||||
JA_Sound_t* sound = new JA_Sound_t();
|
||||
SDL_AudioSpec wavSpec;
|
||||
SDL_LoadWAV(filename, &wavSpec, &sound->buffer, &sound->length);
|
||||
|
||||
SDL_AudioCVT cvt;
|
||||
SDL_BuildAudioCVT(&cvt, wavSpec.format, wavSpec.channels, wavSpec.freq, JA_format, JA_channels, JA_freq);
|
||||
cvt.len = sound->length;
|
||||
cvt.buf = (Uint8*)SDL_malloc(cvt.len * cvt.len_mult);
|
||||
SDL_memcpy(cvt.buf, sound->buffer, sound->length);
|
||||
SDL_ConvertAudio(&cvt);
|
||||
SDL_FreeWAV(sound->buffer);
|
||||
sound->buffer = cvt.buf;
|
||||
sound->length = cvt.len_cvt;
|
||||
|
||||
return sound;
|
||||
}
|
||||
|
||||
int JA_PlaySound(JA_Sound_t* sound, const int loop) {
|
||||
if (!JA_soundEnabled)
|
||||
return -1;
|
||||
|
||||
int channel = 0;
|
||||
while (channel < JA_MAX_SIMULTANEOUS_CHANNELS && channels[channel].state != JA_CHANNEL_FREE) {
|
||||
channel++;
|
||||
}
|
||||
if (channel == JA_MAX_SIMULTANEOUS_CHANNELS)
|
||||
channel = 0;
|
||||
|
||||
channels[channel].sound = sound;
|
||||
channels[channel].times = loop;
|
||||
channels[channel].pos = 0;
|
||||
channels[channel].state = JA_CHANNEL_PLAYING;
|
||||
return channel;
|
||||
}
|
||||
|
||||
int JA_PlaySoundOnChannel(JA_Sound_t* sound, const int channel, const int loop) {
|
||||
if (!JA_soundEnabled)
|
||||
return -1;
|
||||
|
||||
if (channel >= JA_MAX_SIMULTANEOUS_CHANNELS)
|
||||
return -1;
|
||||
|
||||
channels[channel].sound = sound;
|
||||
channels[channel].times = loop;
|
||||
channels[channel].pos = 0;
|
||||
channels[channel].state = JA_CHANNEL_PLAYING;
|
||||
return channel;
|
||||
}
|
||||
|
||||
void JA_DeleteSound(JA_Sound_t* sound) {
|
||||
for (int i = 0; i < JA_MAX_SIMULTANEOUS_CHANNELS; i++) {
|
||||
if (channels[i].sound == sound)
|
||||
JA_StopChannel(i);
|
||||
}
|
||||
SDL_free(sound->buffer);
|
||||
delete sound;
|
||||
}
|
||||
|
||||
void JA_PauseChannel(const int channel) {
|
||||
if (!JA_soundEnabled)
|
||||
return;
|
||||
|
||||
if (channel == -1) {
|
||||
for (int i = 0; i < JA_MAX_SIMULTANEOUS_CHANNELS; i++) {
|
||||
if (channels[i].state == JA_CHANNEL_PLAYING)
|
||||
channels[i].state = JA_CHANNEL_PAUSED;
|
||||
}
|
||||
} else if (channel >= 0 && channel < JA_MAX_SIMULTANEOUS_CHANNELS) {
|
||||
if (channels[channel].state == JA_CHANNEL_PLAYING)
|
||||
channels[channel].state = JA_CHANNEL_PAUSED;
|
||||
}
|
||||
}
|
||||
|
||||
void JA_ResumeChannel(const int channel) {
|
||||
if (!JA_soundEnabled)
|
||||
return;
|
||||
|
||||
if (channel == -1) {
|
||||
for (int i = 0; i < JA_MAX_SIMULTANEOUS_CHANNELS; i++) {
|
||||
if (channels[i].state == JA_CHANNEL_PAUSED)
|
||||
channels[i].state = JA_CHANNEL_PLAYING;
|
||||
}
|
||||
} else if (channel >= 0 && channel < JA_MAX_SIMULTANEOUS_CHANNELS) {
|
||||
if (channels[channel].state == JA_CHANNEL_PAUSED)
|
||||
channels[channel].state = JA_CHANNEL_PLAYING;
|
||||
}
|
||||
}
|
||||
|
||||
void JA_StopChannel(const int channel) {
|
||||
if (!JA_soundEnabled)
|
||||
return;
|
||||
|
||||
if (channel == -1) {
|
||||
for (int i = 0; i < JA_MAX_SIMULTANEOUS_CHANNELS; i++) {
|
||||
channels[i].state = JA_CHANNEL_FREE;
|
||||
channels[i].pos = 0;
|
||||
channels[i].sound = NULL;
|
||||
}
|
||||
} else if (channel >= 0 && channel < JA_MAX_SIMULTANEOUS_CHANNELS) {
|
||||
channels[channel].state = JA_CHANNEL_FREE;
|
||||
channels[channel].pos = 0;
|
||||
channels[channel].sound = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
JA_Channel_state JA_GetChannelState(const int channel) {
|
||||
if (!JA_soundEnabled)
|
||||
return JA_SOUND_DISABLED;
|
||||
|
||||
if (channel < 0 || channel >= JA_MAX_SIMULTANEOUS_CHANNELS)
|
||||
return JA_CHANNEL_INVALID;
|
||||
return channels[channel].state;
|
||||
}
|
||||
|
||||
int JA_SetSoundVolume(int volume) {
|
||||
JA_soundVolume = volume > 128 ? 128 : volume < 0 ? 0
|
||||
: volume;
|
||||
return JA_soundVolume;
|
||||
}
|
||||
|
||||
void JA_EnableSound(const bool value) {
|
||||
for (int i = 0; i < JA_MAX_SIMULTANEOUS_CHANNELS; i++) {
|
||||
if (channels[i].state == JA_CHANNEL_PLAYING)
|
||||
JA_StopChannel(i);
|
||||
}
|
||||
JA_soundEnabled = value;
|
||||
}
|
||||
|
||||
int JA_SetVolume(int volume) {
|
||||
JA_musicVolume = volume > 128 ? 128 : volume < 0 ? 0
|
||||
: volume;
|
||||
JA_soundVolume = JA_musicVolume / 2;
|
||||
return JA_musicVolume;
|
||||
}
|
||||
56
source/external/jail_audio.h
vendored
Normal file
56
source/external/jail_audio.h
vendored
Normal file
@@ -0,0 +1,56 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL3/SDL_audio.h> // Para SDL_AudioFormat
|
||||
#include <SDL3/SDL_stdinc.h> // Para Uint32, Uint8
|
||||
struct JA_Music_t; // lines 5-5
|
||||
struct JA_Sound_t; // lines 6-6
|
||||
|
||||
enum JA_Channel_state {
|
||||
JA_CHANNEL_INVALID,
|
||||
JA_CHANNEL_FREE,
|
||||
JA_CHANNEL_PLAYING,
|
||||
JA_CHANNEL_PAUSED,
|
||||
JA_SOUND_DISABLED
|
||||
};
|
||||
enum JA_Music_state {
|
||||
JA_MUSIC_INVALID,
|
||||
JA_MUSIC_PLAYING,
|
||||
JA_MUSIC_PAUSED,
|
||||
JA_MUSIC_STOPPED,
|
||||
JA_MUSIC_DISABLED
|
||||
};
|
||||
|
||||
struct JA_Sound_t;
|
||||
struct JA_Music_t;
|
||||
|
||||
void JA_Init(const int freq, const SDL_AudioFormat format, const int channels);
|
||||
void JA_Quit();
|
||||
|
||||
JA_Music_t* JA_LoadMusic(const char* filename);
|
||||
JA_Music_t* JA_LoadMusic(Uint8* buffer, Uint32 length);
|
||||
void JA_PlayMusic(JA_Music_t* music, const int loop = -1);
|
||||
void JA_PauseMusic();
|
||||
void JA_ResumeMusic();
|
||||
void JA_StopMusic();
|
||||
void JA_FadeOutMusic(const int milliseconds);
|
||||
JA_Music_state JA_GetMusicState();
|
||||
void JA_DeleteMusic(JA_Music_t* music);
|
||||
int JA_SetMusicVolume(int volume);
|
||||
void JA_SetMusicPosition(float value);
|
||||
float JA_GetMusicPosition();
|
||||
void JA_EnableMusic(const bool value);
|
||||
|
||||
JA_Sound_t* JA_NewSound(Uint8* buffer, Uint32 length);
|
||||
JA_Sound_t* JA_LoadSound(Uint8* buffer, Uint32 length);
|
||||
JA_Sound_t* JA_LoadSound(const char* filename);
|
||||
int JA_PlaySound(JA_Sound_t* sound, const int loop = 0);
|
||||
int JA_PlaySoundOnChannel(JA_Sound_t* sound, const int channel, const int loop = 0);
|
||||
void JA_PauseChannel(const int channel);
|
||||
void JA_ResumeChannel(const int channel);
|
||||
void JA_StopChannel(const int channel);
|
||||
JA_Channel_state JA_GetChannelState(const int channel);
|
||||
void JA_DeleteSound(JA_Sound_t* sound);
|
||||
int JA_SetSoundVolume(int volume);
|
||||
void JA_EnableSound(const bool value);
|
||||
|
||||
int JA_SetVolume(int volume);
|
||||
267
source/external/jail_shader.cpp
vendored
Normal file
267
source/external/jail_shader.cpp
vendored
Normal file
@@ -0,0 +1,267 @@
|
||||
#include "jail_shader.h"
|
||||
|
||||
#include <SDL3/SDL_rect.h> // Para SDL_Point
|
||||
#include <SDL3/SDL_stdinc.h> // Para SDL_bool
|
||||
|
||||
#include <cstring> // Para strncmp
|
||||
#include <iostream> // Para basic_ostream, operator<<, endl, cout
|
||||
#include <stdexcept> // Para runtime_error
|
||||
#include <vector> // Para vector
|
||||
|
||||
#ifdef __APPLE__
|
||||
#include <OpenGL/OpenGL.h> // Para OpenGL en macOS
|
||||
|
||||
#include "CoreFoundation/CoreFoundation.h" // Para Core Foundation en macOS
|
||||
#if ESSENTIAL_GL_PRACTICES_SUPPORT_GL3
|
||||
#include <OpenGL/gl3.h> // Para OpenGL 3 en macOS
|
||||
#else // NO ESSENTIAL_GL_PRACTICES_SUPPORT_GL3
|
||||
#include <OpenGL/gl.h> // Para OpenGL (compatibilidad) en macOS
|
||||
#endif // ESSENTIAL_GL_PRACTICES_SUPPORT_GL3
|
||||
#else // SI NO ES __APPLE__
|
||||
#include <SDL3/SDL_opengl.h> // Para GLuint, glTexCoord2f, glVertex2f, GLfloat
|
||||
#endif // __APPLE__
|
||||
|
||||
namespace shader {
|
||||
SDL_Window* win = nullptr;
|
||||
SDL_Renderer* renderer = nullptr;
|
||||
GLuint programId = 0;
|
||||
SDL_Texture* backBuffer = nullptr;
|
||||
SDL_Point win_size = {320 * 4, 256 * 4};
|
||||
SDL_Point tex_size = {320, 256};
|
||||
bool usingOpenGL = false;
|
||||
|
||||
#ifndef __APPLE__
|
||||
// Declaración de funciones de extensión de OpenGL (evitando GLEW)
|
||||
PFNGLCREATESHADERPROC glCreateShader;
|
||||
PFNGLSHADERSOURCEPROC glShaderSource;
|
||||
PFNGLCOMPILESHADERPROC glCompileShader;
|
||||
PFNGLGETSHADERIVPROC glGetShaderiv;
|
||||
PFNGLGETSHADERINFOLOGPROC glGetShaderInfoLog;
|
||||
PFNGLDELETESHADERPROC glDeleteShader;
|
||||
PFNGLATTACHSHADERPROC glAttachShader;
|
||||
PFNGLCREATEPROGRAMPROC glCreateProgram;
|
||||
PFNGLLINKPROGRAMPROC glLinkProgram;
|
||||
PFNGLVALIDATEPROGRAMPROC glValidateProgram;
|
||||
PFNGLGETPROGRAMIVPROC glGetProgramiv;
|
||||
PFNGLGETPROGRAMINFOLOGPROC glGetProgramInfoLog;
|
||||
PFNGLUSEPROGRAMPROC glUseProgram;
|
||||
|
||||
bool initGLExtensions() {
|
||||
glCreateShader = (PFNGLCREATESHADERPROC)SDL_GL_GetProcAddress("glCreateShader");
|
||||
glShaderSource = (PFNGLSHADERSOURCEPROC)SDL_GL_GetProcAddress("glShaderSource");
|
||||
glCompileShader = (PFNGLCOMPILESHADERPROC)SDL_GL_GetProcAddress("glCompileShader");
|
||||
glGetShaderiv = (PFNGLGETSHADERIVPROC)SDL_GL_GetProcAddress("glGetShaderiv");
|
||||
glGetShaderInfoLog = (PFNGLGETSHADERINFOLOGPROC)SDL_GL_GetProcAddress("glGetShaderInfoLog");
|
||||
glDeleteShader = (PFNGLDELETESHADERPROC)SDL_GL_GetProcAddress("glDeleteShader");
|
||||
glAttachShader = (PFNGLATTACHSHADERPROC)SDL_GL_GetProcAddress("glAttachShader");
|
||||
glCreateProgram = (PFNGLCREATEPROGRAMPROC)SDL_GL_GetProcAddress("glCreateProgram");
|
||||
glLinkProgram = (PFNGLLINKPROGRAMPROC)SDL_GL_GetProcAddress("glLinkProgram");
|
||||
glValidateProgram = (PFNGLVALIDATEPROGRAMPROC)SDL_GL_GetProcAddress("glValidateProgram");
|
||||
glGetProgramiv = (PFNGLGETPROGRAMIVPROC)SDL_GL_GetProcAddress("glGetProgramiv");
|
||||
glGetProgramInfoLog = (PFNGLGETPROGRAMINFOLOGPROC)SDL_GL_GetProcAddress("glGetProgramInfoLog");
|
||||
glUseProgram = (PFNGLUSEPROGRAMPROC)SDL_GL_GetProcAddress("glUseProgram");
|
||||
|
||||
return glCreateShader && glShaderSource && glCompileShader && glGetShaderiv &&
|
||||
glGetShaderInfoLog && glDeleteShader && glAttachShader && glCreateProgram &&
|
||||
glLinkProgram && glValidateProgram && glGetProgramiv && glGetProgramInfoLog &&
|
||||
glUseProgram;
|
||||
}
|
||||
#endif
|
||||
|
||||
// Función para compilar un shader a partir de un std::string
|
||||
GLuint compileShader(const std::string& source, GLuint shaderType) {
|
||||
if (source.empty()) {
|
||||
throw std::runtime_error("ERROR FATAL: El código fuente del shader está vacío.");
|
||||
}
|
||||
|
||||
// Crear identificador del shader
|
||||
GLuint resultado = glCreateShader(shaderType);
|
||||
|
||||
// Agregar una directiva según el tipo de shader
|
||||
std::string directiva = (shaderType == GL_VERTEX_SHADER)
|
||||
? "#define VERTEX\n"
|
||||
: "#define FRAGMENT\n";
|
||||
|
||||
const char* sources[2] = {directiva.c_str(), source.c_str()};
|
||||
|
||||
// Especificar el código fuente del shader
|
||||
glShaderSource(resultado, 2, sources, nullptr);
|
||||
|
||||
// Compilar el shader
|
||||
glCompileShader(resultado);
|
||||
|
||||
// Verificar si la compilación fue exitosa
|
||||
GLint compiladoCorrectamente = GL_FALSE;
|
||||
glGetShaderiv(resultado, GL_COMPILE_STATUS, &compiladoCorrectamente);
|
||||
if (compiladoCorrectamente != GL_TRUE) {
|
||||
std::cout << "Error en la compilación del shader (" << resultado << ")!" << std::endl;
|
||||
GLint longitudLog;
|
||||
glGetShaderiv(resultado, GL_INFO_LOG_LENGTH, &longitudLog);
|
||||
if (longitudLog > 0) {
|
||||
std::vector<GLchar> log(longitudLog);
|
||||
glGetShaderInfoLog(resultado, longitudLog, &longitudLog, log.data());
|
||||
std::cout << "Registro de compilación del shader: " << log.data() << std::endl;
|
||||
}
|
||||
glDeleteShader(resultado);
|
||||
resultado = 0;
|
||||
}
|
||||
return resultado;
|
||||
}
|
||||
|
||||
// Función para compilar un programa de shaders (vertex y fragment) a partir de std::string
|
||||
GLuint compileProgram(const std::string& vertexShaderSource, const std::string& fragmentShaderSource) {
|
||||
GLuint idPrograma = glCreateProgram();
|
||||
|
||||
// Si el fragment shader está vacío, reutilizamos el código del vertex shader
|
||||
GLuint idShaderVertice = compileShader(vertexShaderSource, GL_VERTEX_SHADER);
|
||||
GLuint idShaderFragmento = compileShader(fragmentShaderSource.empty() ? vertexShaderSource : fragmentShaderSource, GL_FRAGMENT_SHADER);
|
||||
|
||||
if (idShaderVertice && idShaderFragmento) {
|
||||
// Asociar los shaders al programa
|
||||
glAttachShader(idPrograma, idShaderVertice);
|
||||
glAttachShader(idPrograma, idShaderFragmento);
|
||||
glLinkProgram(idPrograma);
|
||||
glValidateProgram(idPrograma);
|
||||
|
||||
// Verificar el estado del enlace
|
||||
GLint longitudLog;
|
||||
glGetProgramiv(idPrograma, GL_INFO_LOG_LENGTH, &longitudLog);
|
||||
if (longitudLog > 0) {
|
||||
std::vector<char> log(longitudLog);
|
||||
glGetProgramInfoLog(idPrograma, longitudLog, &longitudLog, log.data());
|
||||
std::cout << "Registro de información del programa:" << std::endl
|
||||
<< log.data() << std::endl;
|
||||
}
|
||||
}
|
||||
if (idShaderVertice) {
|
||||
glDeleteShader(idShaderVertice);
|
||||
}
|
||||
if (idShaderFragmento) {
|
||||
glDeleteShader(idShaderFragmento);
|
||||
}
|
||||
return idPrograma;
|
||||
}
|
||||
|
||||
bool init(SDL_Window* ventana, SDL_Texture* texturaBackBuffer, const std::string& vertexShader, const std::string& fragmentShader) {
|
||||
shader::win = ventana;
|
||||
shader::renderer = SDL_GetRenderer(ventana);
|
||||
shader::backBuffer = texturaBackBuffer;
|
||||
SDL_GetWindowSize(ventana, &win_size.x, &win_size.y);
|
||||
|
||||
int acceso;
|
||||
SDL_QueryTexture(texturaBackBuffer, nullptr, &acceso, &tex_size.x, &tex_size.y);
|
||||
if (acceso != SDL_TEXTUREACCESS_TARGET) {
|
||||
throw std::runtime_error("ERROR FATAL: La textura debe tener definido SDL_TEXTUREACCESS_TARGET.");
|
||||
}
|
||||
|
||||
SDL_RendererInfo infoRenderer;
|
||||
SDL_GetRendererInfo(renderer, &infoRenderer);
|
||||
|
||||
// Verificar que el renderer sea OpenGL
|
||||
if (!strncmp(infoRenderer.name, "opengl", 6)) {
|
||||
#ifndef __APPLE__
|
||||
if (!initGLExtensions()) {
|
||||
std::cout << "ADVERTENCIA: No se han podido inicializar las extensiones de OpenGL." << std::endl;
|
||||
usingOpenGL = false;
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
// Compilar el programa de shaders utilizando std::string
|
||||
programId = compileProgram(vertexShader, fragmentShader);
|
||||
} else {
|
||||
std::cout << "ADVERTENCIA: El driver del renderer no es OpenGL." << std::endl;
|
||||
usingOpenGL = false;
|
||||
return false;
|
||||
}
|
||||
usingOpenGL = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
void render() {
|
||||
GLint oldProgramId;
|
||||
// Establece el color de fondo
|
||||
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
|
||||
SDL_SetRenderTarget(renderer, nullptr);
|
||||
SDL_RenderClear(renderer);
|
||||
|
||||
if (usingOpenGL) {
|
||||
SDL_GL_BindTexture(backBuffer, nullptr, nullptr);
|
||||
if (programId != 0) {
|
||||
glGetIntegerv(GL_CURRENT_PROGRAM, &oldProgramId);
|
||||
glUseProgram(programId);
|
||||
}
|
||||
|
||||
// Recupera el tamaño lógico configurado con SDL_RenderSetLogicalSize
|
||||
int logicalW, logicalH;
|
||||
SDL_RenderGetLogicalSize(renderer, &logicalW, &logicalH);
|
||||
if (logicalW == 0 || logicalH == 0) {
|
||||
logicalW = win_size.x;
|
||||
logicalH = win_size.y;
|
||||
}
|
||||
|
||||
// Cálculo del viewport
|
||||
int viewportX = 0, viewportY = 0, viewportW = win_size.x, viewportH = win_size.y;
|
||||
SDL_bool useIntegerScale = SDL_RenderGetIntegerScale(renderer);
|
||||
if (useIntegerScale) {
|
||||
// Calcula el factor de escalado entero máximo que se puede aplicar
|
||||
int scaleX = win_size.x / logicalW;
|
||||
int scaleY = win_size.y / logicalH;
|
||||
int scale = (scaleX < scaleY ? scaleX : scaleY);
|
||||
if (scale < 1)
|
||||
scale = 1;
|
||||
viewportW = logicalW * scale;
|
||||
viewportH = logicalH * scale;
|
||||
viewportX = (win_size.x - viewportW) / 2;
|
||||
viewportY = (win_size.y - viewportH) / 2;
|
||||
} else {
|
||||
// Letterboxing: preserva la relación de aspecto usando una escala flotante
|
||||
float windowAspect = static_cast<float>(win_size.x) / win_size.y;
|
||||
float logicalAspect = static_cast<float>(logicalW) / logicalH;
|
||||
if (windowAspect > logicalAspect) {
|
||||
viewportW = static_cast<int>(logicalAspect * win_size.y);
|
||||
viewportX = (win_size.x - viewportW) / 2;
|
||||
} else {
|
||||
viewportH = static_cast<int>(win_size.x / logicalAspect);
|
||||
viewportY = (win_size.y - viewportH) / 2;
|
||||
}
|
||||
}
|
||||
glViewport(viewportX, viewportY, viewportW, viewportH);
|
||||
|
||||
// Configurar la proyección ortográfica usando el espacio lógico
|
||||
glMatrixMode(GL_PROJECTION);
|
||||
glLoadIdentity();
|
||||
|
||||
// Queremos que el origen esté en la esquina superior izquierda del espacio lógico.
|
||||
glOrtho(0, static_cast<GLdouble>(logicalW), static_cast<GLdouble>(logicalH), 0, -1, 1);
|
||||
glMatrixMode(GL_MODELVIEW);
|
||||
glLoadIdentity();
|
||||
|
||||
// Dibuja el quad con las coordenadas ajustadas.
|
||||
// Se asignan las coordenadas de textura "normales" para que no quede espejado horizontalmente,
|
||||
// y se mantiene el flip vertical para que la imagen no aparezca volteada.
|
||||
glBegin(GL_TRIANGLE_STRIP);
|
||||
// Vértice superior izquierdo
|
||||
glTexCoord2f(0.0f, 1.0f);
|
||||
glVertex2f(0.0f, 0.0f);
|
||||
// Vértice superior derecho
|
||||
glTexCoord2f(1.0f, 1.0f);
|
||||
glVertex2f(static_cast<GLfloat>(logicalW), 0.0f);
|
||||
// Vértice inferior izquierdo
|
||||
glTexCoord2f(0.0f, 0.0f);
|
||||
glVertex2f(0.0f, static_cast<GLfloat>(logicalH));
|
||||
// Vértice inferior derecho
|
||||
glTexCoord2f(1.0f, 0.0f);
|
||||
glVertex2f(static_cast<GLfloat>(logicalW), static_cast<GLfloat>(logicalH));
|
||||
glEnd();
|
||||
|
||||
SDL_GL_SwapWindow(win);
|
||||
|
||||
if (programId != 0) {
|
||||
glUseProgram(oldProgramId);
|
||||
}
|
||||
} else {
|
||||
SDL_RenderCopy(renderer, backBuffer, nullptr, nullptr);
|
||||
SDL_RenderPresent(renderer);
|
||||
}
|
||||
}
|
||||
} // namespace shader
|
||||
45
source/external/jail_shader.h
vendored
Normal file
45
source/external/jail_shader.h
vendored
Normal file
@@ -0,0 +1,45 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL3/SDL_render.h> // Para SDL_Texture
|
||||
#include <SDL3/SDL_video.h> // Para SDL_Window
|
||||
|
||||
#include <string>
|
||||
|
||||
// TIPS:
|
||||
// =======================================================================
|
||||
// Abans de crear el renderer, cridar a la següent funció:
|
||||
//
|
||||
// SDL_SetHint(SDL_HINT_RENDER_DRIVER, "opengl");
|
||||
//
|
||||
// Aixó li diu que volem un renderer que use especificament opengl. A més,
|
||||
// al crear el renderer li tenim que dir que el volem que use acceeració
|
||||
// per hardware, i que soporte render a textura. Per exemple:
|
||||
//
|
||||
// SDL_Renderer *ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED |
|
||||
// SDL_RENDERER_TARGETTEXTURE);
|
||||
//
|
||||
// Per altra part, al crear la textura tenim que definir que puga ser target
|
||||
// de renderitzat (SDL_TEXTUREACCESS_TARGET), per exemple:
|
||||
//
|
||||
// SDL_Texture *tex = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888,
|
||||
// SDL_TEXTUREACCESS_TARGET, 320, 240);
|
||||
//
|
||||
// Els shaders li'ls passem com una cadena, som nosaltres els que s'encarreguem
|
||||
// de carregarlos de disc, amb fopen, ifstream, jfile o el que vullgues.
|
||||
// Si els tens en un std::string, passa-li-la com "cadena.c_str()".
|
||||
//
|
||||
// Poden ser els dos el mateix arxiu, com fa libRetro, jo desde dins ja fique
|
||||
// els defines necessaris. Si es el mateix arxiu, pots no ficar el quart paràmetre.
|
||||
//
|
||||
// Els shaders de libRetro no funcionen directament, hi ha que fer algunes modificacions.
|
||||
//
|
||||
// El pintat final de la teua escena l'has de fer com si "backBuffer" fora la pantalla.
|
||||
//
|
||||
// Ah! una cosa mes: al compilar, en Linux afegir "-lGL", en Windows afegir "-lopengl32".
|
||||
// En Mac ni idea
|
||||
|
||||
namespace shader {
|
||||
// const bool init(SDL_Window *ventana, SDL_Texture *texturaBackBuffer, const char *vertexShader, const char *fragmentShader = nullptr);
|
||||
bool init(SDL_Window* ventana, SDL_Texture* texturaBackBuffer, const std::string& vertexShader, const std::string& fragmentShader = "");
|
||||
void render();
|
||||
} // namespace shader
|
||||
9251
source/external/stb_image.h
vendored
Normal file
9251
source/external/stb_image.h
vendored
Normal file
File diff suppressed because it is too large
Load Diff
5565
source/external/stb_vorbis.c
vendored
Normal file
5565
source/external/stb_vorbis.c
vendored
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user