- [NEW] Passat a SDL3

- [NEW] Passat a de SDL_Mixer a JailAudio
- [NEW] Actualitzat a Respack2
- [NEW] Llevat el data.jrf i afegit el directori 'data' amb els arxius. Molt millor per a developar.
- [CHG] Músiques passades a OGG
- [NEW] Afegit lagueirtofile
This commit is contained in:
2025-06-29 14:18:38 +02:00
parent 7e5318c501
commit 8805799f2e
48 changed files with 6736 additions and 399 deletions

20
.gitignore vendored
View File

@@ -1,22 +1,8 @@
syntax: glob syntax: glob
aee aee
recursos/*
bin/*
obj/*
Debug/*
Release/*
data/*
*.suo
*.sdf
*.opensdf
*.user
*.dll
*.exe
.DS_Store .DS_Store
trick.ini trick.ini
*.xcuserstate .vscode/*
project.xcworkspace/ data.jrf
xcuserdata/ build/*
*.opk
.vscode/*

View File

@@ -1,5 +1,6 @@
#include "bola.h" #include "bola.h"
#include "jgame.h" #include "jgame.h"
#include <stdlib.h>
Bola::Bola( JD8_Surface gfx, Info* info, Prota* sam ) : Sprite( gfx ) { Bola::Bola( JD8_Surface gfx, Info* info, Prota* sam ) : Sprite( gfx ) {
this->info = info; this->info = info;

BIN
data.jrf

Binary file not shown.

BIN
data/00000001.ogg Normal file

Binary file not shown.

BIN
data/00000002.ogg Normal file

Binary file not shown.

BIN
data/00000003.ogg Normal file

Binary file not shown.

BIN
data/00000004.ogg Normal file

Binary file not shown.

BIN
data/00000005.ogg Normal file

Binary file not shown.

BIN
data/00000006.ogg Normal file

Binary file not shown.

BIN
data/00000007.ogg Normal file

Binary file not shown.

BIN
data/00000008.ogg Normal file

Binary file not shown.

234
data/crtpi.glsl Normal file
View File

@@ -0,0 +1,234 @@
/*
crt-pi - A Raspberry Pi friendly CRT shader.
Copyright (C) 2015-2016 davej
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at your option)
any later version.
Notes:
This shader is designed to work well on Raspberry Pi GPUs (i.e. 1080P @ 60Hz on a game with a 4:3 aspect ratio). It pushes the Pi's GPU hard and enabling some features will slow it down so that it is no longer able to match 1080P @ 60Hz. You will need to overclock your Pi to the fastest setting in raspi-config to get the best results from this shader: 'Pi2' for Pi2 and 'Turbo' for original Pi and Pi Zero. Note: Pi2s are slower at running the shader than other Pis, this seems to be down to Pi2s lower maximum memory speed. Pi2s don't quite manage 1080P @ 60Hz - they drop about 1 in 1000 frames. You probably won't notice this, but if you do, try enabling FAKE_GAMMA.
SCANLINES enables scanlines. You'll almost certainly want to use it with MULTISAMPLE to reduce moire effects. SCANLINE_WEIGHT defines how wide scanlines are (it is an inverse value so a higher number = thinner lines). SCANLINE_GAP_BRIGHTNESS defines how dark the gaps between the scan lines are. Darker gaps between scan lines make moire effects more likely.
GAMMA enables gamma correction using the values in INPUT_GAMMA and OUTPUT_GAMMA. FAKE_GAMMA causes it to ignore the values in INPUT_GAMMA and OUTPUT_GAMMA and approximate gamma correction in a way which is faster than true gamma whilst still looking better than having none. You must have GAMMA defined to enable FAKE_GAMMA.
CURVATURE distorts the screen by CURVATURE_X and CURVATURE_Y. Curvature slows things down a lot.
By default the shader uses linear blending horizontally. If you find this too blury, enable SHARPER.
BLOOM_FACTOR controls the increase in width for bright scanlines.
MASK_TYPE defines what, if any, shadow mask to use. MASK_BRIGHTNESS defines how much the mask type darkens the screen.
*/
#pragma parameter CURVATURE_X "Screen curvature - horizontal" 0.10 0.0 1.0 0.01
#pragma parameter CURVATURE_Y "Screen curvature - vertical" 0.15 0.0 1.0 0.01
#pragma parameter MASK_BRIGHTNESS "Mask brightness" 0.70 0.0 1.0 0.01
#pragma parameter SCANLINE_WEIGHT "Scanline weight" 6.0 0.0 15.0 0.1
#pragma parameter SCANLINE_GAP_BRIGHTNESS "Scanline gap brightness" 0.12 0.0 1.0 0.01
#pragma parameter BLOOM_FACTOR "Bloom factor" 1.5 0.0 5.0 0.01
#pragma parameter INPUT_GAMMA "Input gamma" 2.4 0.0 5.0 0.01
#pragma parameter OUTPUT_GAMMA "Output gamma" 2.2 0.0 5.0 0.01
// Haven't put these as parameters as it would slow the code down.
#define SCANLINES
#define MULTISAMPLE
#define GAMMA
//#define FAKE_GAMMA
#define CURVATURE
//#define SHARPER
// MASK_TYPE: 0 = none, 1 = green/magenta, 2 = trinitron(ish)
#define MASK_TYPE 1
#ifdef GL_ES
#define COMPAT_PRECISION mediump
precision mediump float;
#else
#define COMPAT_PRECISION
#endif
#ifdef PARAMETER_UNIFORM
uniform COMPAT_PRECISION float CURVATURE_X;
uniform COMPAT_PRECISION float CURVATURE_Y;
uniform COMPAT_PRECISION float MASK_BRIGHTNESS;
uniform COMPAT_PRECISION float SCANLINE_WEIGHT;
uniform COMPAT_PRECISION float SCANLINE_GAP_BRIGHTNESS;
uniform COMPAT_PRECISION float BLOOM_FACTOR;
uniform COMPAT_PRECISION float INPUT_GAMMA;
uniform COMPAT_PRECISION float OUTPUT_GAMMA;
#else
#define CURVATURE_X 0.25
#define CURVATURE_Y 0.45
#define MASK_BRIGHTNESS 0.70
#define SCANLINE_WEIGHT 6.0
#define SCANLINE_GAP_BRIGHTNESS 0.12
#define BLOOM_FACTOR 1.5
#define INPUT_GAMMA 2.4
#define OUTPUT_GAMMA 2.2
#endif
/* COMPATIBILITY
- GLSL compilers
*/
//uniform vec2 TextureSize;
#if defined(CURVATURE)
varying vec2 screenScale;
#endif
varying vec2 TEX0;
varying float filterWidth;
#if defined(VERTEX)
//uniform mat4 MVPMatrix;
//attribute vec4 VertexCoord;
//attribute vec2 TexCoord;
//uniform vec2 InputSize;
//uniform vec2 OutputSize;
void main()
{
#if defined(CURVATURE)
screenScale = vec2(1.0, 1.0); //TextureSize / InputSize;
#endif
filterWidth = (768.0 / 240.0) / 3.0;
TEX0 = vec2(gl_MultiTexCoord0.x, 1.0-gl_MultiTexCoord0.y)*1.0001;
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}
#elif defined(FRAGMENT)
uniform sampler2D Texture;
#if defined(CURVATURE)
vec2 Distort(vec2 coord)
{
vec2 CURVATURE_DISTORTION = vec2(CURVATURE_X, CURVATURE_Y);
// Barrel distortion shrinks the display area a bit, this will allow us to counteract that.
vec2 barrelScale = 1.0 - (0.23 * CURVATURE_DISTORTION);
coord *= screenScale;
coord -= vec2(0.5);
float rsq = coord.x * coord.x + coord.y * coord.y;
coord += coord * (CURVATURE_DISTORTION * rsq);
coord *= barrelScale;
if (abs(coord.x) >= 0.5 || abs(coord.y) >= 0.5)
coord = vec2(-1.0); // If out of bounds, return an invalid value.
else
{
coord += vec2(0.5);
coord /= screenScale;
}
return coord;
}
#endif
float CalcScanLineWeight(float dist)
{
return max(1.0-dist*dist*SCANLINE_WEIGHT, SCANLINE_GAP_BRIGHTNESS);
}
float CalcScanLine(float dy)
{
float scanLineWeight = CalcScanLineWeight(dy);
#if defined(MULTISAMPLE)
scanLineWeight += CalcScanLineWeight(dy-filterWidth);
scanLineWeight += CalcScanLineWeight(dy+filterWidth);
scanLineWeight *= 0.3333333;
#endif
return scanLineWeight;
}
void main()
{
vec2 TextureSize = vec2(320.0, 240.0);
#if defined(CURVATURE)
vec2 texcoord = Distort(TEX0);
if (texcoord.x < 0.0)
gl_FragColor = vec4(0.0);
else
#else
vec2 texcoord = TEX0;
#endif
{
vec2 texcoordInPixels = texcoord * TextureSize;
#if defined(SHARPER)
vec2 tempCoord = floor(texcoordInPixels) + 0.5;
vec2 coord = tempCoord / TextureSize;
vec2 deltas = texcoordInPixels - tempCoord;
float scanLineWeight = CalcScanLine(deltas.y);
vec2 signs = sign(deltas);
deltas.x *= 2.0;
deltas = deltas * deltas;
deltas.y = deltas.y * deltas.y;
deltas.x *= 0.5;
deltas.y *= 8.0;
deltas /= TextureSize;
deltas *= signs;
vec2 tc = coord + deltas;
#else
float tempY = floor(texcoordInPixels.y) + 0.5;
float yCoord = tempY / TextureSize.y;
float dy = texcoordInPixels.y - tempY;
float scanLineWeight = CalcScanLine(dy);
float signY = sign(dy);
dy = dy * dy;
dy = dy * dy;
dy *= 8.0;
dy /= TextureSize.y;
dy *= signY;
vec2 tc = vec2(texcoord.x, yCoord + dy);
#endif
vec3 colour = texture2D(Texture, tc).rgb;
#if defined(SCANLINES)
#if defined(GAMMA)
#if defined(FAKE_GAMMA)
colour = colour * colour;
#else
colour = pow(colour, vec3(INPUT_GAMMA));
#endif
#endif
scanLineWeight *= BLOOM_FACTOR;
colour *= scanLineWeight;
#if defined(GAMMA)
#if defined(FAKE_GAMMA)
colour = sqrt(colour);
#else
colour = pow(colour, vec3(1.0/OUTPUT_GAMMA));
#endif
#endif
#endif
#if MASK_TYPE == 0
gl_FragColor = vec4(colour, 1.0);
#else
#if MASK_TYPE == 1
float whichMask = fract((gl_FragCoord.x*1.0001) * 0.5);
vec3 mask;
if (whichMask < 0.5)
mask = vec3(MASK_BRIGHTNESS, 1.0, MASK_BRIGHTNESS);
else
mask = vec3(1.0, MASK_BRIGHTNESS, 1.0);
#elif MASK_TYPE == 2
float whichMask = fract((gl_FragCoord.x*1.0001) * 0.3333333);
vec3 mask = vec3(MASK_BRIGHTNESS, MASK_BRIGHTNESS, MASK_BRIGHTNESS);
if (whichMask < 0.3333333)
mask.x = 1.0;
else if (whichMask < 0.6666666)
mask.y = 1.0;
else
mask.z = 1.0;
#endif
gl_FragColor = vec4(colour * mask, 1.0);
#endif
}
}
#endif

BIN
data/ffase.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

BIN
data/final.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

BIN
data/finals.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.3 KiB

BIN
data/frames.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

BIN
data/frames2.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

BIN
data/gameover.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.1 KiB

BIN
data/intro.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.1 KiB

BIN
data/intro2.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

BIN
data/intro3.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

BIN
data/logo.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.2 KiB

BIN
data/menu.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.5 KiB

BIN
data/menu2.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

BIN
data/tomba1.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

BIN
data/tomba2.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

View File

@@ -1,5 +1,6 @@
#include "engendro.h" #include "engendro.h"
#include "jgame.h" #include "jgame.h"
#include <stdlib.h>
Engendro::Engendro( JD8_Surface gfx, Uint16 x, Uint16 y ) : Sprite( gfx ) { Engendro::Engendro( JD8_Surface gfx, Uint16 x, Uint16 y ) : Sprite( gfx ) {

509
jail_audio.cpp Normal file
View File

@@ -0,0 +1,509 @@
#ifndef JA_USESDLMIXER
#include "jail_audio.h"
#include "stb_vorbis.h"
#include <SDL3/SDL.h>
#include <stdio.h>
#define JA_MAX_SIMULTANEOUS_CHANNELS 5
struct JA_Sound_t
{
SDL_AudioSpec spec { SDL_AUDIO_S16, 2, 48000 };
Uint32 length { 0 };
Uint8 *buffer { NULL };
};
struct JA_Channel_t
{
JA_Sound_t *sound { nullptr };
int pos { 0 };
int times { 0 };
SDL_AudioStream *stream { nullptr };
JA_Channel_state state { JA_CHANNEL_FREE };
};
struct JA_Music_t
{
SDL_AudioSpec spec { SDL_AUDIO_S16, 2, 48000 };
Uint32 length { 0 };
Uint8 *buffer { nullptr };
char *filename { nullptr };
int pos { 0 };
int times { 0 };
SDL_AudioStream *stream { nullptr };
JA_Music_state state { JA_MUSIC_INVALID };
};
JA_Music_t *current_music { nullptr };
JA_Channel_t channels[JA_MAX_SIMULTANEOUS_CHANNELS];
SDL_AudioSpec JA_audioSpec { SDL_AUDIO_S16, 2, 48000 };
float JA_musicVolume { 1.0f };
float JA_soundVolume { 0.5f };
bool JA_musicEnabled { true };
bool JA_soundEnabled { true };
SDL_AudioDeviceID sdlAudioDevice { 0 };
SDL_TimerID JA_timerID { 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) {
const int size = SDL_min(len, current_music->samples*2-current_music->pos);
SDL_MixAudioFormat(stream, (Uint8*)(current_music->output+current_music->pos), AUDIO_S16, size, JA_musicVolume);
current_music->pos += size/2;
if (size < len) {
if (current_music->times != 0) {
SDL_MixAudioFormat(stream+size, (Uint8*)current_music->output, AUDIO_S16, len-size, JA_musicVolume);
current_music->pos = (len-size)/2;
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);
}
}
}
}
}
*/
Uint32 JA_UpdateCallback(void *userdata, SDL_TimerID timerID, Uint32 interval)
{
if (JA_musicEnabled && current_music && current_music->state == JA_MUSIC_PLAYING)
{
if (fading) {
int time = SDL_GetTicks();
if (time > (fade_start_time+fade_duration)) {
fading = false;
JA_StopMusic();
return 30;
} else {
const int time_passed = time - fade_start_time;
const float percent = (float)time_passed / (float)fade_duration;
SDL_SetAudioStreamGain(current_music->stream, JA_musicVolume*(1.0 - percent));
}
}
if (current_music->times != 0)
{
if (SDL_GetAudioStreamAvailable(current_music->stream) < int(current_music->length/2)) {
SDL_PutAudioStreamData(current_music->stream, current_music->buffer, current_music->length);
}
if (current_music->times>0) current_music->times--;
}
else
{
if (SDL_GetAudioStreamAvailable(current_music->stream) == 0) JA_StopMusic();
}
}
if (JA_soundEnabled)
{
for (int i=0; i < JA_MAX_SIMULTANEOUS_CHANNELS; ++i)
if (channels[i].state == JA_CHANNEL_PLAYING)
{
if (channels[i].times != 0)
{
if (SDL_GetAudioStreamAvailable(channels[i].stream) < int(channels[i].sound->length/2)) {
SDL_PutAudioStreamData(channels[i].stream, channels[i].sound->buffer, channels[i].sound->length);
if (channels[i].times>0) channels[i].times--;
}
}
else
{
if (SDL_GetAudioStreamAvailable(channels[i].stream) == 0) JA_StopChannel(i);
}
}
}
return 30;
}
void JA_Init(const int freq, const SDL_AudioFormat format, const int num_channels)
{
#ifdef DEBUG
SDL_SetLogPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_DEBUG);
#endif
SDL_Log("Iniciant JailAudio...");
JA_audioSpec = {format, num_channels, freq };
if (!sdlAudioDevice) SDL_CloseAudioDevice(sdlAudioDevice);
sdlAudioDevice = SDL_OpenAudioDevice(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &JA_audioSpec);
SDL_Log( (sdlAudioDevice==0) ? "Failed to initialize SDL audio!\n" : "OK!\n");
for (int i=0;i<JA_MAX_SIMULTANEOUS_CHANNELS;++i) channels[i].state = JA_CHANNEL_FREE;
//SDL_PauseAudioDevice(sdlAudioDevice);
JA_timerID = SDL_AddTimer(30, JA_UpdateCallback, nullptr);
}
void JA_Quit()
{
if (JA_timerID) SDL_RemoveTimer(JA_timerID);
if (!sdlAudioDevice) SDL_CloseAudioDevice(sdlAudioDevice);
sdlAudioDevice = 0;
}
JA_Music_t *JA_LoadMusic(Uint8* buffer, Uint32 length, const char* filename)
{
JA_Music_t *music = new JA_Music_t();
int chan, samplerate;
short *output;
music->length = stb_vorbis_decode_memory(buffer, length, &chan, &samplerate, &output) * chan * 2;
music->spec.channels = chan;
music->spec.freq = samplerate;
music->spec.format = SDL_AUDIO_S16;
music->buffer = (Uint8*)SDL_malloc(music->length);
SDL_memcpy(music->buffer, output, music->length);
free(output);
music->pos = 0;
music->state = JA_MUSIC_STOPPED;
if (filename) {
music->filename = (char*)malloc(strlen(filename)+1);
strcpy(music->filename, filename);
}
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, filename);
free(buffer);
return music;
}
void JA_PlayMusic(JA_Music_t *music, const int loop)
{
if (!JA_musicEnabled) return;
JA_StopMusic();
current_music = music;
current_music->pos = 0;
current_music->state = JA_MUSIC_PLAYING;
current_music->times = loop;
current_music->stream = SDL_CreateAudioStream(&current_music->spec, &JA_audioSpec);
if (!SDL_PutAudioStreamData(current_music->stream, current_music->buffer, current_music->length)) printf("[ERROR] SDL_PutAudioStreamData failed!\n");
SDL_SetAudioStreamGain(current_music->stream, JA_musicVolume);
if (!SDL_BindAudioStream(sdlAudioDevice, current_music->stream)) printf("[ERROR] SDL_BindAudioStream failed!\n");
//SDL_ResumeAudioStreamDevice(current_music->stream);
}
char *JA_GetMusicFilename(JA_Music_t *music)
{
if (!music) music = current_music;
return music->filename;
}
void JA_PauseMusic()
{
if (!JA_musicEnabled) return;
if (!current_music || current_music->state == JA_MUSIC_INVALID) return;
current_music->state = JA_MUSIC_PAUSED;
//SDL_PauseAudioStreamDevice(current_music->stream);
SDL_UnbindAudioStream(current_music->stream);
}
void JA_ResumeMusic()
{
if (!JA_musicEnabled) return;
if (!current_music || current_music->state == JA_MUSIC_INVALID) return;
current_music->state = JA_MUSIC_PLAYING;
//SDL_ResumeAudioStreamDevice(current_music->stream);
SDL_BindAudioStream(sdlAudioDevice, current_music->stream);
}
void JA_StopMusic()
{
if (!JA_musicEnabled) return;
if (!current_music || current_music->state == JA_MUSIC_INVALID) return;
current_music->pos = 0;
current_music->state = JA_MUSIC_STOPPED;
//SDL_PauseAudioStreamDevice(current_music->stream);
SDL_DestroyAudioStream(current_music->stream);
current_music->stream = nullptr;
free(current_music->filename);
current_music->filename = nullptr;
}
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) return JA_MUSIC_INVALID;
return current_music->state;
}
void JA_DeleteMusic(JA_Music_t *music)
{
if (current_music == music) current_music = nullptr;
SDL_free(music->buffer);
if (music->stream) SDL_DestroyAudioStream(music->stream);
delete music;
}
float JA_SetMusicVolume(float volume)
{
JA_musicVolume = SDL_clamp( volume, 0.0f, 1.0f );
if (current_music) SDL_SetAudioStreamGain(current_music->stream, JA_musicVolume);
return JA_musicVolume;
}
void JA_SetMusicPosition(float value)
{
if (!current_music) return;
current_music->pos = value * current_music->spec.freq;
}
float JA_GetMusicPosition()
{
if (!current_music) return 0;
return float(current_music->pos)/float(current_music->spec.freq);
}
void JA_EnableMusic(const bool value)
{
if ( !value && current_music && (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_LoadWAV_IO(SDL_IOFromMem(buffer, size),1, &sound->spec, &sound->buffer, &sound->length);
return sound;
}
JA_Sound_t *JA_LoadSound(const char* filename)
{
JA_Sound_t *sound = new JA_Sound_t();
SDL_LoadWAV(filename, &sound->spec, &sound->buffer, &sound->length);
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;
JA_StopChannel(channel);
channels[channel].sound = sound;
channels[channel].times = loop;
channels[channel].pos = 0;
channels[channel].state = JA_CHANNEL_PLAYING;
channels[channel].stream = SDL_CreateAudioStream(&channels[channel].sound->spec, &JA_audioSpec);
SDL_PutAudioStreamData(channels[channel].stream, channels[channel].sound->buffer, channels[channel].sound->length);
SDL_SetAudioStreamGain(channels[channel].stream, JA_soundVolume);
SDL_BindAudioStream(sdlAudioDevice, channels[channel].stream);
return channel;
}
int JA_PlaySoundOnChannel(JA_Sound_t *sound, const int channel, const int loop)
{
if (!JA_soundEnabled) return -1;
if (channel < 0 || channel >= JA_MAX_SIMULTANEOUS_CHANNELS) return -1;
JA_StopChannel(channel);
channels[channel].sound = sound;
channels[channel].times = loop;
channels[channel].pos = 0;
channels[channel].state = JA_CHANNEL_PLAYING;
channels[channel].stream = SDL_CreateAudioStream(&channels[channel].sound->spec, &JA_audioSpec);
SDL_PutAudioStreamData(channels[channel].stream, channels[channel].sound->buffer, channels[channel].sound->length);
SDL_SetAudioStreamGain(channels[channel].stream, JA_soundVolume);
SDL_BindAudioStream(sdlAudioDevice, channels[channel].stream);
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;
//SDL_PauseAudioStreamDevice(channels[i].stream);
SDL_UnbindAudioStream(channels[i].stream);
}
}
else if (channel >= 0 && channel < JA_MAX_SIMULTANEOUS_CHANNELS)
{
if (channels[channel].state == JA_CHANNEL_PLAYING)
{
channels[channel].state = JA_CHANNEL_PAUSED;
//SDL_PauseAudioStreamDevice(channels[channel].stream);
SDL_UnbindAudioStream(channels[channel].stream);
}
}
}
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;
//SDL_ResumeAudioStreamDevice(channels[i].stream);
SDL_BindAudioStream(sdlAudioDevice, channels[i].stream);
}
}
else if (channel >= 0 && channel < JA_MAX_SIMULTANEOUS_CHANNELS)
{
if (channels[channel].state == JA_CHANNEL_PAUSED)
{
channels[channel].state = JA_CHANNEL_PLAYING;
//SDL_ResumeAudioStreamDevice(channels[channel].stream);
SDL_BindAudioStream(sdlAudioDevice, channels[channel].stream);
}
}
}
void JA_StopChannel(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_FREE) SDL_DestroyAudioStream(channels[i].stream);
channels[i].stream = nullptr;
channels[i].state = JA_CHANNEL_FREE;
channels[i].pos = 0;
channels[i].sound = NULL;
}
}
else if (channel >= 0 && channel < JA_MAX_SIMULTANEOUS_CHANNELS)
{
if (channels[channel].state != JA_CHANNEL_FREE) SDL_DestroyAudioStream(channels[channel].stream);
channels[channel].stream = nullptr;
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;
}
float JA_SetSoundVolume(float volume)
{
JA_soundVolume = SDL_clamp( volume, 0.0f, 1.0f );
for (int i = 0; i < JA_MAX_SIMULTANEOUS_CHANNELS; i++)
if ( (channels[i].state == JA_CHANNEL_PLAYING) || (channels[i].state == JA_CHANNEL_PAUSED) )
SDL_SetAudioStreamGain(channels[i].stream, JA_soundVolume);
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;
}
float JA_SetVolume(float volume)
{
JA_SetSoundVolume(JA_SetMusicVolume(volume) / 2.0f);
return JA_musicVolume;
}
#endif

41
jail_audio.h Normal file
View File

@@ -0,0 +1,41 @@
#pragma once
#include <SDL3/SDL.h>
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 num_channels);
void JA_Quit();
JA_Music_t *JA_LoadMusic(const char* filename);
JA_Music_t *JA_LoadMusic(Uint8* buffer, Uint32 length, const char* filename=nullptr);
void JA_PlayMusic(JA_Music_t *music, const int loop = -1);
char *JA_GetMusicFilename(JA_Music_t *music = nullptr);
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);
float JA_SetMusicVolume(float 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);
float JA_SetSoundVolume(float volume);
void JA_EnableSound(const bool value);
float JA_SetVolume(float volume);

View File

@@ -33,16 +33,16 @@ void JD8_Init(const char *title) {
main_palette = (JD8_Palette)calloc( 1, 768 ); main_palette = (JD8_Palette)calloc( 1, 768 );
pixel_data = (Uint32*)calloc(1, 320 * 200 * 4); // 1048576 ); pixel_data = (Uint32*)calloc(1, 320 * 200 * 4); // 1048576 );
sdlWindow = SDL_CreateWindow( title, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN ); sdlWindow = SDL_CreateWindow( title, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_OPENGL );
SDL_SetHint(SDL_HINT_RENDER_DRIVER, "opengl"); SDL_SetHint(SDL_HINT_RENDER_DRIVER, "opengl");
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "nearest"); //SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "nearest");
sdlRenderer = SDL_CreateRenderer(sdlWindow, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_TARGETTEXTURE); sdlRenderer = SDL_CreateRenderer(sdlWindow, NULL);
sdlTexture = SDL_CreateTexture(sdlRenderer, SDL_PIXELFORMAT_ABGR8888, SDL_TEXTUREACCESS_STREAMING, 320, 200); sdlTexture = SDL_CreateTexture(sdlRenderer, SDL_PIXELFORMAT_ABGR8888, SDL_TEXTUREACCESS_STREAMING, 320, 200);
backBuffer = SDL_CreateTexture(sdlRenderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, 320, 200); backBuffer = SDL_CreateTexture(sdlRenderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, 320, 200);
int filesize = 0; int filesize = 0;
char *buffer = JF_GetBufferFromResource("crtpi.glsl", filesize, true); char *buffer = file_getfilebuffer("crtpi.glsl", filesize, true);
shader::init(sdlWindow, backBuffer, buffer); shader::init(sdlWindow, backBuffer, buffer);
free(buffer); free(buffer);
@@ -68,7 +68,7 @@ JD8_Surface JD8_NewSurface() {
JD8_Surface JD8_LoadSurface(const char *file) { JD8_Surface JD8_LoadSurface(const char *file) {
int filesize = 0; int filesize = 0;
char *buffer = JF_GetBufferFromResource(file, filesize); char *buffer = file_getfilebuffer(file, filesize);
unsigned short w, h; unsigned short w, h;
Uint8* pixels = LoadGif((unsigned char*)buffer, &w, &h); Uint8* pixels = LoadGif((unsigned char*)buffer, &w, &h);
@@ -90,7 +90,7 @@ JD8_Surface JD8_LoadSurface(const char *file) {
JD8_Palette JD8_LoadPalette(const char *file) { JD8_Palette JD8_LoadPalette(const char *file) {
int filesize = 0; int filesize = 0;
char *buffer = NULL; char *buffer = NULL;
buffer = JF_GetBufferFromResource(file, filesize); buffer = file_getfilebuffer(file, filesize);
JD8_Palette palette = (JD8_Palette)LoadPalette((unsigned char*)buffer); JD8_Palette palette = (JD8_Palette)LoadPalette((unsigned char*)buffer);
@@ -191,7 +191,7 @@ void JD8_Flip() {
} }
SDL_UpdateTexture(sdlTexture, NULL, pixel_data, 320 * sizeof(Uint32)); SDL_UpdateTexture(sdlTexture, NULL, pixel_data, 320 * sizeof(Uint32));
SDL_SetRenderTarget(sdlRenderer, backBuffer); SDL_SetRenderTarget(sdlRenderer, backBuffer);
SDL_RenderCopy(sdlRenderer, sdlTexture, NULL, NULL); SDL_RenderTexture(sdlRenderer, sdlTexture, NULL, NULL);
shader::render(); shader::render();
//SDL_RenderPresent(sdlRenderer); //SDL_RenderPresent(sdlRenderer);
} }

View File

@@ -1,5 +1,5 @@
#pragma once #pragma once
#include <SDL2/SDL.h> #include <SDL3/SDL.h>
struct Color { struct Color {
Uint8 r; Uint8 r;

393
jfile.cpp
View File

@@ -1,163 +1,230 @@
#include "jfile.h" #include <stdlib.h>
#include <SDL2/SDL.h> #include <string.h>
#include <fstream> #include <stdio.h>
#include <stdint.h>
#pragma pack(push,1) #include "jfile.h"
#include <sys/stat.h>
struct DATA_Header { #include <unistd.h>
char magic[4]; #include <iostream>
Uint32 num_files; #include <fstream>
Uint32 index_offset; #include <filesystem>
}; #include <string>
#include <vector>
struct DATA_Info {
Uint32 offset; #ifndef _WIN32
Uint32 length; #include <pwd.h>
char name[13]; #endif
};
#define DEFAULT_FILENAME "data.jf2"
struct DATA_Index { #define DEFAULT_FOLDER "data/"
DATA_Info* file_info; #define CONFIG_FILENAME "config.txt"
};
struct file_t
struct DATA_File { {
DATA_Header header; std::string path;
DATA_Index index; uint32_t size;
}; uint32_t offset;
};
#pragma pack(pop)
std::vector<file_t> toc;
const char *resourceFileName = "data.jrf";
DATA_File *data_file = NULL; /* El std::map me fa coses rares, vaig a usar un good old std::vector amb una estructura key,value propia i au, que sempre funciona */
struct keyvalue_t {
void JF_SetResourceFile(const char *p_resourceFileName) { std::string key, value;
resourceFileName = p_resourceFileName; };
}
char *resource_filename = NULL;
char *resource_folder = NULL;
void JF_GetDataFile() { int file_source = SOURCE_FILE;
std::ifstream fd( resourceFileName, std::ios::in | std::ios::binary ); char scratch[255];
static std::string config_folder;
if( fd.fail() ) { std::vector<keyvalue_t> config;
perror("No s'ha pogut obrir l'arxiu de recursos");
exit(1); void file_setresourcefilename(const char *str) {
} if (resource_filename != NULL) free(resource_filename);
resource_filename = (char*)malloc(strlen(str)+1);
data_file = (DATA_File*)malloc( sizeof( DATA_File ) ); strcpy(resource_filename, str);
}
fd.read( (char*)&data_file->header, sizeof( DATA_Header ) );
void file_setresourcefolder(const char *str) {
fd.seekg( data_file->header.index_offset ); if (resource_folder != NULL) free(resource_folder);
resource_folder = (char*)malloc(strlen(str)+1);
data_file->index.file_info = (DATA_Info*)malloc( data_file->header.num_files * sizeof( DATA_Info ) ); strcpy(resource_folder, str);
}
fd.read( (char*)data_file->index.file_info, data_file->header.num_files * sizeof( DATA_Info ) );
void file_setsource(const int src) {
fd.close(); file_source = src%2; // mod 2 so it always is a valid value, 0 (file) or 1 (folder)
} if (src==SOURCE_FOLDER && resource_folder==NULL) file_setresourcefolder(DEFAULT_FOLDER);
}
char *JF_GetBufferFromResource(const char *resourcename, int& filesize, const bool addZero) {
bool file_getdictionary() {
if( data_file == NULL ) { if (resource_filename == NULL) file_setresourcefilename(DEFAULT_FILENAME);
JF_GetDataFile();
} std::ifstream fi (resource_filename, std::ios::binary);
if (!fi.is_open()) return false;
bool found = false; char header[4];
int count = 0; fi.read(header, 4);
while( !found && count < data_file->header.num_files ) { uint32_t num_files, toc_offset;
found = ( strcmp( resourcename, data_file->index.file_info[count].name ) == 0 ); fi.read((char*)&num_files, 4);
if( !found ) count++; fi.read((char*)&toc_offset, 4);
} fi.seekg(toc_offset);
if( !found ) { for (int i=0; i<num_files; ++i)
perror("El recurs no s'ha trobat en l'arxiu de recursos"); {
exit(1); uint32_t file_offset, file_size;
} fi.read( (char*)&file_offset, 4 );
fi.read( (char*)&file_size, 4 );
filesize = data_file->index.file_info[count].length; uint8_t path_size;
fi.read( (char*)&path_size, 1 );
std::ifstream fd( resourceFileName, std::ios::in | std::ios::binary ); char file_name[path_size+1];
fi.read( file_name, path_size );
if( fd.fail() ) { file_name[path_size] = 0;
perror("No s'ha pogut obrir l'arxiu de recursos"); std::string filename = file_name;
exit(1); toc.push_back({filename, file_size, file_offset});
} }
fi.close();
fd.seekg( data_file->index.file_info[count].offset ); }
char* buffer = (char*)malloc( filesize + (addZero ? 1 : 0) ); char *file_getfilenamewithfolder(const char* filename) {
fd.read( buffer, filesize ); strcpy(scratch, resource_folder);
fd.close(); strcat(scratch, filename);
return scratch;
if (addZero) buffer[filesize] = 0; }
return buffer; FILE *file_getfilepointer(const char *resourcename, int& filesize, const bool binary) {
}
if (file_source==SOURCE_FILE and toc.size()==0) {
// //Read the first INT, which will tell us how many files are in this resource if (not file_getdictionary()) file_setsource(SOURCE_FOLDER);
// int numfiles; }
// int resultat = read(fd, &numfiles, sizeof(int));
// FILE *f;
//#ifdef _WIN32
// int final = eof(fd); if (file_source==SOURCE_FILE) {
//#endif bool found = false;
// uint32_t count = 0;
// //Get the pointers to the stored files while( !found && count < toc.size() ) {
// int *filestart = (int *) malloc(sizeof(int) * numfiles); found = ( std::string(resourcename) == toc[count].path );
// resultat = read(fd, filestart, sizeof(int) * numfiles); if( !found ) count++;
// }
// //Loop through the files, looking for the file in question
// int filenamesize; if( !found ) {
// char *buffer; perror("El recurs no s'ha trobat en l'arxiu de recursos");
// int i; exit(1);
// for(i=0;i<numfiles;i++) }
// {
// int result = 129; filesize = toc[count].size;
// char *filename;
// //Seek to the location f = fopen(resource_filename, binary?"rb":"r");
// lseek(fd, filestart[i], SEEK_SET); if (not f) {
// //Get the filesize value perror("No s'ha pogut obrir l'arxiu de recursos");
// read(fd, filesize, sizeof(int)); exit(1);
// //Get the size of the filename string }
// read(fd, &filenamesize, sizeof(int)); fseek(f, toc[count].offset, SEEK_SET);
// //Size the buffer and read the filename } else {
// filename = (char *) malloc(filenamesize + 1); f = fopen(file_getfilenamewithfolder(resourcename), binary?"rb":"r");
// result = read(fd, filename, filenamesize); fseek(f, 0, SEEK_END);
// //Remember to terminate the string properly! filesize = ftell(f);
// filename[filenamesize] = '\0'; fseek(f, 0, SEEK_SET);
// //Compare to the string we're looking for }
// if (strcmp(filename, resourcename) == 0) return f;
// { }
// //Get the contents of the file
// buffer = (char *) malloc(*filesize); char *file_getfilebuffer(const char *resourcename, int& filesize, const bool zero_terminate) {
// read(fd, buffer, *filesize); FILE *f = file_getfilepointer(resourcename, filesize, true);
// free(filename); char* buffer = (char*)malloc(zero_terminate?filesize:filesize+1);
// break; fread(buffer, filesize, 1, f);
// } if (zero_terminate) buffer[filesize]=0;
// //Free the filename buffer fclose(f);
// free(filename); return buffer;
// } }
//
// //Release memory // Crea la carpeta del sistema donde guardar datos
// free(filestart); void file_setconfigfolder(const char *foldername)
// {
// //Close the resource file! #ifdef _WIN32
// close(fd); config_folder = std::string(getenv("APPDATA")) + "/" + foldername;
// #elif __APPLE__
// //Did we find the file within the resource that we were looking for? struct passwd *pw = getpwuid(getuid());
// if (buffer == NULL) const char *homedir = pw->pw_dir;
// { config_folder = std::string(homedir) + "/Library/Application Support/" + foldername;
// printf("Unable to find '%s' in the resource file!\n", resourcename); #elif __linux__
// exit(1); struct passwd *pw = getpwuid(getuid());
// } const char *homedir = pw->pw_dir;
// config_folder = std::string(homedir) + "/." + foldername;
// //Return the buffer #endif
// return buffer;
//} struct stat st = {0};
if (stat(config_folder.c_str(), &st) == -1)
void JF_Quit() { {
if( data_file != NULL ) { #ifdef _WIN32
free( data_file->index.file_info ); int ret = mkdir(config_folder.c_str());
free( data_file ); #else
} int ret = mkdir(config_folder.c_str(), S_IRWXU);
} #endif
if (ret == -1)
{
printf("ERROR CREATING CONFIG FOLDER.");
exit(EXIT_FAILURE);
}
}
}
const char *file_getconfigfolder() {
std::string folder = config_folder + "/";
return folder.c_str();
}
void file_loadconfigvalues() {
config.clear();
std::string config_file = config_folder + "/config.txt";
FILE *f = fopen(config_file.c_str(), "r");
if (!f) return;
char line[1024];
while (fgets(line, sizeof(line), f)) {
char *value = strchr(line, '=');
if (value) {
*value='\0'; value++;
value[strlen(value)-1] = '\0';
config.push_back({line, value});
}
}
fclose(f);
}
void file_saveconfigvalues() {
std::string config_file = config_folder + "/config.txt";
FILE *f = fopen(config_file.c_str(), "w");
if (f) {
for (auto pair : config) {
fprintf(f, "%s=%s\n", pair.key.c_str(), pair.value.c_str());
}
fclose(f);
}
}
const char* file_getconfigvalue(const char *key) {
if (config.empty()) file_loadconfigvalues();
for (auto pair : config) {
if (pair.key == std::string(key)) {
strcpy(scratch, pair.value.c_str());
return scratch;
}
}
return NULL;
}
void file_setconfigvalue(const char* key, const char* value) {
if (config.empty()) file_loadconfigvalues();
for (auto &pair : config) {
if (pair.key == std::string(key)) {
pair.value = value;
file_saveconfigvalues();
return;
}
}
config.push_back({key, value});
file_saveconfigvalues();
return;
}

25
jfile.h
View File

@@ -1,7 +1,18 @@
#pragma once #pragma once
#include <stdio.h>
void JF_SetResourceFile(const char *p_resourceFileName);
#define SOURCE_FILE 0
char *JF_GetBufferFromResource(const char *resourcename, int& filesize, const bool addZero=false); #define SOURCE_FOLDER 1
void JF_Quit(); void file_setconfigfolder(const char *foldername);
const char *file_getconfigfolder();
void file_setresourcefilename(const char *str);
void file_setresourcefolder(const char *str);
void file_setsource(const int src);
FILE *file_getfilepointer(const char *resourcename, int& filesize, const bool binary=false);
char *file_getfilebuffer(const char *resourcename, int& filesize, const bool zero_terminate=false);
const char* file_getconfigvalue(const char *key);
void file_setconfigvalue(const char* key, const char* value);

View File

@@ -7,7 +7,7 @@ Uint32 cycle_counter = 0;
void JG_Init() { void JG_Init() {
SDL_Init( SDL_INIT_EVERYTHING ); SDL_Init( SDL_INIT_VIDEO | SDL_INIT_AUDIO );
//SDL_WM_SetCaption( title, NULL ); //SDL_WM_SetCaption( title, NULL );
updateTime = SDL_GetTicks(); updateTime = SDL_GetTicks();
} }

View File

@@ -1,5 +1,5 @@
#pragma once #pragma once
#include <SDL2/SDL.h> #include <SDL3/SDL.h>
void JG_Init(); void JG_Init();

View File

@@ -2,7 +2,7 @@
#include "jgame.h" #include "jgame.h"
#include <string> #include <string>
const Uint8 *keystates;// = SDL_GetKeyboardState( NULL ); const bool *keystates;// = SDL_GetKeyboardState( NULL );
SDL_Event event; SDL_Event event;
Uint8 cheat[5]; Uint8 cheat[5];
bool key_pressed = false; bool key_pressed = false;
@@ -27,10 +27,10 @@ void JI_Update() {
if (waitTime > 0) waitTime--; if (waitTime > 0) waitTime--;
while ( SDL_PollEvent( &event ) ) { while ( SDL_PollEvent( &event ) ) {
if ( event.type == SDL_QUIT ) JG_QuitSignal(); if ( event.type == SDL_EVENT_QUIT ) JG_QuitSignal();
if( event.type == SDL_KEYUP ) { if( event.type == SDL_EVENT_KEY_UP ) {
key_pressed = true; key_pressed = true;
JI_moveCheats( event.key.keysym.sym ); JI_moveCheats( event.key.scancode );
} }
} }
} }

View File

@@ -1,5 +1,5 @@
#pragma once #pragma once
#include <SDL2/SDL.h> #include <SDL3/SDL.h>
void JI_DisableKeyboard(Uint32 time); void JI_DisableKeyboard(Uint32 time);

View File

@@ -12,8 +12,8 @@
#include <OpenGL/gl.h> #include <OpenGL/gl.h>
#endif //!ESSENTIAL_GL_PRACTICES_SUPPORT_GL3 #endif //!ESSENTIAL_GL_PRACTICES_SUPPORT_GL3
#else #else
#include <SDL2/SDL_opengl.h> #include <SDL3/SDL_opengl.h>
#include <SDL2/SDL_opengl_glext.h> #include <SDL3/SDL_opengl_glext.h>
#endif #endif
namespace shader namespace shader
@@ -23,8 +23,10 @@ namespace shader
GLuint programId = 0; GLuint programId = 0;
SDL_Texture* backBuffer = nullptr; SDL_Texture* backBuffer = nullptr;
SDL_Point win_size = {640, 480}; SDL_Point win_size = {640, 480};
SDL_Point tex_size = {320, 240}; SDL_FPoint tex_size = {320, 240};
bool usingOpenGL; bool usingOpenGL;
GLuint texture_number;
GLuint nose;
#ifndef __APPLE__ #ifndef __APPLE__
@@ -80,7 +82,8 @@ namespace shader
//Check vertex shader for errors //Check vertex shader for errors
GLint shaderCompiled = GL_FALSE; GLint shaderCompiled = GL_FALSE;
glGetShaderiv( result, GL_COMPILE_STATUS, &shaderCompiled ); glGetShaderiv( result, GL_COMPILE_STATUS, &shaderCompiled );
if( shaderCompiled != GL_TRUE ) { if (shaderCompiled != GL_TRUE)
{
std::cout << "Error en la compilación: " << result << "!" << std::endl; std::cout << "Error en la compilación: " << result << "!" << std::endl;
GLint logLength; GLint logLength;
glGetShaderiv(result, GL_INFO_LOG_LENGTH, &logLength); glGetShaderiv(result, GL_INFO_LOG_LENGTH, &logLength);
@@ -93,13 +96,12 @@ namespace shader
} }
glDeleteShader(result); glDeleteShader(result);
result = 0; result = 0;
// } else {
// std::cout << "Shader compilado correctamente. Id = " << result << std::endl;
} }
return result; return result;
} }
GLuint compileProgram(const char* vertexShaderSource, const char* fragmentShaderSource) { GLuint compileProgram(const char* vertexShaderSource, const char* fragmentShaderSource)
{
GLuint programId = 0; GLuint programId = 0;
GLuint vtxShaderId, fragShaderId; GLuint vtxShaderId, fragShaderId;
@@ -108,7 +110,8 @@ namespace shader
vtxShaderId = compileShader(vertexShaderSource, GL_VERTEX_SHADER); vtxShaderId = compileShader(vertexShaderSource, GL_VERTEX_SHADER);
fragShaderId = compileShader(fragmentShaderSource?fragmentShaderSource:vertexShaderSource, GL_FRAGMENT_SHADER); fragShaderId = compileShader(fragmentShaderSource?fragmentShaderSource:vertexShaderSource, GL_FRAGMENT_SHADER);
if(vtxShaderId && fragShaderId) { if(vtxShaderId && fragShaderId)
{
// Associate shader with program // Associate shader with program
glAttachShader(programId, vtxShaderId); glAttachShader(programId, vtxShaderId);
glAttachShader(programId, fragShaderId); glAttachShader(programId, fragShaderId);
@@ -118,7 +121,8 @@ namespace shader
// Check the status of the compile/link // Check the status of the compile/link
GLint logLen; GLint logLen;
glGetProgramiv(programId, GL_INFO_LOG_LENGTH, &logLen); glGetProgramiv(programId, GL_INFO_LOG_LENGTH, &logLen);
if(logLen > 0) { if (logLen > 0)
{
char* log = (char*) malloc(logLen * sizeof(char)); char* log = (char*) malloc(logLen * sizeof(char));
// Show any errors as appropriate // Show any errors as appropriate
glGetProgramInfoLog(programId, logLen, &logLen, log); glGetProgramInfoLog(programId, logLen, &logLen, log);
@@ -126,12 +130,8 @@ namespace shader
free(log); free(log);
} }
} }
if(vtxShaderId) { if (vtxShaderId) glDeleteShader(vtxShaderId);
glDeleteShader(vtxShaderId); if (fragShaderId) glDeleteShader(fragShaderId);
}
if(fragShaderId) {
glDeleteShader(fragShaderId);
}
return programId; return programId;
} }
@@ -141,29 +141,34 @@ namespace shader
shader::renderer = SDL_GetRenderer(win); shader::renderer = SDL_GetRenderer(win);
shader::backBuffer = backBuffer; shader::backBuffer = backBuffer;
SDL_GetWindowSize(win, &win_size.x, &win_size.y); SDL_GetWindowSize(win, &win_size.x, &win_size.y);
int access; SDL_GetTextureSize(backBuffer, &tex_size.x, &tex_size.y);
SDL_QueryTexture(backBuffer, NULL, &access, &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) if (access != SDL_TEXTUREACCESS_TARGET)
{ {
std::cout << "ERROR FATAL: La textura per al render ha de tindre SDL_TEXTUREACCESS_TARGET definit." << std::endl; std::cout << "ERROR FATAL: La textura per al render ha de tindre SDL_TEXTUREACCESS_TARGET definit." << std::endl;
exit(1); exit(1);
} }
SDL_RendererInfo rendererInfo; const char * renderer_name = SDL_GetRendererName(renderer);
SDL_GetRendererInfo(renderer, &rendererInfo); printf("rendererInfo.name: %s\n", renderer_name);
if(!strncmp(rendererInfo.name, "opengl", 6)) { if(!strncmp(renderer_name, "opengl", 6)) {
//std::cout << "Es OpenGL!" << std::endl; #ifndef __APPLE__
#ifndef __APPLE__
if (!initGLExtensions()) { if (!initGLExtensions()) {
std::cout << "WARNING: No s'han pogut inicialitzar les extensions d'OpenGL!" << std::endl; std::cout << "WARNING: No s'han pogut inicialitzar les extensions d'OpenGL!" << std::endl;
usingOpenGL = false; usingOpenGL = false;
return false; return false;
} }
#endif #endif
// Compilar el shader y dejarlo listo para usar. // Compilar el shader y dejarlo listo para usar.
programId = compileProgram(vertexShader, fragmentShader); programId = compileProgram(vertexShader, fragmentShader);
//std::cout << "programId = " << programId << std::endl;
} else { } else {
std::cout << "WARNING: El driver del renderer no es OpenGL." << std::endl; std::cout << "WARNING: El driver del renderer no es OpenGL." << std::endl;
usingOpenGL = false; usingOpenGL = false;
@@ -173,57 +178,54 @@ namespace shader
return true; return true;
} }
unsigned char pixels[512*240*4];
void render() void render()
{ {
GLint oldProgramId; SDL_FlushRenderer(renderer);
// Guarrada para obtener el textureid (en driverdata->texture)
//Detach the texture
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_SetRenderTarget(renderer, NULL); SDL_SetRenderTarget(renderer, NULL);
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer); SDL_RenderClear(renderer);
SDL_FlushRenderer(renderer);
if (usingOpenGL) { if (usingOpenGL)
SDL_GL_BindTexture(backBuffer, NULL, NULL); {
if(programId != 0) { GLint oldProgramId;
glGetIntegerv(GL_CURRENT_PROGRAM,&oldProgramId); if (programId != 0)
{
glGetIntegerv(GL_CURRENT_PROGRAM, &oldProgramId);
glUseProgram(programId); glUseProgram(programId);
} }
GLfloat minx, miny, maxx, maxy;
GLfloat minu, maxu, minv, maxv;
// Coordenadas de la ventana donde pintar.
minx = 0.0f;
miny = 0.0f;
maxx = tex_size.x;
maxy = tex_size.y;
minu = 0.0f;
maxu = 1.0f;
minv = 0.0f;
maxv = 1.0f;
glEnable(GL_TEXTURE_2D);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, 1);
//glGetTexImage(GL_TEXTURE_2D, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8, pixels);
//if (glGetError()) { printf("GLGETERROR!\n"); exit(1);}
//GLint param;
//glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &param);
//printf("tex width: %i\n", param);
glViewport(0, 0, win_size.x, win_size.y); glViewport(0, 0, win_size.x, win_size.y);
glBegin(GL_TRIANGLE_STRIP); glBegin(GL_TRIANGLE_STRIP);
glTexCoord2f(minu, minv); glTexCoord2f(0.0f, 0.0f);
glVertex2f(minx, miny); glVertex2f(0.0f, 0.0f);
glTexCoord2f(maxu, minv); glTexCoord2f(1.0f, 0.0f);
glVertex2f(maxx, miny); glVertex2f(tex_size.x, 0.0f);
glTexCoord2f(minu, maxv); glTexCoord2f(0.0f, 1.0f);
glVertex2f(minx, maxy); glVertex2f(0.0f, tex_size.y);
glTexCoord2f(maxu, maxv); glTexCoord2f(1.0f, 1.0f);
glVertex2f(maxx, maxy); glVertex2f(tex_size.x, tex_size.y);
glEnd(); glEnd();
SDL_GL_SwapWindow(win); SDL_GL_SwapWindow(win);
if(programId != 0) { if (programId != 0) glUseProgram(oldProgramId);
glUseProgram(oldProgramId);
}
} else { } else {
SDL_RenderCopy(renderer, backBuffer, NULL, NULL); SDL_RenderTexture(renderer, backBuffer, NULL, NULL);
SDL_RenderPresent(renderer); SDL_RenderPresent(renderer);
} }
if (glGetError()) { printf("GLERROR!\n"); exit(1); }
} }
} }

View File

@@ -1,6 +1,6 @@
#pragma once #pragma once
#include <SDL2/SDL.h> #include <SDL3/SDL.h>
// TIPS: // TIPS:
// ======================================================================= // =======================================================================

View File

@@ -1,79 +0,0 @@
#include "jsound.h"
#include "jfile.h"
#include <string>
Mix_Music *music = NULL;
char currentMusic[12];
bool JS_Init() {
Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT, 2, 1024);
Mix_AllocateChannels(8);
currentMusic[0] = 0;
return true;
}
void JS_LoadMusic(const char *musicFilename)
{
if (music != NULL) {
Mix_HaltMusic();
Mix_FreeMusic(music);
}
Mix_VolumeMusic(MIX_MAX_VOLUME);
int filesize = 0;
char *buffer = JF_GetBufferFromResource(musicFilename, filesize);
SDL_RWops *rw = SDL_RWFromMem(buffer, filesize);
music = Mix_LoadMUS_RW(rw, 1);
strcpy(currentMusic, musicFilename);
}
void JS_SetMusicVolume(int volume) {
Mix_VolumeMusic(volume);
}
void JS_PlayMusic(int loops) {
Mix_PlayMusic(music, loops);
}
void JS_PauseMusic() {
Mix_PauseMusic();
}
void JS_FadeOutMusic() {
Mix_FadeOutMusic(500);
}
bool JS_MusicPlaying() {
return (Mix_PlayingMusic() == 1);// && (Mix_FadingMusic() != MIX_FADING_OUT);
}
const char* JS_GetMusicName() {
return currentMusic;
}
JS_Sound *JS_LoadSound(char *soundFilename) {
int filesize = 0;
char *buffer = JF_GetBufferFromResource(soundFilename, filesize);
SDL_RWops *rw = SDL_RWFromMem(buffer, filesize);
return Mix_LoadWAV_RW(rw, false);
}
void JS_SetSoundVolume(JS_Sound *sound, int volume) {
Mix_VolumeChunk(sound, volume);
}
void JS_PlaySound(JS_Sound *sound) {
Mix_PlayChannel(-1, sound, 0);
}
void JS_FreeSound(JS_Sound *sound) {
Mix_FreeChunk(sound);
}
void JS_Finalize() {
Mix_FreeMusic(music);
Mix_CloseAudio();
}

View File

@@ -1,30 +0,0 @@
#pragma once
#include <SDL2/SDL_mixer.h>
typedef Mix_Chunk JS_Sound;
bool JS_Init();
void JS_LoadMusic(const char *musicFilename);
void JS_SetMusicVolume(int volume);
void JS_PlayMusic(int loops);
void JS_PauseMusic();
void JS_FadeOutMusic();
bool JS_MusicPlaying();
const char* JS_GetMusicName();
JS_Sound *JS_LoadSound(char *soundFilename);
void JS_SetSoundVolume(JS_Sound *sound, int volume);
void JS_PlaySound(JS_Sound *sound);
void JS_FreeSound(JS_Sound *sound);
void JS_Finalize();

5
lagueirtofile Normal file
View File

@@ -0,0 +1,5 @@
libs = -lSDL3 -lGL
cppflags = -D DEBUG -D VERBOSE -g -Wall
executable = aee
sourcepath = .
buildpath = build

View File

@@ -1,6 +1,6 @@
#include "jgame.h" #include "jgame.h"
#include "jdraw8.h" #include "jdraw8.h"
#include "jsound.h" #include "jail_audio.h"
#include "jfile.h" #include "jfile.h"
#include "info.h" #include "info.h"
#include "modulegame.h" #include "modulegame.h"
@@ -16,7 +16,7 @@
int main( int argc, char* args[] ) { int main( int argc, char* args[] ) {
JF_SetResourceFile("data.jrf"); file_setresourcefilename("data.jrf");
/*#ifdef WIN32 /*#ifdef WIN32
JF_SetResourceFile("data.jrf"); JF_SetResourceFile("data.jrf");
#else #else
@@ -35,7 +35,7 @@ int main( int argc, char* args[] ) {
JG_Init(); JG_Init();
JD8_Init("Aventures En Egipte"); JD8_Init("Aventures En Egipte");
JS_Init(); JA_Init(48000, SDL_AUDIO_S16, 2);
Info info; Info info;
info.num_habitacio = 1; info.num_habitacio = 1;
@@ -72,8 +72,7 @@ int main( int argc, char* args[] ) {
} }
} }
JF_Quit(); JA_Quit();
JS_Finalize();
JD8_Quit(); JD8_Quit();
JG_Finalize(); JG_Finalize();

View File

@@ -2,7 +2,7 @@
#include "jgame.h" #include "jgame.h"
#include "jdraw8.h" #include "jdraw8.h"
#include "jsound.h" #include "jail_audio.h"
#include "jinput.h" #include "jinput.h"
#include "jfile.h" #include "jfile.h"
@@ -49,10 +49,12 @@ int ModuleGame::Go() {
this->Draw(); this->Draw();
const char* music = this->info->num_piramide == 3 ? "00000008.xm" : (this->info->num_piramide == 2 ? "00000007.xm" : (this->info->num_piramide == 6 ? "00000002.xm" : "00000006.xm")); const char* music = this->info->num_piramide == 3 ? "00000008.ogg" : (this->info->num_piramide == 2 ? "00000007.ogg" : (this->info->num_piramide == 6 ? "00000002.ogg" : "00000006.ogg"));
if (!JS_MusicPlaying() || !(SDL_strcmp(music, JS_GetMusicName()) == 0)) { const char *current_music = JA_GetMusicFilename();
JS_LoadMusic(music); if ( (JA_GetMusicState()!=JA_MUSIC_PLAYING) || !(strcmp(music, current_music) == 0)) {
JS_PlayMusic(-1); int size;
char *buffer = file_getfilebuffer(music, size);
JA_PlayMusic(JA_LoadMusic((Uint8*)buffer, size, music));
} }
JD8_FadeToPal( JD8_LoadPalette(this->info->pepe_activat ? "frames2.gif" : "frames.gif") ); JD8_FadeToPal( JD8_LoadPalette(this->info->pepe_activat ? "frames2.gif" : "frames.gif") );

View File

@@ -3,7 +3,8 @@
#include "jgame.h" #include "jgame.h"
#include "jdraw8.h" #include "jdraw8.h"
#include "jinput.h" #include "jinput.h"
#include "jsound.h" #include "jfile.h"
#include "jail_audio.h"
#include <stdlib.h> #include <stdlib.h>
#include <string> #include <string>
@@ -22,14 +23,14 @@ int ModuleSequence::Go() {
case 255: // Intro case 255: // Intro
doIntro(); doIntro();
break; break;
case 0: // Men<65> case 0: // Men<65>
doMenu(); doMenu();
break; break;
case 1: // Slides case 1: // Slides
case 7: case 7:
doSlides(); doSlides();
break; break;
case 2: // Pre-pir<69>mide case 2: // Pre-pir<69>mide
case 3: case 3:
case 4: case 4:
case 5: case 5:
@@ -80,11 +81,17 @@ const int minim( const int a, const int b ) {
if( b < a ) { return b; } else { return a; } if( b < a ) { return b; } else { return a; }
} }
void play_music(const char *music, bool loop = -1)
{
int size;
char *buffer = file_getfilebuffer(music, size);
JA_PlayMusic(JA_LoadMusic((Uint8*)buffer, size, music), loop);
}
void ModuleSequence::doIntro() { void ModuleSequence::doIntro() {
JG_SetUpdateTicks(1000); JG_SetUpdateTicks(1000);
JS_LoadMusic("00000003.xm"); play_music("00000003.ogg");
JS_PlayMusic(-1);
JD8_Surface gfx = JD8_LoadSurface( "logo.gif" ); JD8_Surface gfx = JD8_LoadSurface( "logo.gif" );
JD8_Palette pal = JD8_LoadPalette( "logo.gif" ); JD8_Palette pal = JD8_LoadPalette( "logo.gif" );
@@ -638,8 +645,7 @@ void ModuleSequence::doSlides() {
const char* arxiu; const char* arxiu;
if( this->info->num_piramide == 7 ) { if( this->info->num_piramide == 7 ) {
JS_LoadMusic("00000005.xm"); play_music("00000005.ogg", 1);
JS_PlayMusic(1);
if (this->info->diners < 200) { if (this->info->diners < 200) {
arxiu = "intro2.gif"; arxiu = "intro2.gif";
} else { } else {
@@ -700,7 +706,7 @@ void ModuleSequence::doSlides() {
step++; step++;
break; break;
case 8: case 8:
if (this->info->num_piramide != 7) JS_FadeOutMusic(); if (this->info->num_piramide != 7) JA_FadeOutMusic(250);
exit = true; exit = true;
break; break;
} }
@@ -712,8 +718,7 @@ void ModuleSequence::doSlides() {
} }
void ModuleSequence::doBanner() { void ModuleSequence::doBanner() {
JS_LoadMusic("00000004.xm"); play_music("00000004.ogg");
JS_PlayMusic(-1);
this->contador = 5000; this->contador = 5000;
@@ -754,12 +759,11 @@ void ModuleSequence::doBanner() {
if( contador == 0 ) exit = true; if( contador == 0 ) exit = true;
} }
} }
JS_FadeOutMusic(); JA_FadeOutMusic(250);
} }
void ModuleSequence::doSecreta() { void ModuleSequence::doSecreta() {
JS_LoadMusic("00000002.xm"); play_music("00000002.ogg");
JS_PlayMusic(-1);
JG_SetUpdateTicks(20); JG_SetUpdateTicks(20);
JD8_FadeOut(); JD8_FadeOut();
JD8_Surface gfx = JD8_LoadSurface("tomba1.gif"); JD8_Surface gfx = JD8_LoadSurface("tomba1.gif");
@@ -834,7 +838,7 @@ void ModuleSequence::doSecreta() {
JD8_FadeOut(); JD8_FadeOut();
memcpy(pal, pal_aux, 768); memcpy(pal, pal_aux, 768);
JD8_ClearScreen(255); JD8_ClearScreen(255);
JS_FadeOutMusic(); JA_FadeOutMusic(250);
exit = true; exit = true;
break; break;
} }
@@ -912,8 +916,7 @@ void ModuleSequence::doCredits() {
} }
void ModuleSequence::doMort() { void ModuleSequence::doMort() {
JS_LoadMusic("00000001.xm"); play_music("00000001.ogg");
JS_PlayMusic(1);
JI_DisableKeyboard(60); JI_DisableKeyboard(60);
@@ -942,7 +945,6 @@ void ModuleSequence::doMort() {
if( contador == 0 ) exit = true; if( contador == 0 ) exit = true;
} }
} }
JS_LoadMusic("00000003.xm"); play_music("00000003.ogg");
JS_PlayMusic(-1);
} }

View File

@@ -1,6 +1,7 @@
#include "prota.h" #include "prota.h"
#include "jgame.h" #include "jgame.h"
#include "jinput.h" #include "jinput.h"
#include <stdlib.h>
Prota::Prota( JD8_Surface gfx, Info* info ) : Sprite( gfx ) { Prota::Prota( JD8_Surface gfx, Info* info ) : Sprite( gfx ) {
this->info = info; this->info = info;

View File

@@ -1,4 +1,5 @@
#include "sprite.h" #include "sprite.h"
#include <stdlib.h>
Sprite::Sprite( JD8_Surface gfx ) { Sprite::Sprite( JD8_Surface gfx ) {

5584
stb_vorbis.h Normal file

File diff suppressed because it is too large Load Diff