Compare commits
15 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| c9114abb2d | |||
| 734cab010e | |||
| 20986a8947 | |||
| 4c0eabcc5c | |||
| a45cb95030 | |||
| 4016118375 | |||
| eea4a67542 | |||
| 752a065246 | |||
| 916f0a458e | |||
| 669090238a | |||
| 7ada99f766 | |||
| 27c122f5bb | |||
| a77a4bd364 | |||
| bee9b2e489 | |||
| 49cb0af228 |
@@ -2,6 +2,8 @@
|
||||
|
||||
x=0
|
||||
rot=0
|
||||
music_states = { "INVALID", "PLAYING", "PAUSED", "STOPPED", "DISABLED" }
|
||||
music_pos = 0
|
||||
|
||||
function mini.init()
|
||||
s = surf.load("gfx/logo.gif")
|
||||
@@ -18,6 +20,7 @@ function mini.init()
|
||||
print("========================")
|
||||
|
||||
f = font.load("font.fnt")
|
||||
--music.play("mus_menu.ogg")
|
||||
end
|
||||
|
||||
function mini.update()
|
||||
@@ -50,4 +53,30 @@ function mini.update()
|
||||
draw.text("0146",100,50,28)
|
||||
font.current(font.DEFAULT)
|
||||
rot=rot+1
|
||||
|
||||
-- MUSICA
|
||||
draw.text("music:", 1, 20, 22)
|
||||
draw.text("duration:" .. music.duration(), 1, 28, 28)
|
||||
draw.text("position:" .. music.pos(), 1, 36, 28)
|
||||
draw.text("state: " .. music_states[music.state()+1], 1, 44, 28)
|
||||
|
||||
if key.press(key.P) then
|
||||
if music.state() == music.PLAYING then
|
||||
music.pause()
|
||||
elseif music.state() == music.PAUSED then
|
||||
music.resume()
|
||||
else
|
||||
music.play("mus_menu.ogg")
|
||||
end
|
||||
elseif key.press(key.S) then
|
||||
music.stop()
|
||||
elseif key.press(key.C) then
|
||||
if music.state() == music.PLAYING then
|
||||
music_pos = music.pos()
|
||||
music.stop()
|
||||
else
|
||||
music.play("mus_menu.ogg")
|
||||
music.pos(music_pos)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Binary file not shown.
@@ -25,3 +25,19 @@ libs = -lmingw32 -lSDL3 -lopengl32
|
||||
executable = mini_debug.exe
|
||||
sourcepath = source+
|
||||
buildpath = build
|
||||
|
||||
[macos]
|
||||
compiler = clang++
|
||||
cppflags = -Wall -Os -Wno-deprecated -ffunction-sections -fdata-sections -std=c++20 -Isource
|
||||
libs = -lSDL3 -framework OpenGL
|
||||
executable = mini
|
||||
sourcepath = source+
|
||||
buildpath = build
|
||||
|
||||
[macos_debug]
|
||||
compiler = clang++
|
||||
cppflags = -D DEBUG -g -Wall -Wno-deprecated -std=c++20 -Isource
|
||||
libs = -lSDL3 -framework OpenGL
|
||||
executable = mini_debug
|
||||
sourcepath = source+
|
||||
buildpath = build
|
||||
|
||||
@@ -1,16 +1,16 @@
|
||||
#ifndef JA_USESDLMIXER
|
||||
#include "jail_audio.h"
|
||||
#if BACKEND == SDL3
|
||||
|
||||
#include "backends/backend.h"
|
||||
#include "state.h"
|
||||
#include "external/stb_vorbis.h"
|
||||
#include "other/log.h"
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <vector>
|
||||
|
||||
// structs i variables
|
||||
// =============================
|
||||
namespace jail
|
||||
namespace backend
|
||||
{
|
||||
namespace audio
|
||||
{
|
||||
@@ -22,14 +22,17 @@ namespace jail
|
||||
{
|
||||
struct music_t
|
||||
{
|
||||
SDL_AudioSpec spec { SDL_AUDIO_S16, 2, 48000 };
|
||||
Uint32 length { 0 };
|
||||
Uint8 *buffer { nullptr };
|
||||
SDL_AudioSpec spec { SDL_AUDIO_S16, 2, 48000 };
|
||||
SDL_AudioStream* stream { nullptr };
|
||||
|
||||
int pos { 0 };
|
||||
int times { 0 };
|
||||
SDL_AudioStream *stream { nullptr };
|
||||
music::state state { music::state::invalid };
|
||||
const uint8_t* ogg_data { nullptr };
|
||||
uint32_t ogg_length { 0 };
|
||||
|
||||
stb_vorbis* vorbis { nullptr };
|
||||
stb_vorbis_info info {};
|
||||
|
||||
int times { 0 };
|
||||
music::state state { music::state::invalid };
|
||||
};
|
||||
|
||||
static int current { -1 };
|
||||
@@ -79,17 +82,19 @@ namespace jail
|
||||
|
||||
// Funcions
|
||||
// ==================
|
||||
namespace jail
|
||||
namespace backend
|
||||
{
|
||||
namespace audio
|
||||
{
|
||||
static void updateMusic()
|
||||
{
|
||||
if (!music::enabled) return;
|
||||
if (music::current < 0 || music::current > static_cast<int>(music::musics.size())) return;
|
||||
auto &m = music::musics[music::current];
|
||||
if (music::current < 0) return;
|
||||
|
||||
auto& m = music::musics[music::current];
|
||||
if (m.state != music::state::playing) return;
|
||||
|
||||
// Fade-out
|
||||
if (music::fade::fading) {
|
||||
int time = SDL_GetTicks();
|
||||
if (time > (music::fade::start_time + music::fade::duration)) {
|
||||
@@ -97,23 +102,37 @@ namespace jail
|
||||
music::stop();
|
||||
return;
|
||||
} else {
|
||||
const int time_passed = time - music::fade::start_time;
|
||||
const float percent = (float)time_passed / (float)music::fade::duration;
|
||||
SDL_SetAudioStreamGain(m.stream, 1.0 - percent);
|
||||
float percent = float(time - music::fade::start_time) / float(music::fade::duration);
|
||||
SDL_SetAudioStreamGain(m.stream, 1.0f - percent);
|
||||
}
|
||||
}
|
||||
|
||||
if (m.times != 0)
|
||||
{
|
||||
if (SDL_GetAudioStreamAvailable(m.stream) < static_cast<int>(m.length/2)) {
|
||||
SDL_PutAudioStreamData(m.stream, m.buffer, m.length);
|
||||
// ¿Hay suficiente audio en el stream?
|
||||
if (SDL_GetAudioStreamAvailable(m.stream) > 48000) return; // 1 segundo
|
||||
|
||||
// Decodificar un bloque
|
||||
const int SAMPLES = 4096;
|
||||
static int16_t temp[SAMPLES * 2];
|
||||
|
||||
int n = stb_vorbis_get_samples_short_interleaved(
|
||||
m.vorbis,
|
||||
m.info.channels,
|
||||
temp,
|
||||
SAMPLES
|
||||
);
|
||||
|
||||
if (n == 0) {
|
||||
if (m.times != 0) {
|
||||
stb_vorbis_seek_start(m.vorbis);
|
||||
if (m.times > 0) m.times--;
|
||||
return;
|
||||
} else {
|
||||
music::stop();
|
||||
return;
|
||||
}
|
||||
if (m.times>0) m.times--;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (SDL_GetAudioStreamAvailable(m.stream) == 0) music::stop();
|
||||
}
|
||||
|
||||
SDL_PutAudioStreamData(m.stream, temp, n * m.info.channels * sizeof(int16_t));
|
||||
}
|
||||
|
||||
static void updateSound()
|
||||
@@ -193,26 +212,31 @@ namespace jail
|
||||
{
|
||||
int load(const uint8_t* buffer, uint32_t length)
|
||||
{
|
||||
int music = 0;
|
||||
while (music < static_cast<int>(musics.size()) && musics[music].state != state::invalid) { music++; }
|
||||
if (music == static_cast<int>(musics.size())) musics.emplace_back();
|
||||
int idx = 0;
|
||||
while (idx < (int)musics.size() && musics[idx].state != state::invalid) idx++;
|
||||
if (idx == (int)musics.size()) musics.emplace_back();
|
||||
|
||||
auto &m = musics[music];
|
||||
auto& m = musics[idx];
|
||||
|
||||
int chan, samplerate;
|
||||
short *output;
|
||||
m.length = stb_vorbis_decode_memory(buffer, length, &chan, &samplerate, &output) * chan * 2;
|
||||
m.ogg_data = buffer;
|
||||
m.ogg_length = length;
|
||||
|
||||
m.spec.channels = chan;
|
||||
m.spec.freq = samplerate;
|
||||
int error;
|
||||
m.vorbis = stb_vorbis_open_memory(buffer, length, &error, nullptr);
|
||||
if (!m.vorbis) {
|
||||
log_msg(LOG_FAIL, "stb_vorbis_open_memory failed: %d\n", error);
|
||||
return -1;
|
||||
}
|
||||
|
||||
m.info = stb_vorbis_get_info(m.vorbis);
|
||||
|
||||
m.spec.channels = m.info.channels;
|
||||
m.spec.freq = m.info.sample_rate;
|
||||
m.spec.format = SDL_AUDIO_S16;
|
||||
m.buffer = (uint8_t*)SDL_malloc(m.length);
|
||||
SDL_memcpy(m.buffer, output, m.length);
|
||||
free(output);
|
||||
m.pos = 0;
|
||||
|
||||
m.state = state::stopped;
|
||||
|
||||
return music;
|
||||
return idx;
|
||||
}
|
||||
|
||||
int load(const char* filename)
|
||||
@@ -235,29 +259,25 @@ namespace jail
|
||||
|
||||
void play(int mus, int loop)
|
||||
{
|
||||
if (!music::enabled) return;
|
||||
stop();
|
||||
if (mus < 0 || mus >= static_cast<int>(musics.size())) {
|
||||
log_msg(LOG_FAIL, "music::play: Illegal music handle: %i\n", mus);
|
||||
return;
|
||||
}
|
||||
|
||||
current = mus;
|
||||
auto &m = musics[current];
|
||||
m.pos = 0;
|
||||
m.state = state::playing;
|
||||
auto& m = musics[mus];
|
||||
|
||||
m.times = loop;
|
||||
m.state = state::playing;
|
||||
|
||||
stb_vorbis_seek_start(m.vorbis);
|
||||
|
||||
m.stream = SDL_CreateAudioStream(&m.spec, &audioSpec);
|
||||
if (!SDL_PutAudioStreamData(m.stream, m.buffer, m.length)) log_msg(LOG_FAIL, "SDL_PutAudioStreamData failed!\n");
|
||||
SDL_SetAudioStreamGain(m.stream, volume);
|
||||
if (!SDL_BindAudioStream(sdlAudioDevice, m.stream)) log_msg(LOG_FAIL, "SDL_BindAudioStream failed!\n");
|
||||
//SDL_ResumeAudioStreamDevice(current->stream);
|
||||
SDL_BindAudioStream(sdlAudioDevice, m.stream);
|
||||
}
|
||||
|
||||
void pause()
|
||||
{
|
||||
if (!music::enabled) return;
|
||||
if (current<0 || current>static_cast<int>(musics.size())) {
|
||||
if (!music::enabled || (current<0)) return;
|
||||
if (current>static_cast<int>(musics.size())) {
|
||||
log_msg(LOG_FAIL, "music::pause: Illegal music handle: %i\n", current);
|
||||
return;
|
||||
}
|
||||
@@ -274,8 +294,8 @@ namespace jail
|
||||
|
||||
void resume()
|
||||
{
|
||||
if (!music::enabled) return;
|
||||
if (current<0 || current>static_cast<int>(musics.size())) {
|
||||
if (!music::enabled || (current<0)) return;
|
||||
if (current>static_cast<int>(musics.size())) {
|
||||
log_msg(LOG_FAIL, "music::resume: Illegal music handle: %i\n", current);
|
||||
return;
|
||||
}
|
||||
@@ -292,28 +312,21 @@ namespace jail
|
||||
|
||||
void stop()
|
||||
{
|
||||
if (!music::enabled) return;
|
||||
if (current<0 || current>static_cast<int>(musics.size())) {
|
||||
log_msg(LOG_FAIL, "music::stop: Illegal music handle: %i\n", current);
|
||||
return;
|
||||
}
|
||||
auto &m = musics[current];
|
||||
if (m.state == state::invalid) {
|
||||
log_msg(LOG_FAIL, "music::stop: Invalidated music handle: %i\n", current);
|
||||
return;
|
||||
}
|
||||
if (current < 0) return;
|
||||
|
||||
auto& m = musics[current];
|
||||
|
||||
m.pos = 0;
|
||||
m.state = state::stopped;
|
||||
//SDL_PauseAudioStreamDevice(current->stream);
|
||||
SDL_DestroyAudioStream(m.stream);
|
||||
|
||||
if (m.stream) SDL_DestroyAudioStream(m.stream);
|
||||
m.stream = nullptr;
|
||||
current = -1;
|
||||
}
|
||||
|
||||
void fadeOut(int milliseconds)
|
||||
{
|
||||
if (!music::enabled) return;
|
||||
if (current<0 || current>static_cast<int>(musics.size())) {
|
||||
if (!music::enabled || (current<0)) return;
|
||||
if (current>static_cast<int>(musics.size())) {
|
||||
log_msg(LOG_FAIL, "music::fadeOut: Illegal music handle: %i\n", current);
|
||||
return;
|
||||
}
|
||||
@@ -338,16 +351,16 @@ namespace jail
|
||||
|
||||
void destroy(int mus)
|
||||
{
|
||||
if (current == mus) current = -1;
|
||||
if (mus<0 || mus>static_cast<int>(musics.size())) {
|
||||
log_msg(LOG_FAIL, "music::destroy: Illegal music handle: %i\n", mus);
|
||||
return;
|
||||
}
|
||||
auto &m = musics[mus];
|
||||
SDL_free(m.buffer);
|
||||
m.buffer = nullptr;
|
||||
if (mus < 0 || mus >= (int)musics.size()) return;
|
||||
|
||||
auto& m = musics[mus];
|
||||
|
||||
if (m.stream) SDL_DestroyAudioStream(m.stream);
|
||||
if (m.vorbis) stb_vorbis_close(m.vorbis);
|
||||
|
||||
m.stream = nullptr;
|
||||
m.vorbis = nullptr;
|
||||
|
||||
m.state = state::invalid;
|
||||
}
|
||||
|
||||
@@ -365,34 +378,50 @@ namespace jail
|
||||
|
||||
void setPosition(float value)
|
||||
{
|
||||
if (!music::enabled) return;
|
||||
if (current<0 || current>static_cast<int>(musics.size())) {
|
||||
log_msg(LOG_FAIL, "music::setPosition: Illegal music handle: %i\n", current);
|
||||
return;
|
||||
}
|
||||
auto &m = musics[current];
|
||||
if (m.state == state::invalid) {
|
||||
log_msg(LOG_FAIL, "music::setPosition: Invalidated music handle: %i\n", current);
|
||||
return;
|
||||
}
|
||||
auto& m = musics[current];
|
||||
|
||||
m.pos = value * m.spec.freq;
|
||||
int sample = int(value * m.info.sample_rate);
|
||||
|
||||
if (stb_vorbis_seek(m.vorbis, sample)) {
|
||||
SDL_ClearAudioStream(m.stream); // importante
|
||||
}
|
||||
}
|
||||
|
||||
float getPosition()
|
||||
{
|
||||
if (!music::enabled) return 0;
|
||||
if (current<0 || current>static_cast<int>(musics.size())) {
|
||||
log_msg(LOG_FAIL, "music::getPosition: Illegal music handle: %i\n", current);
|
||||
//log_msg(LOG_FAIL, "music::getPosition: Illegal music handle: %i\n", current);
|
||||
return 0;
|
||||
}
|
||||
auto &m = musics[current];
|
||||
if (m.state == state::invalid) {
|
||||
log_msg(LOG_FAIL, "music::getPosition: Invalidated music handle: %i\n", current);
|
||||
//log_msg(LOG_FAIL, "music::getPosition: Invalidated music handle: %i\n", current);
|
||||
return 0;
|
||||
}
|
||||
int sample = stb_vorbis_get_sample_offset(m.vorbis);
|
||||
return float(sample) / float(m.info.sample_rate);
|
||||
}
|
||||
|
||||
float getDuration()
|
||||
{
|
||||
if (!music::enabled) return 0;
|
||||
if (current < 0 || current > static_cast<int>(musics.size())) {
|
||||
//log_msg(LOG_FAIL, "music::getDuration: Illegal music handle: %i\n", current);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return float(m.pos)/float(m.spec.freq);
|
||||
auto &m = musics[current];
|
||||
if (m.state == state::invalid) {
|
||||
//log_msg(LOG_FAIL, "music::getDuration: Invalidated music handle: %i\n", current);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Total de muestras del stream
|
||||
int total_samples = stb_vorbis_stream_length_in_samples(m.vorbis);
|
||||
|
||||
// Convertir a segundos
|
||||
return float(total_samples) / float(m.info.sample_rate);
|
||||
}
|
||||
|
||||
void enable(bool value)
|
||||
@@ -0,0 +1,246 @@
|
||||
#if BACKEND == SDL3
|
||||
|
||||
#include "backends/backend.h"
|
||||
#include "state.h"
|
||||
#include "mini/file/file.h"
|
||||
#include "mini/win/win.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#ifdef __APPLE__
|
||||
#include <CoreFoundation/CoreFoundation.h>
|
||||
#include <OpenGL/OpenGL.h>
|
||||
|
||||
#if ESSENTIAL_GL_PRACTICES_SUPPORT_GL3
|
||||
#include <OpenGL/gl3.h>
|
||||
#else
|
||||
#include <OpenGL/gl.h>
|
||||
#endif //!ESSENTIAL_GL_PRACTICES_SUPPORT_GL3
|
||||
#else
|
||||
#include <SDL3/SDL_opengl.h>
|
||||
#include <SDL3/SDL_opengl_glext.h>
|
||||
#endif
|
||||
|
||||
namespace backend
|
||||
{
|
||||
namespace shader
|
||||
{
|
||||
GLuint programId = 0;
|
||||
SDL_Point win_size = {640, 480};
|
||||
SDL_FPoint tex_size = {320, 240};
|
||||
bool can_use_opengl = false;
|
||||
bool using_opengl = false;
|
||||
|
||||
#ifndef __APPLE__
|
||||
|
||||
// I'm avoiding the use of GLEW or some extensions handler, but that
|
||||
// doesn't mean you should...
|
||||
PFNGLCREATESHADERPROC glCreateShader;
|
||||
PFNGLSHADERSOURCEPROC glShaderSource;
|
||||
PFNGLCOMPILESHADERPROC glCompileShader;
|
||||
PFNGLGETSHADERIVPROC glGetShaderiv;
|
||||
PFNGLGETSHADERINFOLOGPROC glGetShaderInfoLog;
|
||||
PFNGLDELETESHADERPROC glDeleteShader;
|
||||
PFNGLATTACHSHADERPROC glAttachShader;
|
||||
PFNGLCREATEPROGRAMPROC glCreateProgram;
|
||||
PFNGLDELETEPROGRAMPROC glDeleteProgram;
|
||||
PFNGLLINKPROGRAMPROC glLinkProgram;
|
||||
PFNGLVALIDATEPROGRAMPROC glValidateProgram;
|
||||
PFNGLGETPROGRAMIVPROC glGetProgramiv;
|
||||
PFNGLGETPROGRAMINFOLOGPROC glGetProgramInfoLog;
|
||||
PFNGLUSEPROGRAMPROC glUseProgram;
|
||||
PFNGLGETUNIFORMLOCATIONPROC glGetUniformLocation;
|
||||
PFNGLACTIVETEXTUREPROC glActiveTexture;
|
||||
|
||||
|
||||
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");
|
||||
glDeleteProgram = (PFNGLDELETEPROGRAMPROC)SDL_GL_GetProcAddress("glDeleteProgram");
|
||||
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");
|
||||
glGetUniformLocation = (PFNGLGETUNIFORMLOCATIONPROC)SDL_GL_GetProcAddress("glGetUniformLocation");
|
||||
glActiveTexture = (PFNGLACTIVETEXTUREPROC)SDL_GL_GetProcAddress("glActiveTexture");
|
||||
|
||||
return glCreateShader && glShaderSource && glCompileShader && glGetShaderiv &&
|
||||
glGetShaderInfoLog && glDeleteShader && glAttachShader && glCreateProgram &&
|
||||
glDeleteProgram && glLinkProgram && glValidateProgram && glGetProgramiv &&
|
||||
glGetProgramInfoLog && glUseProgram && glGetUniformLocation;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
GLuint compileShader(const char* source, GLuint shaderType) {
|
||||
// Create ID for shader
|
||||
GLuint result = glCreateShader(shaderType);
|
||||
// Add define depending on shader type
|
||||
const char *sources[2] = { shaderType==GL_VERTEX_SHADER?"#define VERTEX\n":"#define FRAGMENT\n", source };
|
||||
// Define shader text
|
||||
glShaderSource(result, 2, sources, NULL);
|
||||
// Compile shader
|
||||
glCompileShader(result);
|
||||
|
||||
//Check vertex shader for errors
|
||||
GLint shaderCompiled = GL_FALSE;
|
||||
glGetShaderiv( result, GL_COMPILE_STATUS, &shaderCompiled );
|
||||
if (shaderCompiled != GL_TRUE)
|
||||
{
|
||||
std::cout << "Error en la compilación: " << result << "!" << std::endl;
|
||||
GLint logLength;
|
||||
glGetShaderiv(result, GL_INFO_LOG_LENGTH, &logLength);
|
||||
if (logLength > 0)
|
||||
{
|
||||
GLchar *log = (GLchar*)malloc(logLength);
|
||||
glGetShaderInfoLog(result, logLength, &logLength, log);
|
||||
std::cout << "Shader compile log:" << log << std::endl;
|
||||
//std::cout << source << std::endl;
|
||||
free(log);
|
||||
}
|
||||
glDeleteShader(result);
|
||||
result = 0;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
GLuint compileProgram(const char* vertexShaderSource, const char* fragmentShaderSource)
|
||||
{
|
||||
GLuint programId = 0;
|
||||
GLuint vtxShaderId, fragShaderId;
|
||||
|
||||
if (programId != 0) glDeleteProgram(programId);
|
||||
programId = glCreateProgram();
|
||||
|
||||
|
||||
vtxShaderId = compileShader(vertexShaderSource, GL_VERTEX_SHADER);
|
||||
fragShaderId = compileShader(fragmentShaderSource?fragmentShaderSource:vertexShaderSource, GL_FRAGMENT_SHADER);
|
||||
|
||||
if(vtxShaderId && fragShaderId)
|
||||
{
|
||||
// Associate shader with program
|
||||
glAttachShader(programId, vtxShaderId);
|
||||
glAttachShader(programId, fragShaderId);
|
||||
glLinkProgram(programId);
|
||||
glValidateProgram(programId);
|
||||
|
||||
// Check the status of the compile/link
|
||||
GLint logLen;
|
||||
glGetProgramiv(programId, GL_INFO_LOG_LENGTH, &logLen);
|
||||
if (logLen > 0)
|
||||
{
|
||||
char* log = (char*) malloc(logLen * sizeof(char));
|
||||
// Show any errors as appropriate
|
||||
glGetProgramInfoLog(programId, logLen, &logLen, log);
|
||||
std::cout << "Prog Info Log: " << std::endl << log << std::endl;
|
||||
free(log);
|
||||
}
|
||||
}
|
||||
if (vtxShaderId) glDeleteShader(vtxShaderId);
|
||||
if (fragShaderId) glDeleteShader(fragShaderId);
|
||||
return programId;
|
||||
}
|
||||
|
||||
void init(const char* vshader, const char* fshader)
|
||||
{
|
||||
int filesize;
|
||||
char *vshaderfile = mini::file::getfilebuffer(vshader, filesize, true);
|
||||
|
||||
char *fshaderfile = nullptr;
|
||||
if (fshader) { fshaderfile = mini::file::getfilebuffer(fshader, filesize, true); }
|
||||
|
||||
SDL_GetWindowSize(video::window, &win_size.x, &win_size.y);
|
||||
SDL_GetTextureSize(video::tex_shader, &tex_size.x, &tex_size.y);
|
||||
|
||||
SDL_PropertiesID props = SDL_GetTextureProperties(video::tex_shader);
|
||||
int access = SDL_GetNumberProperty(props, SDL_PROP_TEXTURE_ACCESS_NUMBER, -1);
|
||||
if (access != SDL_TEXTUREACCESS_TARGET) {
|
||||
std::cout << "ERROR FATAL: La textura per al render ha de tindre SDL_TEXTUREACCESS_TARGET definit." << std::endl;
|
||||
::exit(1);
|
||||
}
|
||||
const char * renderer_name = SDL_GetRendererName(backend::video::renderer);
|
||||
if(!strncmp(renderer_name, "opengl", 6)) {
|
||||
#ifndef __APPLE__
|
||||
static bool gl_extensions_initialized = false;
|
||||
if (!gl_extensions_initialized) {
|
||||
if (!initGLExtensions()) {
|
||||
std::cout << "WARNING: No s'han pogut inicialitzar les extensions d'OpenGL!" << std::endl;
|
||||
can_use_opengl = false;
|
||||
return;
|
||||
}
|
||||
gl_extensions_initialized = true;
|
||||
}
|
||||
#endif
|
||||
// Compilar el shader y dejarlo listo para usar.
|
||||
if (!vshaderfile) {
|
||||
can_use_opengl = false;
|
||||
return;
|
||||
}
|
||||
programId = compileProgram(vshaderfile, fshaderfile);
|
||||
} else {
|
||||
std::cout << "WARNING: El driver del renderer no es OpenGL." << std::endl;
|
||||
can_use_opengl = false;
|
||||
return;
|
||||
}
|
||||
can_use_opengl = true;
|
||||
return;
|
||||
}
|
||||
|
||||
void enable() { if (can_use_opengl) using_opengl = true; }
|
||||
void disable() { using_opengl = false; }
|
||||
|
||||
void render()
|
||||
{
|
||||
SDL_FlushRenderer(backend::video::renderer);
|
||||
SDL_SetRenderTarget(backend::video::renderer, NULL);
|
||||
SDL_SetRenderDrawColor(backend::video::renderer, 0, 0, 0, 255);
|
||||
SDL_RenderClear(backend::video::renderer);
|
||||
SDL_FlushRenderer(backend::video::renderer);
|
||||
|
||||
if (using_opengl)
|
||||
{
|
||||
GLint oldProgramId;
|
||||
if (programId != 0)
|
||||
{
|
||||
glGetIntegerv(GL_CURRENT_PROGRAM, &oldProgramId);
|
||||
glUseProgram(programId);
|
||||
}
|
||||
|
||||
glEnable(GL_TEXTURE_2D);
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_2D, 1);
|
||||
glViewport(0, 0, win_size.x, win_size.y);
|
||||
|
||||
glBegin(GL_TRIANGLE_STRIP);
|
||||
glTexCoord2f(0.0f, 0.0f);
|
||||
glVertex2f(-1.0f, -1.0f);
|
||||
glTexCoord2f(tex_size.x, 0.0f);
|
||||
glVertex2f(1.0f, -1.0f);
|
||||
glTexCoord2f(0.0f, tex_size.y);
|
||||
glVertex2f(-1.0f, 1.0f);
|
||||
glTexCoord2f(tex_size.x, tex_size.y);
|
||||
glVertex2f(1.0f, 1.0f);
|
||||
glEnd();
|
||||
|
||||
SDL_GL_SwapWindow(backend::video::window);
|
||||
|
||||
if (programId != 0) glUseProgram(oldProgramId);
|
||||
|
||||
} else {
|
||||
SDL_RenderTexture(backend::video::renderer, backend::video::tex_shader, NULL, NULL);
|
||||
SDL_RenderPresent(backend::video::renderer);
|
||||
}
|
||||
if (glGetError()) { printf("GLERROR!\n"); ::exit(1); }
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -43,11 +43,6 @@ namespace backend
|
||||
// Crear textura shaders i inicialitzar shaders
|
||||
tex_shader = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, width*zoom, height*zoom);
|
||||
SDL_SetTextureScaleMode(tex_shader, SDL_SCALEMODE_NEAREST);
|
||||
|
||||
mini::shader::init(window, tex_shader, nullptr);
|
||||
|
||||
// [TODO]
|
||||
//log_msg(LOG_OK, "Graphics subsystem initialized\n");
|
||||
}
|
||||
|
||||
void quit() {
|
||||
@@ -72,7 +67,7 @@ namespace backend
|
||||
SDL_UnlockTexture(tex_back);
|
||||
SDL_RenderTexture(renderer, tex_back, NULL, NULL); //NEW
|
||||
|
||||
mini::shader::render();
|
||||
backend::shader::render();
|
||||
|
||||
//SDL_RenderTexture(mini_ren, mini_bak, NULL, NULL);
|
||||
//SDL_RenderPresent(mini_ren);
|
||||
|
||||
@@ -33,10 +33,11 @@ namespace backend
|
||||
void cursor(const bool value);
|
||||
}
|
||||
|
||||
namespace audio
|
||||
namespace shader
|
||||
{
|
||||
void init();
|
||||
void quit();
|
||||
void init(const char* vshader, const char* fshader);
|
||||
void enable();
|
||||
void disable();
|
||||
void render();
|
||||
}
|
||||
|
||||
@@ -74,4 +75,54 @@ namespace backend
|
||||
int press();
|
||||
}
|
||||
}
|
||||
|
||||
namespace audio
|
||||
{
|
||||
void init();
|
||||
void quit();
|
||||
|
||||
namespace music
|
||||
{
|
||||
enum state { invalid=0, playing=1, paused=2, stopped=3, disabled=4 };
|
||||
|
||||
int load(const char* filename);
|
||||
int load(const uint8_t* buffer, uint32_t length);
|
||||
void play(int mus, int loop = -1);
|
||||
void pause();
|
||||
void resume();
|
||||
void stop();
|
||||
void fadeOut(int milliseconds);
|
||||
state getState();
|
||||
void destroy(int mus);
|
||||
float setVolume(float vol);
|
||||
void setPosition(float value);
|
||||
float getPosition();
|
||||
float getDuration();
|
||||
void enable(bool value);
|
||||
bool isEnabled();
|
||||
}
|
||||
|
||||
namespace sound
|
||||
{
|
||||
int create(uint8_t* buffer, uint32_t length);
|
||||
int load(uint8_t* buffer, uint32_t length);
|
||||
int load(const char* filename);
|
||||
int play(int snd, int loop = 0);
|
||||
int playOnChannel(int snd, int chan, int loop = 0);
|
||||
void destroy(int snd);
|
||||
float setVolume(float vol);
|
||||
void enable(bool value);
|
||||
bool isEnabled();
|
||||
|
||||
namespace channel
|
||||
{
|
||||
enum state { invalid, free, playing, paused, disabled };
|
||||
void pause(int chan);
|
||||
void resume(int chan);
|
||||
void stop(int chan);
|
||||
state getState(int chan);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Vendored
+11
-2
@@ -649,6 +649,13 @@ static void findloader (lua_State *L, const char *name) {
|
||||
// [RZC 12/03/2026] ==================================
|
||||
// Soport per a rutes relatives i absolutes
|
||||
//
|
||||
static void safe_concat3(char *out, size_t outsz, const char *a, const char *b, const char *c) {
|
||||
out[0] = '\0';
|
||||
strncat(out, a, outsz - 1);
|
||||
strncat(out, b, outsz - strlen(out) - 1);
|
||||
strncat(out, c, outsz - strlen(out) - 1);
|
||||
}
|
||||
|
||||
static void resolve_module_name(lua_State *L, char *out, size_t outsz) {
|
||||
const char *req = luaL_checkstring(L, 1);
|
||||
|
||||
@@ -730,7 +737,8 @@ static void resolve_module_name(lua_State *L, char *out, size_t outsz) {
|
||||
// Hemos llegado a la raíz
|
||||
strncpy(out, rest, outsz - 1);
|
||||
} else {
|
||||
snprintf(out, outsz, "%s.%s", temp, rest);
|
||||
//snprintf(out, outsz, "%s.%s", temp, rest);
|
||||
safe_concat3(out, outsz, temp, ".", rest);
|
||||
}
|
||||
|
||||
out[outsz - 1] = '\0';
|
||||
@@ -742,7 +750,8 @@ static void resolve_module_name(lua_State *L, char *out, size_t outsz) {
|
||||
// Estamos en la raíz
|
||||
strncpy(out, req, outsz - 1);
|
||||
} else {
|
||||
snprintf(out, outsz, "%s.%s", caller, req);
|
||||
//snprintf(out, outsz, "%s.%s", caller, req);
|
||||
safe_concat3(out, outsz, caller, ".", req);
|
||||
}
|
||||
|
||||
out[outsz - 1] = '\0';
|
||||
|
||||
Vendored
+3
-2
@@ -1401,12 +1401,13 @@ static int set_file_offset(stb_vorbis *f, unsigned int loc)
|
||||
#endif
|
||||
f->eof = 0;
|
||||
if (USE_MEMORY(f)) {
|
||||
if (f->stream_start + loc >= f->stream_end || f->stream_start + loc < f->stream_start) {
|
||||
uint8_t *pos = f->stream_start + loc;
|
||||
if (pos >= f->stream_end || pos < f->stream_start) {
|
||||
f->stream = f->stream_end;
|
||||
f->eof = 1;
|
||||
return 0;
|
||||
} else {
|
||||
f->stream = f->stream_start + loc;
|
||||
f->stream = pos;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
+27
-24
@@ -1,5 +1,5 @@
|
||||
#include "audio.h"
|
||||
#include "jail_audio.h"
|
||||
#include "backends/backend.h"
|
||||
#include "mini/file/file.h"
|
||||
|
||||
namespace mini
|
||||
@@ -7,18 +7,13 @@ namespace mini
|
||||
namespace audio
|
||||
{
|
||||
int current_music = -1;
|
||||
//#define MAX_SOUNDS 50
|
||||
//JA_Sound_t *sounds[MAX_SOUNDS];
|
||||
|
||||
void init() {
|
||||
jail::audio::init();
|
||||
//for (int i=0;i<MAX_SOUNDS;++i) sounds[i] = NULL;
|
||||
backend::audio::init();
|
||||
}
|
||||
|
||||
void quit() {
|
||||
//if (current_music != NULL) JA_DeleteMusic(current_music);
|
||||
//for (int i=0;i<MAX_SOUNDS;++i) if (sounds[i]!=NULL) JA_DeleteSound(sounds[i]);
|
||||
jail::audio::quit();
|
||||
backend::audio::quit();
|
||||
}
|
||||
|
||||
namespace music
|
||||
@@ -26,45 +21,53 @@ namespace mini
|
||||
void play(const char *filename, const int loop) {
|
||||
int size;
|
||||
char *buffer = file::getfilebuffer(filename, size);
|
||||
if (current_music != -1) jail::audio::music::destroy(current_music);
|
||||
current_music = jail::audio::music::load((uint8_t*)buffer, size);
|
||||
jail::audio::music::play(current_music, loop);
|
||||
if (current_music != -1) backend::audio::music::destroy(current_music);
|
||||
current_music = backend::audio::music::load((uint8_t*)buffer, size);
|
||||
backend::audio::music::play(current_music, loop);
|
||||
}
|
||||
|
||||
void pause() {
|
||||
jail::audio::music::pause();
|
||||
backend::audio::music::pause();
|
||||
}
|
||||
|
||||
void resume() {
|
||||
jail::audio::music::resume();
|
||||
backend::audio::music::resume();
|
||||
}
|
||||
|
||||
void stop(const int t) {
|
||||
jail::audio::music::stop();
|
||||
backend::audio::music::stop();
|
||||
}
|
||||
|
||||
float duration() {
|
||||
return backend::audio::music::getDuration();
|
||||
}
|
||||
|
||||
int state() {
|
||||
return backend::audio::music::getState();
|
||||
}
|
||||
|
||||
namespace pos {
|
||||
void set(float value)
|
||||
{
|
||||
jail::audio::music::setPosition(value);
|
||||
backend::audio::music::setPosition(value);
|
||||
}
|
||||
|
||||
float get()
|
||||
{
|
||||
return jail::audio::music::getPosition();
|
||||
return backend::audio::music::getPosition();
|
||||
}
|
||||
}
|
||||
|
||||
namespace enable {
|
||||
void set(const bool value)
|
||||
{
|
||||
jail::audio::music::enable(value);
|
||||
backend::audio::music::enable(value);
|
||||
file::setconfigvalue("music", value?"true":"false");
|
||||
}
|
||||
|
||||
const bool get()
|
||||
{
|
||||
return jail::audio::music::isEnabled();
|
||||
return backend::audio::music::isEnabled();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -74,32 +77,32 @@ namespace mini
|
||||
int load(const char *filename) {
|
||||
int size;
|
||||
char *buffer = file::getfilebuffer(filename, size);
|
||||
return jail::audio::sound::load((uint8_t*)buffer, size);
|
||||
return backend::audio::sound::load((uint8_t*)buffer, size);
|
||||
}
|
||||
|
||||
void free(int soundfile) {
|
||||
return jail::audio::sound::destroy(soundfile);
|
||||
return backend::audio::sound::destroy(soundfile);
|
||||
}
|
||||
|
||||
int play(int soundfile, const int volume) {
|
||||
// [TODO] Ficar el volumen
|
||||
return jail::audio::sound::play(soundfile, 0);
|
||||
return backend::audio::sound::play(soundfile, 0);
|
||||
}
|
||||
|
||||
void stop(int soundchannel) {
|
||||
return jail::audio::sound::channel::stop(soundchannel);
|
||||
return backend::audio::sound::channel::stop(soundchannel);
|
||||
}
|
||||
|
||||
namespace enable {
|
||||
void set(const bool value)
|
||||
{
|
||||
return jail::audio::sound::enable(value);
|
||||
return backend::audio::sound::enable(value);
|
||||
file::setconfigvalue("sound", value?"true":"false");
|
||||
}
|
||||
|
||||
const bool get()
|
||||
{
|
||||
return jail::audio::sound::isEnabled();
|
||||
return backend::audio::sound::isEnabled();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,6 +17,8 @@ namespace mini
|
||||
void set(float value);
|
||||
float get();
|
||||
}
|
||||
float duration();
|
||||
int state();
|
||||
namespace enable {
|
||||
void set(const bool value);
|
||||
const bool get();
|
||||
|
||||
@@ -1,57 +0,0 @@
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
|
||||
namespace jail
|
||||
{
|
||||
namespace audio
|
||||
{
|
||||
void init(/*const int freq, const SDL_AudioFormat format, const int channels*/);
|
||||
void quit();
|
||||
|
||||
namespace music
|
||||
{
|
||||
//struct JA_Music_t;
|
||||
enum state { invalid, playing, paused, stopped, disabled };
|
||||
|
||||
int load(const char* filename);
|
||||
int load(const uint8_t* buffer, uint32_t length);
|
||||
void play(int mus, int loop = -1);
|
||||
void pause();
|
||||
void resume();
|
||||
void stop();
|
||||
void fadeOut(int milliseconds);
|
||||
state getState();
|
||||
void destroy(int mus);
|
||||
float setVolume(float vol);
|
||||
void setPosition(float value);
|
||||
float getPosition();
|
||||
void enable(bool value);
|
||||
bool isEnabled();
|
||||
}
|
||||
|
||||
namespace sound
|
||||
{
|
||||
//struct JA_Sound_t;
|
||||
int create(uint8_t* buffer, uint32_t length);
|
||||
int load(uint8_t* buffer, uint32_t length);
|
||||
int load(const char* filename);
|
||||
int play(int snd, int loop = 0);
|
||||
int playOnChannel(int snd, int chan, int loop = 0);
|
||||
void destroy(int snd);
|
||||
float setVolume(float vol);
|
||||
void enable(bool value);
|
||||
bool isEnabled();
|
||||
|
||||
namespace channel
|
||||
{
|
||||
enum state { invalid, free, playing, paused, disabled };
|
||||
void pause(int chan);
|
||||
void resume(int chan);
|
||||
void stop(int chan);
|
||||
state getState(int chan);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+46
-13
@@ -1,6 +1,7 @@
|
||||
#include "lua.h"
|
||||
#include "lua.wrappers.h"
|
||||
#include "lua.debug.h"
|
||||
#include "lua.utils.h"
|
||||
#include "external/lua/lua.hpp"
|
||||
#include "mini/file/file.h"
|
||||
#include "other/log.h"
|
||||
@@ -22,6 +23,38 @@ namespace mini
|
||||
return is_playing;
|
||||
}
|
||||
|
||||
std::string assemble_error_message(const char *lua_error_string) {
|
||||
std::string error = lua_error_string; // tu cadena original
|
||||
|
||||
std::string nombre;
|
||||
int linea = 0;
|
||||
std::string mensaje;
|
||||
|
||||
// 1. Buscar el nombre entre comillas
|
||||
auto p1 = error.find('"');
|
||||
auto p2 = error.find('"', p1 + 1);
|
||||
if (p1 != std::string::npos && p2 != std::string::npos)
|
||||
nombre = error.substr(p1 + 1, p2 - (p1 + 1));
|
||||
|
||||
// 2. Buscar la línea después de "]:"
|
||||
auto p3 = error.find("]:", p2);
|
||||
if (p3 != std::string::npos) {
|
||||
size_t start = p3 + 2;
|
||||
linea = std::stoi(error.substr(start));
|
||||
}
|
||||
|
||||
// 3. Buscar el mensaje después del siguiente ':'
|
||||
auto p4 = error.find(':', p3 + 2);
|
||||
if (p4 != std::string::npos)
|
||||
mensaje = error.substr(p4 + 2); // saltar ": "
|
||||
|
||||
// Ahora tienes:
|
||||
// nombre → "main"
|
||||
// linea → 18
|
||||
// mensaje → "unexpected symbol near '+'"
|
||||
return debug::chunkToPath(nombre) + ":" + std::to_string(linea) + ":" + mensaje;
|
||||
}
|
||||
|
||||
int MiniLoader(lua_State *L) {
|
||||
const char *name = luaL_checkstring(L, 1);
|
||||
|
||||
@@ -58,7 +91,7 @@ namespace mini
|
||||
lua_setfield(L, -2, registerpath.c_str());
|
||||
lua_pop(L, 2);
|
||||
} else {
|
||||
log_msg(LOG_LUALD, "Error cargando %s: %s\n", fullpath.c_str(), lua_tostring(L, -1));
|
||||
log_msg(LOG_LUALD, "Error cargando %s: %s\n", fullpath.c_str(), assemble_error_message(lua_tostring(L, -1)).c_str());
|
||||
lua_pop(L, 1);
|
||||
}
|
||||
|
||||
@@ -85,7 +118,7 @@ namespace mini
|
||||
}
|
||||
|
||||
if (luaL_loadbuffer(L, buffer, size, name)) {
|
||||
log_msg(LOG_LUALD, "%s\n", lua_tostring(L, -1));
|
||||
log_msg(LOG_LUALD, "%s\n", assemble_error_message(lua_tostring(L, -1)).c_str());
|
||||
lua_pop(L, 1);
|
||||
free(buffer);
|
||||
lua_pushnil(L);
|
||||
@@ -107,14 +140,14 @@ namespace mini
|
||||
int size;
|
||||
char* buffer = file::getfilebuffer(main_lua_file, size);
|
||||
if (luaL_loadbuffer(L, buffer, size, "main")) {
|
||||
log_msg(LOG_LUALD, "%s\n", lua_tostring(L, -1));
|
||||
log_msg(LOG_LUALD, "%s\n", assemble_error_message(lua_tostring(L, -1)).c_str());
|
||||
lua_pop(L,1);
|
||||
return;
|
||||
}
|
||||
free(buffer);
|
||||
if (lua_pcall(L,0, LUA_MULTRET, 0)) {
|
||||
//luaL_traceback(L, L, NULL, 1);
|
||||
log_msg(LOG_LUART, "%s\n", lua_tostring(L, -1));
|
||||
log_msg(LOG_LUART, "%s\n", assemble_error_message(lua_tostring(L, -1)).c_str());
|
||||
lua_pop(L,1);
|
||||
return;
|
||||
}
|
||||
@@ -135,7 +168,7 @@ namespace mini
|
||||
debug::init(L);
|
||||
|
||||
//std::thread(debugCommandThread).detach();
|
||||
printf("stdin isatty: %d\n", isatty(0));
|
||||
//printf("stdin isatty: %d\n", isatty(0));
|
||||
is_playing = true;
|
||||
}
|
||||
|
||||
@@ -157,15 +190,15 @@ namespace mini
|
||||
lua_getglobal(L, "mini");
|
||||
lua_getfield(L, -1, "init");
|
||||
|
||||
#if defined(DEBUG) && defined(__linux__)
|
||||
if (debug::is_enabled()) {
|
||||
is_playing = debug::call_and_handle_exceptions(L);
|
||||
#else
|
||||
} else {
|
||||
if (lua_pcall(L, 0, 0, 0)) {
|
||||
log_msg(LOG_LUART, "%s\n", lua_tostring(L, -1));
|
||||
log_msg(LOG_LUART, "%s\n", assemble_error_message(lua_tostring(L, -1)).c_str());
|
||||
lua_pop(L,1);
|
||||
is_playing = false;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
void update() {
|
||||
@@ -173,15 +206,15 @@ namespace mini
|
||||
lua_getglobal(L, "mini");
|
||||
lua_getfield(L, -1, "update");
|
||||
|
||||
#if defined(DEBUG) && defined(__linux__)
|
||||
if (debug::is_enabled()) {
|
||||
is_playing = debug::call_and_handle_exceptions(L);
|
||||
#else
|
||||
} else {
|
||||
if (lua_pcall(L, 0, 0, 0)) {
|
||||
log_msg(LOG_LUART, "%s\n", lua_tostring(L, -1));
|
||||
log_msg(LOG_LUART, "%s\n", assemble_error_message(lua_tostring(L, -1)).c_str());
|
||||
lua_pop(L,1);
|
||||
is_playing = false;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#if defined(DEBUG) && defined(__linux__)
|
||||
|
||||
#include "lua.debug.h"
|
||||
#include "lua.utils.h"
|
||||
|
||||
#include "external/lua/lua.hpp"
|
||||
#include "mini/win/win.h"
|
||||
|
||||
@@ -63,7 +64,7 @@ namespace mini
|
||||
int g_pauseLine = 0;
|
||||
bool function_has_breakpoints = false;
|
||||
std::stack<bool> funBreakStack;
|
||||
bool debug_enabled = true;
|
||||
bool debug_enabled = false;
|
||||
|
||||
enum StepMode {
|
||||
STEP_NONE,
|
||||
@@ -77,52 +78,6 @@ namespace mini
|
||||
|
||||
using json = nlohmann::json;
|
||||
|
||||
std::string pathToChunk(const std::string& path) {
|
||||
std::filesystem::path p(path);
|
||||
|
||||
// 1. Normalizar la ruta
|
||||
p = p.lexically_normal();
|
||||
|
||||
// 2. Buscar el directorio "data"
|
||||
auto it = std::find(p.begin(), p.end(), "data");
|
||||
if (it == p.end())
|
||||
return ""; // no es un script Lua válido
|
||||
|
||||
// 3. Construir la parte relativa después de "data"
|
||||
std::filesystem::path rel;
|
||||
for (++it; it != p.end(); ++it)
|
||||
rel /= *it;
|
||||
|
||||
// 4. Quitar ".lua"
|
||||
std::string s = rel.string();
|
||||
if (s.ends_with(".lua"))
|
||||
s = s.substr(0, s.size() - 4);
|
||||
|
||||
// 5. Convertir "/" → "."
|
||||
for (char& c : s)
|
||||
if (c == '/')
|
||||
c = '.';
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
std::string chunkToPath(const std::string& chunk) {
|
||||
// 1. Convertir "ia.test" → "ia/test"
|
||||
std::string rel;
|
||||
rel.reserve(chunk.size() + 10);
|
||||
|
||||
for (char c : chunk)
|
||||
rel += (c == '.' ? '/' : c);
|
||||
|
||||
// 2. Añadir prefijo y sufijo
|
||||
rel = "data/" + rel + ".lua";
|
||||
|
||||
// 3. Convertir a ruta absoluta
|
||||
std::filesystem::path abs = std::filesystem::current_path() / rel;
|
||||
|
||||
return abs.lexically_normal().string();
|
||||
}
|
||||
|
||||
int getStackDepth(lua_State* L) {
|
||||
lua_Debug ar;
|
||||
int depth = 0;
|
||||
@@ -880,8 +835,11 @@ namespace mini
|
||||
|
||||
void processDebugCommand(lua_State* L, const std::string& line) {
|
||||
//printf("COMANDO PROCESADO: %s\n", line.c_str());
|
||||
if (!line.starts_with("@@DEBUGCMD@@"))
|
||||
if (!line.starts_with("@@DEBUGCMD@@")) {
|
||||
disable(L);
|
||||
return;
|
||||
}
|
||||
enable(L);
|
||||
|
||||
json j = json::parse(line.substr(12));
|
||||
|
||||
@@ -1348,7 +1306,7 @@ namespace mini
|
||||
}
|
||||
|
||||
void init(lua_State* L) {
|
||||
lua_sethook(L, luaHook, LUA_MASKCALL | LUA_MASKRET, 0);
|
||||
//lua_sethook(L, luaHook, LUA_MASKCALL | LUA_MASKRET, 0);
|
||||
}
|
||||
|
||||
void kill_thread() {
|
||||
@@ -1366,6 +1324,19 @@ namespace mini
|
||||
}
|
||||
}
|
||||
|
||||
void enable(lua_State* L) {
|
||||
if (debug_enabled) return;
|
||||
debug_enabled = true;
|
||||
lua_sethook(L, luaHook, LUA_MASKCALL | LUA_MASKRET, 0);
|
||||
}
|
||||
|
||||
void disable(lua_State* L) {
|
||||
if (!debug_enabled) return;
|
||||
debug_enabled = false;
|
||||
lua_sethook(L, NULL,0, 0);
|
||||
}
|
||||
|
||||
bool is_enabled() { return debug_enabled; }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
struct lua_State;
|
||||
|
||||
namespace mini
|
||||
@@ -13,12 +14,18 @@ namespace mini
|
||||
void kill_thread();
|
||||
void process_commands(lua_State* L);
|
||||
void toggle(lua_State* L);
|
||||
void enable(lua_State* L);
|
||||
void disable(lua_State* L);
|
||||
bool is_enabled();
|
||||
#else
|
||||
bool call_and_handle_exceptions(lua_State* L) { return true; };
|
||||
void init(lua_State* L) {};
|
||||
void kill_thread() {};
|
||||
void process_commands(lua_State* L) {};
|
||||
void toggle(lua_State* L) {};
|
||||
void enable(lua_State* L) {};
|
||||
void disable(lua_State* L) {};
|
||||
bool is_enabled() { return false; };
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
#include "lua.utils.h"
|
||||
#include <string>
|
||||
#include <filesystem>
|
||||
#include <algorithm>
|
||||
|
||||
namespace mini
|
||||
{
|
||||
namespace lua
|
||||
{
|
||||
namespace debug
|
||||
{
|
||||
std::string pathToChunk(const std::string& path) {
|
||||
std::filesystem::path p(path);
|
||||
|
||||
// 1. Normalizar la ruta
|
||||
p = p.lexically_normal();
|
||||
|
||||
// 2. Buscar el directorio "data"
|
||||
auto it = std::find(p.begin(), p.end(), "data");
|
||||
if (it == p.end())
|
||||
return ""; // no es un script Lua válido
|
||||
|
||||
// 3. Construir la parte relativa después de "data"
|
||||
std::filesystem::path rel;
|
||||
for (++it; it != p.end(); ++it)
|
||||
rel /= *it;
|
||||
|
||||
// 4. Quitar ".lua"
|
||||
std::string s = rel.string();
|
||||
if (s.ends_with(".lua"))
|
||||
s = s.substr(0, s.size() - 4);
|
||||
|
||||
// 5. Convertir "/" → "."
|
||||
for (char& c : s)
|
||||
if (c == '/')
|
||||
c = '.';
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
std::string chunkToPath(const std::string& chunk) {
|
||||
// 1. Convertir "ia.test" → "ia/test"
|
||||
std::string rel;
|
||||
rel.reserve(chunk.size() + 10);
|
||||
|
||||
for (char c : chunk)
|
||||
rel += (c == '.' ? '/' : c);
|
||||
|
||||
// 2. Añadir prefijo y sufijo
|
||||
rel = "data/" + rel + ".lua";
|
||||
|
||||
// 3. Convertir a ruta absoluta
|
||||
std::filesystem::path abs = std::filesystem::current_path() / rel;
|
||||
|
||||
return abs.lexically_normal().string();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
|
||||
namespace mini
|
||||
{
|
||||
namespace lua
|
||||
{
|
||||
namespace debug
|
||||
{
|
||||
std::string pathToChunk(const std::string& path);
|
||||
std::string chunkToPath(const std::string& chunk);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -154,12 +154,12 @@ namespace mini
|
||||
static int target(lua_State *L) {
|
||||
const int numargs = lua_gettop(L);
|
||||
switch (numargs) {
|
||||
case 0: {
|
||||
case 1: {
|
||||
lua_pushinteger(L, mini::surf::target::get());
|
||||
return 1;
|
||||
}
|
||||
case 1: {
|
||||
uint8_t surface = luaL_checkinteger(L, 1);
|
||||
case 2: {
|
||||
uint8_t surface = luaL_checkinteger(L, 2);
|
||||
mini::surf::target::set(surface);
|
||||
return 0;
|
||||
}
|
||||
@@ -168,15 +168,31 @@ namespace mini
|
||||
};
|
||||
}
|
||||
|
||||
static int target_push(lua_State *L) {
|
||||
const int numargs = lua_gettop(L);
|
||||
if (numargs==1) {
|
||||
uint8_t surface = luaL_checkinteger(L, 1);
|
||||
mini::surf::target::push(surface);
|
||||
return 0;
|
||||
} else {
|
||||
return luaL_error(L, "Function 'surface.target.push' Unexpected number of parameters.");
|
||||
};
|
||||
}
|
||||
|
||||
static int target_pop(lua_State *L) {
|
||||
mini::surf::target::pop();
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int source(lua_State *L) {
|
||||
const int numargs = lua_gettop(L);
|
||||
switch (numargs) {
|
||||
case 0: {
|
||||
case 1: {
|
||||
lua_pushinteger(L, mini::surf::source::get());
|
||||
return 1;
|
||||
}
|
||||
case 1: {
|
||||
uint8_t surface = luaL_checkinteger(L, 1);
|
||||
case 2: {
|
||||
uint8_t surface = luaL_checkinteger(L, 2);
|
||||
mini::surf::source::set(surface);
|
||||
return 0;
|
||||
}
|
||||
@@ -185,6 +201,22 @@ namespace mini
|
||||
};
|
||||
}
|
||||
|
||||
static int source_push(lua_State *L) {
|
||||
const int numargs = lua_gettop(L);
|
||||
if (numargs==1) {
|
||||
uint8_t surface = luaL_checkinteger(L, 1);
|
||||
mini::surf::source::push(surface);
|
||||
return 0;
|
||||
} else {
|
||||
return luaL_error(L, "Function 'surface.source.push' Unexpected number of parameters.");
|
||||
};
|
||||
}
|
||||
|
||||
static int source_pop(lua_State *L) {
|
||||
mini::surf::source::pop();
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int cls(lua_State *L) {
|
||||
uint8_t color = luaL_optinteger(L, 1, 0);
|
||||
mini::surf::cls(color);
|
||||
@@ -629,6 +661,16 @@ namespace mini
|
||||
}
|
||||
}
|
||||
|
||||
static int duration(lua_State *L) {
|
||||
lua_pushnumber(L, mini::audio::music::duration());
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int state(lua_State *L) {
|
||||
lua_pushinteger(L, mini::audio::music::state());
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int enable(lua_State *L) {
|
||||
if (lua_gettop(L) == 0) {
|
||||
lua_pushboolean(L, mini::audio::music::enable::get());
|
||||
@@ -1036,8 +1078,23 @@ namespace mini
|
||||
lua_pushcfunction(L,wrappers::surf::save); lua_setfield(L, -2, "save");
|
||||
lua_pushcfunction(L,wrappers::surf::free); lua_setfield(L, -2, "free");
|
||||
lua_pushcfunction(L,wrappers::surf::size); lua_setfield(L, -2, "size");
|
||||
lua_pushcfunction(L,wrappers::surf::target); lua_setfield(L, -2, "target");
|
||||
lua_pushcfunction(L,wrappers::surf::source); lua_setfield(L, -2, "source");
|
||||
|
||||
lua_newtable(L);
|
||||
lua_pushcfunction(L, wrappers::surf::target_push);lua_setfield(L, -2, "push");
|
||||
lua_pushcfunction(L, wrappers::surf::target_pop); lua_setfield(L, -2, "pop");
|
||||
lua_newtable(L);
|
||||
lua_pushcfunction(L, wrappers::surf::target); lua_setfield(L, -2, "__call");
|
||||
lua_setmetatable(L, -2);
|
||||
lua_setfield(L, -2, "target");
|
||||
|
||||
lua_newtable(L);
|
||||
lua_pushcfunction(L, wrappers::surf::source_push);lua_setfield(L, -2, "push");
|
||||
lua_pushcfunction(L, wrappers::surf::source_pop); lua_setfield(L, -2, "pop");
|
||||
lua_newtable(L);
|
||||
lua_pushcfunction(L, wrappers::surf::source); lua_setfield(L, -2, "__call");
|
||||
lua_setmetatable(L, -2);
|
||||
lua_setfield(L, -2, "source");
|
||||
|
||||
lua_pushcfunction(L,wrappers::surf::cls); lua_setfield(L, -2, "cls");
|
||||
lua_pushcfunction(L,wrappers::surf::clip); lua_setfield(L, -2, "clip");
|
||||
|
||||
@@ -1104,7 +1161,16 @@ namespace mini
|
||||
lua_pushcfunction(L,wrappers::music::resume); lua_setfield(L, -2, "resume");
|
||||
lua_pushcfunction(L,wrappers::music::stop); lua_setfield(L, -2, "stop");
|
||||
lua_pushcfunction(L,wrappers::music::pos); lua_setfield(L, -2, "pos");
|
||||
lua_pushcfunction(L,wrappers::music::duration); lua_setfield(L, -2, "duration");
|
||||
lua_pushcfunction(L,wrappers::music::state); lua_setfield(L, -2, "state");
|
||||
lua_pushcfunction(L,wrappers::music::enable); lua_setfield(L, -2, "enabled");
|
||||
|
||||
lua_pushinteger(L, 0); lua_setfield(L, -2, "INVALID");
|
||||
lua_pushinteger(L, 1); lua_setfield(L, -2, "PLAYING");
|
||||
lua_pushinteger(L, 2); lua_setfield(L, -2, "PAUSED");
|
||||
lua_pushinteger(L, 3); lua_setfield(L, -2, "STOPPED");
|
||||
lua_pushinteger(L, 4); lua_setfield(L, -2, "DISABLED");
|
||||
|
||||
lua_setglobal(L, "music");
|
||||
|
||||
lua_newtable(L);
|
||||
|
||||
@@ -1,268 +1,20 @@
|
||||
#include "shader.h"
|
||||
#include "mini/file/file.h"
|
||||
#include "mini/win/win.h"
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#ifdef __APPLE__
|
||||
#include "CoreFoundation/CoreFoundation.h"
|
||||
#include <OpenGL/OpenGL.h>
|
||||
|
||||
#if ESSENTIAL_GL_PRACTICES_SUPPORT_GL3
|
||||
#include <OpenGL/gl3.h>
|
||||
#else
|
||||
#include <OpenGL/gl.h>
|
||||
#endif //!ESSENTIAL_GL_PRACTICES_SUPPORT_GL3
|
||||
#else
|
||||
#include <SDL3/SDL_opengl.h>
|
||||
#include <SDL3/SDL_opengl_glext.h>
|
||||
#endif
|
||||
#include "backends/backend.h"
|
||||
|
||||
namespace mini
|
||||
{
|
||||
namespace shader
|
||||
{
|
||||
SDL_Window *win = nullptr;
|
||||
SDL_Renderer *renderer = nullptr;
|
||||
GLuint programId = 0;
|
||||
SDL_Texture* backBuffer = nullptr;
|
||||
SDL_Point win_size = {640, 480};
|
||||
SDL_FPoint tex_size = {320, 240};
|
||||
bool can_use_opengl = false;
|
||||
bool using_opengl = false;
|
||||
GLuint texture_number;
|
||||
GLuint nose;
|
||||
|
||||
#ifndef __APPLE__
|
||||
|
||||
// I'm avoiding the use of GLEW or some extensions handler, but that
|
||||
// doesn't mean you should...
|
||||
PFNGLCREATESHADERPROC glCreateShader;
|
||||
PFNGLSHADERSOURCEPROC glShaderSource;
|
||||
PFNGLCOMPILESHADERPROC glCompileShader;
|
||||
PFNGLGETSHADERIVPROC glGetShaderiv;
|
||||
PFNGLGETSHADERINFOLOGPROC glGetShaderInfoLog;
|
||||
PFNGLDELETESHADERPROC glDeleteShader;
|
||||
PFNGLATTACHSHADERPROC glAttachShader;
|
||||
PFNGLCREATEPROGRAMPROC glCreateProgram;
|
||||
PFNGLDELETEPROGRAMPROC glDeleteProgram;
|
||||
PFNGLLINKPROGRAMPROC glLinkProgram;
|
||||
PFNGLVALIDATEPROGRAMPROC glValidateProgram;
|
||||
PFNGLGETPROGRAMIVPROC glGetProgramiv;
|
||||
PFNGLGETPROGRAMINFOLOGPROC glGetProgramInfoLog;
|
||||
PFNGLUSEPROGRAMPROC glUseProgram;
|
||||
PFNGLGETUNIFORMLOCATIONPROC glGetUniformLocation;
|
||||
PFNGLACTIVETEXTUREPROC glActiveTexture;
|
||||
|
||||
|
||||
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");
|
||||
glDeleteProgram = (PFNGLDELETEPROGRAMPROC)SDL_GL_GetProcAddress("glDeleteProgram");
|
||||
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");
|
||||
glGetUniformLocation = (PFNGLGETUNIFORMLOCATIONPROC)SDL_GL_GetProcAddress("glGetUniformLocation");
|
||||
glActiveTexture = (PFNGLACTIVETEXTUREPROC)SDL_GL_GetProcAddress("glActiveTexture");
|
||||
|
||||
return glCreateShader && glShaderSource && glCompileShader && glGetShaderiv &&
|
||||
glGetShaderInfoLog && glDeleteShader && glAttachShader && glCreateProgram &&
|
||||
glDeleteProgram && glLinkProgram && glValidateProgram && glGetProgramiv &&
|
||||
glGetProgramInfoLog && glUseProgram && glGetUniformLocation;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
state_t state;
|
||||
|
||||
void init(const char* vshader, const char* fshader)
|
||||
{
|
||||
// [TODO]
|
||||
//int filesize;
|
||||
//char *vshaderfile = file::getfilebuffer(vshader, filesize, true);
|
||||
//
|
||||
//char *fshaderfile = nullptr;
|
||||
//if (fshader) { fshaderfile = file::getfilebuffer(fshader, filesize, true); }
|
||||
//init(win::state.window, win::state.tex_shader, vshaderfile, fshaderfile);
|
||||
void init(const char* vshader, const char* fshader) {
|
||||
backend::shader::init(vshader, fshader);
|
||||
}
|
||||
|
||||
|
||||
GLuint compileShader(const char* source, GLuint shaderType) {
|
||||
// Create ID for shader
|
||||
GLuint result = glCreateShader(shaderType);
|
||||
// Add define depending on shader type
|
||||
const char *sources[2] = { shaderType==GL_VERTEX_SHADER?"#define VERTEX\n":"#define FRAGMENT\n", source };
|
||||
// Define shader text
|
||||
glShaderSource(result, 2, sources, NULL);
|
||||
// Compile shader
|
||||
glCompileShader(result);
|
||||
|
||||
//Check vertex shader for errors
|
||||
GLint shaderCompiled = GL_FALSE;
|
||||
glGetShaderiv( result, GL_COMPILE_STATUS, &shaderCompiled );
|
||||
if (shaderCompiled != GL_TRUE)
|
||||
{
|
||||
std::cout << "Error en la compilación: " << result << "!" << std::endl;
|
||||
GLint logLength;
|
||||
glGetShaderiv(result, GL_INFO_LOG_LENGTH, &logLength);
|
||||
if (logLength > 0)
|
||||
{
|
||||
GLchar *log = (GLchar*)malloc(logLength);
|
||||
glGetShaderInfoLog(result, logLength, &logLength, log);
|
||||
std::cout << "Shader compile log:" << log << std::endl;
|
||||
//std::cout << source << std::endl;
|
||||
free(log);
|
||||
}
|
||||
glDeleteShader(result);
|
||||
result = 0;
|
||||
}
|
||||
return result;
|
||||
void enable() {
|
||||
backend::shader::enable();
|
||||
}
|
||||
|
||||
GLuint compileProgram(const char* vertexShaderSource, const char* fragmentShaderSource)
|
||||
{
|
||||
GLuint programId = 0;
|
||||
GLuint vtxShaderId, fragShaderId;
|
||||
|
||||
if (programId != 0) glDeleteProgram(programId);
|
||||
programId = glCreateProgram();
|
||||
|
||||
|
||||
vtxShaderId = compileShader(vertexShaderSource, GL_VERTEX_SHADER);
|
||||
fragShaderId = compileShader(fragmentShaderSource?fragmentShaderSource:vertexShaderSource, GL_FRAGMENT_SHADER);
|
||||
|
||||
if(vtxShaderId && fragShaderId)
|
||||
{
|
||||
// Associate shader with program
|
||||
glAttachShader(programId, vtxShaderId);
|
||||
glAttachShader(programId, fragShaderId);
|
||||
glLinkProgram(programId);
|
||||
glValidateProgram(programId);
|
||||
|
||||
// Check the status of the compile/link
|
||||
GLint logLen;
|
||||
glGetProgramiv(programId, GL_INFO_LOG_LENGTH, &logLen);
|
||||
if (logLen > 0)
|
||||
{
|
||||
char* log = (char*) malloc(logLen * sizeof(char));
|
||||
// Show any errors as appropriate
|
||||
glGetProgramInfoLog(programId, logLen, &logLen, log);
|
||||
std::cout << "Prog Info Log: " << std::endl << log << std::endl;
|
||||
free(log);
|
||||
}
|
||||
}
|
||||
if (vtxShaderId) glDeleteShader(vtxShaderId);
|
||||
if (fragShaderId) glDeleteShader(fragShaderId);
|
||||
return programId;
|
||||
void disable() {
|
||||
backend::shader::disable();
|
||||
}
|
||||
|
||||
const bool init(SDL_Window* win, SDL_Texture* backBuffer, const char* vertexShader, const char* fragmentShader)
|
||||
{
|
||||
shader::win = win;
|
||||
shader::renderer = SDL_GetRenderer(win);
|
||||
shader::backBuffer = backBuffer;
|
||||
SDL_GetWindowSize(win, &win_size.x, &win_size.y);
|
||||
SDL_GetTextureSize(backBuffer, &tex_size.x, &tex_size.y);
|
||||
//printf("tex size: %fx%f\n", tex_size.x, tex_size.y);
|
||||
SDL_PropertiesID props = SDL_GetTextureProperties(backBuffer);
|
||||
texture_number = SDL_GetNumberProperty(props, SDL_PROP_TEXTURE_OPENGL_TEXTURE_NUMBER, -1);
|
||||
//printf("texture number: %i\n", texture_number);
|
||||
int access = SDL_GetNumberProperty(props, SDL_PROP_TEXTURE_ACCESS_NUMBER, -1);
|
||||
nose = SDL_GetNumberProperty(props, SDL_PROP_TEXTURE_OPENGL_TEXTURE_TARGET_NUMBER, -1);
|
||||
//printf("texture target number: %i\n", nose);
|
||||
|
||||
if (access != SDL_TEXTUREACCESS_TARGET)
|
||||
{
|
||||
std::cout << "ERROR FATAL: La textura per al render ha de tindre SDL_TEXTUREACCESS_TARGET definit." << std::endl;
|
||||
exit(1);
|
||||
}
|
||||
|
||||
const char * renderer_name = SDL_GetRendererName(renderer);
|
||||
//printf("rendererInfo.name: %s\n", renderer_name);
|
||||
|
||||
if(!strncmp(renderer_name, "opengl", 6)) {
|
||||
#ifndef __APPLE__
|
||||
static bool gl_extensions_initialized = false;
|
||||
if (!gl_extensions_initialized) {
|
||||
if (!initGLExtensions()) {
|
||||
std::cout << "WARNING: No s'han pogut inicialitzar les extensions d'OpenGL!" << std::endl;
|
||||
can_use_opengl = false;
|
||||
return false;
|
||||
}
|
||||
gl_extensions_initialized = true;
|
||||
}
|
||||
#endif
|
||||
// Compilar el shader y dejarlo listo para usar.
|
||||
if (!vertexShader) {
|
||||
can_use_opengl = false;
|
||||
return false;
|
||||
}
|
||||
programId = compileProgram(vertexShader, fragmentShader);
|
||||
} else {
|
||||
std::cout << "WARNING: El driver del renderer no es OpenGL." << std::endl;
|
||||
can_use_opengl = false;
|
||||
return false;
|
||||
}
|
||||
can_use_opengl = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
void enable() { if (can_use_opengl) using_opengl = true; }
|
||||
void disable() { using_opengl = false; }
|
||||
|
||||
void render()
|
||||
{
|
||||
SDL_FlushRenderer(renderer);
|
||||
SDL_SetRenderTarget(renderer, NULL);
|
||||
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
|
||||
SDL_RenderClear(renderer);
|
||||
SDL_FlushRenderer(renderer);
|
||||
|
||||
if (using_opengl)
|
||||
{
|
||||
GLint oldProgramId;
|
||||
if (programId != 0)
|
||||
{
|
||||
glGetIntegerv(GL_CURRENT_PROGRAM, &oldProgramId);
|
||||
glUseProgram(programId);
|
||||
}
|
||||
|
||||
glEnable(GL_TEXTURE_2D);
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_2D, 1);
|
||||
glViewport(0, 0, win_size.x, win_size.y);
|
||||
|
||||
glBegin(GL_TRIANGLE_STRIP);
|
||||
glTexCoord2f(0.0f, 0.0f);
|
||||
glVertex2f(-1.0f, -1.0f);
|
||||
glTexCoord2f(tex_size.x, 0.0f);
|
||||
glVertex2f(1.0f, -1.0f);
|
||||
glTexCoord2f(0.0f, tex_size.y);
|
||||
glVertex2f(-1.0f, 1.0f);
|
||||
glTexCoord2f(tex_size.x, tex_size.y);
|
||||
glVertex2f(1.0f, 1.0f);
|
||||
glEnd();
|
||||
|
||||
SDL_GL_SwapWindow(win);
|
||||
|
||||
if (programId != 0) glUseProgram(oldProgramId);
|
||||
|
||||
} else {
|
||||
SDL_RenderTexture(renderer, backBuffer, NULL, NULL);
|
||||
SDL_RenderPresent(renderer);
|
||||
}
|
||||
if (glGetError()) { printf("GLERROR!\n"); exit(1); }
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,27 +1,13 @@
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
|
||||
struct SDL_Window;
|
||||
struct SDL_Texture;
|
||||
|
||||
namespace mini
|
||||
{
|
||||
namespace shader
|
||||
{
|
||||
struct state_t {
|
||||
const bool *keys;
|
||||
uint8_t just_pressed = 0;
|
||||
char text_input_buffer[10];
|
||||
bool has_text_input = false;
|
||||
};
|
||||
extern state_t state;
|
||||
|
||||
const bool init(SDL_Window* win, SDL_Texture* backBuffer, const char* vertexShader, const char* fragmentShader=nullptr);
|
||||
void init(const char* vshader, const char* fshader);
|
||||
|
||||
void enable();
|
||||
void disable();
|
||||
|
||||
void render();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -176,6 +176,16 @@ namespace mini
|
||||
uint8_t get() {
|
||||
return state.dest_surface;
|
||||
}
|
||||
|
||||
void push(uint8_t surface) {
|
||||
state.dest_stack.push(state.dest_surface);
|
||||
state.dest_surface = surface;
|
||||
}
|
||||
|
||||
void pop() {
|
||||
state.dest_surface = state.dest_stack.top();
|
||||
state.dest_stack.pop();
|
||||
}
|
||||
}
|
||||
|
||||
namespace source {
|
||||
@@ -186,6 +196,16 @@ namespace mini
|
||||
uint8_t get() {
|
||||
return state.source_surface;
|
||||
}
|
||||
|
||||
void push(uint8_t surface) {
|
||||
state.source_stack.push(state.source_surface);
|
||||
state.source_surface = surface;
|
||||
}
|
||||
|
||||
void pop() {
|
||||
state.source_surface = state.source_stack.top();
|
||||
state.source_stack.pop();
|
||||
}
|
||||
}
|
||||
|
||||
namespace clip {
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <stdint.h>
|
||||
#include <vector>
|
||||
#include <stack>
|
||||
|
||||
namespace mini
|
||||
{
|
||||
@@ -27,6 +28,8 @@ namespace mini
|
||||
std::vector<surface_t> surfaces;
|
||||
int dest_surface = -1;
|
||||
int source_surface = -1;
|
||||
std::stack<int> dest_stack;
|
||||
std::stack<int> source_stack;
|
||||
};
|
||||
extern state_t state;
|
||||
|
||||
@@ -46,11 +49,15 @@ namespace mini
|
||||
namespace target {
|
||||
void set(uint8_t surface);
|
||||
uint8_t get();
|
||||
void push(uint8_t surface);
|
||||
void pop();
|
||||
}
|
||||
|
||||
namespace source {
|
||||
void set(uint8_t surface);
|
||||
uint8_t get();
|
||||
void push(uint8_t surface);
|
||||
void pop();
|
||||
}
|
||||
|
||||
namespace clip {
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
#pragma once
|
||||
|
||||
#define MINI_VERSION "1.5.5"
|
||||
#define MINI_VERSION "1.5.13"
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
#include "other/log.h"
|
||||
#include "mini/file/file.h"
|
||||
#include "mini/shader/shader.h"
|
||||
#include <backends/backend.h>
|
||||
#include "backends/backend.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
+4
-4
@@ -2,7 +2,7 @@
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
#ifdef DEBUG
|
||||
//#ifdef DEBUG
|
||||
enum LogLevel { LOG_OK, LOG_FAIL, LOG_WARN, LOG_INFO, LOG_LUART, LOG_LUALD, LOG_VERBOSE, LOG_UNSALTED };
|
||||
|
||||
static inline void log_msg(enum LogLevel level, const char *fmt, ...) {
|
||||
@@ -22,6 +22,6 @@ static inline void log_msg(enum LogLevel level, const char *fmt, ...) {
|
||||
vprintf(fmt, args);
|
||||
va_end(args);
|
||||
}
|
||||
#else
|
||||
#define log_msg(...) ((void)0)
|
||||
#endif
|
||||
//#else
|
||||
//#define log_msg(...) ((void)0)
|
||||
//#endif
|
||||
+36
-2
@@ -38,6 +38,8 @@ function surf.free(surface) end
|
||||
---Retrieve width and height of surface
|
||||
function surf.size(surface) end
|
||||
|
||||
surf.target = {}
|
||||
|
||||
---@return number surface
|
||||
---Get current target surface
|
||||
function surf.target() end
|
||||
@@ -46,6 +48,15 @@ function surf.target() end
|
||||
---Set surface as target
|
||||
function surf.target(surface) end
|
||||
|
||||
---@param surface number
|
||||
---push current target to the stack and set surface as target
|
||||
function surf.target.push(surface) end
|
||||
|
||||
---pop surface on the stack as current target
|
||||
function surf.target.pop() end
|
||||
|
||||
surf.source = {}
|
||||
|
||||
---@return number surface
|
||||
---Get current source surface
|
||||
function surf.source() end
|
||||
@@ -54,6 +65,13 @@ function surf.source() end
|
||||
---Set surface as source
|
||||
function surf.source(surface) end
|
||||
|
||||
---@param surface number
|
||||
---push current source to the stack and set surface as source
|
||||
function surf.source.push(surface) end
|
||||
|
||||
---pop surface on the stack and set it as current source
|
||||
function surf.source.pop() end
|
||||
|
||||
---Erase the current target surface with color 0.
|
||||
function surf.cls() end
|
||||
|
||||
@@ -399,6 +417,14 @@ function music.pos() end
|
||||
---Set the playing position of the currently loaded song
|
||||
function music.pos(pos) end
|
||||
|
||||
---@return number time
|
||||
---Get the duration of the currently loaded song
|
||||
function music.duration() end
|
||||
|
||||
---@return integer value
|
||||
---Get the state of the music
|
||||
function music.state() end
|
||||
|
||||
---@return boolean value
|
||||
---Get if music is enabled
|
||||
function music.enabled() end
|
||||
@@ -407,6 +433,12 @@ function music.enabled() end
|
||||
---Set if music is enabled or not
|
||||
function music.enabled(value) end
|
||||
|
||||
music.INVALID = 0
|
||||
music.PLAYING = 1
|
||||
music.PAUSED = 2
|
||||
music.STOPPED = 3
|
||||
music.DISABLED = 4
|
||||
|
||||
|
||||
---@class sound
|
||||
sound = {}
|
||||
@@ -446,9 +478,11 @@ function sound.enabled(value) end
|
||||
---@class sys
|
||||
sys = {}
|
||||
|
||||
---@return number delta
|
||||
---Get delta time from last update in seconds (with decimals)
|
||||
function sys.delta() end
|
||||
|
||||
---@return number time
|
||||
---Get current system timer in seconds (with decimals)
|
||||
function sys.time() end
|
||||
|
||||
@@ -456,8 +490,8 @@ function sys.time() end
|
||||
---Reset chrono time (with offset in seconds) (with decimals)
|
||||
function sys.chrono(offset) end
|
||||
|
||||
---@return number
|
||||
---@---Get chrono time since last chrono reset in seconds (with decimals)
|
||||
---@return number seconds
|
||||
---Get chrono time since last chrono reset in seconds (with decimals)
|
||||
function sys.chrono() end
|
||||
|
||||
---@return boolean
|
||||
|
||||
Reference in New Issue
Block a user