Fix warnings
This commit is contained in:
31
src/Anim.c
31
src/Anim.c
@@ -1,4 +1,4 @@
|
||||
// Copyright (C) 2011 Valeriano Alfonso Rodriguez (Kableado)
|
||||
// Copyright (C) 2011-2023 Valeriano Alfonso Rodriguez (Kableado)
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@@ -12,23 +12,23 @@
|
||||
//
|
||||
typedef struct {
|
||||
DrawImg img;
|
||||
int w;
|
||||
int32_t w;
|
||||
float fps;
|
||||
int frames;
|
||||
int ftime;
|
||||
int time;
|
||||
int32_t frames;
|
||||
int32_t frameTime;
|
||||
int32_t time;
|
||||
} Animation;
|
||||
|
||||
/////////////////////////////
|
||||
// Anim_LoadAnim
|
||||
//
|
||||
//
|
||||
Anim Anim_LoadAnim(char *fichero, int width, int frames, float fps) {
|
||||
Anim Anim_LoadAnim(char *filename, int width, int frames, float fps) {
|
||||
DrawImg img;
|
||||
Animation *anim;
|
||||
int w, h;
|
||||
|
||||
img = Draw_LoadImage(fichero);
|
||||
img = Draw_LoadImage(filename);
|
||||
if (!img) {
|
||||
return (NULL);
|
||||
}
|
||||
@@ -42,10 +42,10 @@ Anim Anim_LoadAnim(char *fichero, int width, int frames, float fps) {
|
||||
if (width <= 0) {
|
||||
anim->w = w / frames;
|
||||
}
|
||||
anim->fps = fps;
|
||||
anim->frames = frames;
|
||||
anim->ftime = 1000 / fps;
|
||||
anim->time = anim->ftime * frames;
|
||||
anim->fps = fps;
|
||||
anim->frames = frames;
|
||||
anim->frameTime = (int)(1000.0f / fps);
|
||||
anim->time = anim->frameTime * frames;
|
||||
|
||||
return ((Anim)anim);
|
||||
}
|
||||
@@ -66,10 +66,10 @@ int Anim_GetTime(Anim a) {
|
||||
// Gets the animation size.
|
||||
void Anim_GetSize(Anim a, int *w, int *h) {
|
||||
Animation *anim = a;
|
||||
int waux;
|
||||
int widthAux;
|
||||
|
||||
*w = anim->w;
|
||||
Draw_GetSize(anim->img, &waux, h);
|
||||
Draw_GetSize(anim->img, &widthAux, h);
|
||||
}
|
||||
|
||||
/////////////////////////////
|
||||
@@ -112,7 +112,7 @@ void Anim_Draw(Anim a, int time_ms, int x, int y, float scale[2]) {
|
||||
Animation *anim = a;
|
||||
int frame;
|
||||
|
||||
frame = (time_ms / anim->ftime) % anim->frames;
|
||||
frame = (time_ms / anim->frameTime) % anim->frames;
|
||||
Draw_DrawImgPartHoriz(anim->img, x, y, anim->w, frame, scale);
|
||||
}
|
||||
|
||||
@@ -214,7 +214,8 @@ void AnimPlay_GetSize(AnimPlay *ani, int *w, int *h) {
|
||||
if (ani->anim) {
|
||||
Anim_GetSize(ani->anim, w, h);
|
||||
return;
|
||||
} else if (ani->img) {
|
||||
}
|
||||
if (ani->img) {
|
||||
Draw_GetSize(ani->img, w, h);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// Copyright (C) 2011 Valeriano Alfonso Rodriguez (Kableado)
|
||||
// Copyright (C) 2011-2023 Valeriano Alfonso Rodriguez (Kableado)
|
||||
|
||||
#ifndef _ANIM_H_
|
||||
#define _ANIM_H_
|
||||
#ifndef Amin_H
|
||||
#define Amin_H
|
||||
|
||||
#include "Draw.h"
|
||||
|
||||
@@ -15,7 +15,7 @@ typedef void *Anim;
|
||||
// Anim_LoadAnim
|
||||
//
|
||||
//
|
||||
Anim Anim_LoadAnim(char *fichero, int width, int frames, float fps);
|
||||
Anim Anim_LoadAnim(char *filename, int width, int frames, float fps);
|
||||
|
||||
/////////////////////////////
|
||||
// Anim_GetTime
|
||||
|
||||
135
src/Audio.c
135
src/Audio.c
@@ -1,4 +1,4 @@
|
||||
// Copyright (C) 2011 Valeriano Alfonso Rodriguez (Kableado)
|
||||
// Copyright (C) 2011-2023 Valeriano Alfonso Rodriguez (Kableado)
|
||||
|
||||
#ifdef WIN32
|
||||
#define _WIN32_WINNT 0x0501
|
||||
@@ -21,16 +21,16 @@ static void Audio_MixerCallback(void *ud, Uint8 *stream, int l);
|
||||
// Reference to a sound.
|
||||
typedef struct TAudioWave TAudioWave, *AudioWave;
|
||||
struct TAudioWave {
|
||||
unsigned int sampleRate;
|
||||
int channels;
|
||||
int bpb;
|
||||
int BPB;
|
||||
Uint32 len;
|
||||
Uint8 *buffer;
|
||||
uint32_t sampleRate;
|
||||
int32_t channels;
|
||||
int32_t bpb;
|
||||
int32_t BPB;
|
||||
int32_t len;
|
||||
uint8_t *buffer;
|
||||
|
||||
AudioWave next;
|
||||
};
|
||||
AudioWave _waves = NULL;
|
||||
AudioWave g_Waves = NULL;
|
||||
|
||||
////////////////////////////////////////////////
|
||||
// AudioChan //
|
||||
@@ -39,18 +39,18 @@ AudioWave _waves = NULL;
|
||||
typedef struct TAudioChan TAudioChan, *AudioChan;
|
||||
struct TAudioChan {
|
||||
AudioWave wave;
|
||||
Uint32 pos;
|
||||
unsigned char rightvol;
|
||||
unsigned char leftvol;
|
||||
int32_t pos;
|
||||
uint8_t rightVolume;
|
||||
uint8_t leftVolume;
|
||||
|
||||
int loop;
|
||||
int32_t loop;
|
||||
|
||||
AudioChan next;
|
||||
};
|
||||
AudioChan _channels = NULL;
|
||||
AudioChan _free_channels = NULL;
|
||||
AudioChan g_Channels = NULL;
|
||||
AudioChan g_FreeChannels = NULL;
|
||||
|
||||
static SDL_AudioDeviceID _audioDeviceID = 0;
|
||||
static SDL_AudioDeviceID g_AudioDeviceID = 0;
|
||||
|
||||
/////////////////////////////
|
||||
// Audio_Init
|
||||
@@ -60,10 +60,9 @@ int Audio_Init() {
|
||||
SDL_AudioSpec as;
|
||||
SDL_AudioSpec as2;
|
||||
|
||||
// Initialize audio subsistem
|
||||
// Initialize audio subsystem
|
||||
if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) {
|
||||
Print("Audio_Init: Failure initializing SDL Audio.\n");
|
||||
Print("\tSDL Error: %s\n", SDL_GetError());
|
||||
Print("ERROR: Audio_Init: Failure initializing SDL Audio. %s\n", SDL_GetError());
|
||||
return (0);
|
||||
}
|
||||
|
||||
@@ -73,22 +72,21 @@ int Audio_Init() {
|
||||
as.channels = 2;
|
||||
as.samples = 2048;
|
||||
as.callback = Audio_MixerCallback;
|
||||
_audioDeviceID = SDL_OpenAudioDevice(NULL, 0, &as, &as2, 0);
|
||||
if (_audioDeviceID == 0) {
|
||||
Print("Audio_Init: Failure opening audio.\n");
|
||||
Print("\tSDL Error: %s\n", SDL_GetError());
|
||||
g_AudioDeviceID = SDL_OpenAudioDevice(NULL, 0, &as, &as2, 0);
|
||||
if (g_AudioDeviceID == 0) {
|
||||
Print("ERROR: Audio_Init: Failure opening audio. %s\n", SDL_GetError());
|
||||
return (0);
|
||||
}
|
||||
|
||||
// Asert results
|
||||
if (as2.format != AUDIO_S16SYS || as2.freq != 44100 || as2.channels != 2) {
|
||||
Print("Audio_Init: Failure opening audio. (44.1Khz/16b/2c).\n");
|
||||
Print("ERROR: Audio_Init: Failure opening audio. (44.1Khz/16b/2c).\n");
|
||||
SDL_CloseAudio();
|
||||
return (0);
|
||||
}
|
||||
|
||||
// Unpause and ready to go
|
||||
SDL_PauseAudioDevice(_audioDeviceID, 0);
|
||||
SDL_PauseAudioDevice(g_AudioDeviceID, 0);
|
||||
|
||||
return (1);
|
||||
}
|
||||
@@ -97,13 +95,13 @@ int Audio_Init() {
|
||||
// Audio_MixerCallback
|
||||
//
|
||||
// Mixes the audio channels.
|
||||
static void Audio_MixerCallback(void *ud, Uint8 *stream, int l) {
|
||||
signed short *ptr_out, *ptr_wave;
|
||||
AudioChan prevchan;
|
||||
static void Audio_MixerCallback(void *ud, uint8_t *stream, int l) {
|
||||
int16_t *ptr_out, *ptr_wave;
|
||||
AudioChan previousChannel;
|
||||
AudioChan chan;
|
||||
AudioWave wave;
|
||||
int len = l / 4; // Asume 16bpb and 2 output chan
|
||||
int chan_remain;
|
||||
int channelRemaining;
|
||||
int len_mix;
|
||||
int i;
|
||||
|
||||
@@ -111,19 +109,19 @@ static void Audio_MixerCallback(void *ud, Uint8 *stream, int l) {
|
||||
memset(stream, 0, l);
|
||||
|
||||
// Mix all the channels
|
||||
prevchan = NULL;
|
||||
chan = _channels;
|
||||
previousChannel = NULL;
|
||||
chan = g_Channels;
|
||||
while (chan) {
|
||||
if (!chan->wave) {
|
||||
// Remove finished channels
|
||||
AudioChan aux_chan = chan->next;
|
||||
chan->next = _free_channels;
|
||||
_free_channels = chan;
|
||||
chan->next = g_FreeChannels;
|
||||
g_FreeChannels = chan;
|
||||
chan = aux_chan;
|
||||
if (prevchan) {
|
||||
prevchan->next = chan;
|
||||
if (previousChannel) {
|
||||
previousChannel->next = chan;
|
||||
} else {
|
||||
_channels = chan;
|
||||
g_Channels = chan;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
@@ -133,15 +131,15 @@ static void Audio_MixerCallback(void *ud, Uint8 *stream, int l) {
|
||||
ptr_wave = ((signed short *)chan->wave->buffer) + chan->pos;
|
||||
wave = chan->wave;
|
||||
|
||||
// Determine mixing lenght
|
||||
chan_remain = wave->len - chan->pos;
|
||||
if (chan_remain > len) {
|
||||
// Determine mixing length
|
||||
channelRemaining = wave->len - chan->pos;
|
||||
if (channelRemaining > len) {
|
||||
len_mix = len;
|
||||
} else {
|
||||
if (chan->loop) {
|
||||
len_mix = len;
|
||||
} else {
|
||||
len_mix = chan_remain;
|
||||
len_mix = channelRemaining;
|
||||
}
|
||||
chan->wave = NULL;
|
||||
}
|
||||
@@ -152,24 +150,24 @@ static void Audio_MixerCallback(void *ud, Uint8 *stream, int l) {
|
||||
|
||||
// Left Channel
|
||||
temp = ptr_out[0];
|
||||
temp += (ptr_wave[0] * chan->leftvol) >> 8;
|
||||
temp += (ptr_wave[0] * chan->leftVolume) >> 8;
|
||||
if (temp > (1 << 14)) {
|
||||
ptr_out[0] = 1 << 14;
|
||||
} else if (temp < -(1 << 14)) {
|
||||
ptr_out[0] = -(1 << 14);
|
||||
} else {
|
||||
ptr_out[0] = temp;
|
||||
ptr_out[0] = (int16_t)temp;
|
||||
}
|
||||
|
||||
// Right Channel
|
||||
temp = ptr_out[1];
|
||||
temp += (ptr_wave[0] * chan->rightvol) >> 8;
|
||||
temp += (ptr_wave[0] * chan->rightVolume) >> 8;
|
||||
if (temp > (1 << 14)) {
|
||||
ptr_out[1] = 1 << 14;
|
||||
} else if (temp < -(1 << 14)) {
|
||||
ptr_out[1] = -(1 << 14);
|
||||
} else {
|
||||
ptr_out[1] = temp;
|
||||
ptr_out[1] = (int16_t)temp;
|
||||
}
|
||||
|
||||
// Next sample
|
||||
@@ -190,7 +188,7 @@ static void Audio_MixerCallback(void *ud, Uint8 *stream, int l) {
|
||||
}
|
||||
|
||||
// Next channel
|
||||
prevchan = chan;
|
||||
previousChannel = chan;
|
||||
chan = chan->next;
|
||||
}
|
||||
}
|
||||
@@ -213,14 +211,14 @@ AudioSnd Audio_LoadSound(char *filename) {
|
||||
|
||||
f = fopen(filename, "rb");
|
||||
if (!f) {
|
||||
Print("Audio_LoadSound: Failure opening file.\n");
|
||||
Print("ERROR: Audio_LoadSound: Failure opening file. \"%s\".\n", filename);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
// Read id "RIFF"
|
||||
fread(id, 4, sizeof(char), f);
|
||||
if (strcmp(id, "RIFF")) {
|
||||
Print("Audio_LoadSound: File is not RIFF.\n");
|
||||
if (strcmp(id, "RIFF") != 0) {
|
||||
Print("ERROR: Audio_LoadSound: File is not RIFF. \"%s\".\n", filename);
|
||||
fclose(f);
|
||||
return (NULL);
|
||||
}
|
||||
@@ -230,8 +228,8 @@ AudioSnd Audio_LoadSound(char *filename) {
|
||||
|
||||
// Read id "WAVE"
|
||||
fread(id, 4, sizeof(char), f);
|
||||
if (strcmp(id, "WAVE")) {
|
||||
Print("Audio_LoadSound: File is not WAVE.\n");
|
||||
if (strcmp(id, "WAVE") != 0) {
|
||||
Print("ERROR:Audio_LoadSound: File is not WAVE. \"%s\".\n", filename);
|
||||
fclose(f);
|
||||
return (NULL);
|
||||
}
|
||||
@@ -240,13 +238,13 @@ AudioSnd Audio_LoadSound(char *filename) {
|
||||
fread(id, 1, sizeof(char) * 4, f); // Read "fmt "
|
||||
fread(&formatLen, 1, sizeof(int), f);
|
||||
if (formatLen < 14) {
|
||||
Print("Audio_LoadSound: File too short.\n");
|
||||
Print("ERROR: Audio_LoadSound: File too short. \"%s\".\n", filename);
|
||||
fclose(f);
|
||||
return (NULL);
|
||||
}
|
||||
fread(&formatTag, 1, sizeof(short), f); // 1=PCM
|
||||
if (formatTag != 1) {
|
||||
Print("Audio_LoadSound: Not PCM format.\n");
|
||||
Print("ERROR: Audio_LoadSound: Not PCM format. \"%s\".\n", filename);
|
||||
fclose(f);
|
||||
return (NULL);
|
||||
}
|
||||
@@ -260,30 +258,31 @@ AudioSnd Audio_LoadSound(char *filename) {
|
||||
// Assert sound format
|
||||
if (sampleRate != 44100 || channels != 1 || bitsPerSample != 2) {
|
||||
Print(
|
||||
"Audio_LoadSound: Format not supported: "
|
||||
"sampleRate:%d; channels:%d; BPB:%d\n",
|
||||
"ERROR: Audio_LoadSound: Format not supported: "
|
||||
"sampleRate:%d; channels:%d; BPB:%d. \"%s\"\n",
|
||||
sampleRate,
|
||||
channels,
|
||||
bitsPerSample);
|
||||
bitsPerSample,
|
||||
filename);
|
||||
fclose(f);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
// Skip no "data" blocks
|
||||
do {
|
||||
int lenRead = fread(id, 1, sizeof(char) * 4, f);
|
||||
size_t lenRead = fread(id, 1, sizeof(char) * 4, f);
|
||||
if (lenRead < 4) {
|
||||
break;
|
||||
}
|
||||
if (strcmp(id, "data")) {
|
||||
if (strcmp(id, "data") != 0) {
|
||||
fread(&dataSize, 1, sizeof(int), f);
|
||||
fseek(f, dataSize, SEEK_CUR);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
} while (1);
|
||||
if (strcmp(id, "data")) {
|
||||
Print("Audio_LoadSound: DATA block not found\n");
|
||||
if (strcmp(id, "data") != 0) {
|
||||
Print("ERROR: Audio_LoadSound: DATA block not found. \"%s\".\n", filename);
|
||||
fclose(f);
|
||||
return (NULL);
|
||||
}
|
||||
@@ -305,8 +304,8 @@ AudioSnd Audio_LoadSound(char *filename) {
|
||||
wave->len = dataSize / (wave->BPB * wave->channels);
|
||||
|
||||
// Take a reference
|
||||
wave->next = _waves;
|
||||
_waves = wave;
|
||||
wave->next = g_Waves;
|
||||
g_Waves = wave;
|
||||
|
||||
return (wave);
|
||||
}
|
||||
@@ -315,7 +314,7 @@ AudioSnd Audio_LoadSound(char *filename) {
|
||||
// Audio_PlaySound
|
||||
//
|
||||
// Loads a sound, giving a reference.
|
||||
AudioChn Audio_PlaySound(AudioSnd snd, float leftvol, float rightvol, int loop) {
|
||||
AudioChn Audio_PlaySound(AudioSnd snd, float leftVolume, float rightVolume, int loop) {
|
||||
AudioChan chan;
|
||||
AudioWave wave;
|
||||
if (!snd) {
|
||||
@@ -326,9 +325,9 @@ AudioChn Audio_PlaySound(AudioSnd snd, float leftvol, float rightvol, int loop)
|
||||
wave = snd;
|
||||
|
||||
// Get a free channel
|
||||
if (_free_channels) {
|
||||
chan = _free_channels;
|
||||
_free_channels = chan->next;
|
||||
if (g_FreeChannels) {
|
||||
chan = g_FreeChannels;
|
||||
g_FreeChannels = chan->next;
|
||||
chan->next = NULL;
|
||||
} else {
|
||||
chan = malloc(sizeof(TAudioChan));
|
||||
@@ -338,13 +337,13 @@ AudioChn Audio_PlaySound(AudioSnd snd, float leftvol, float rightvol, int loop)
|
||||
// Initialize the channel
|
||||
chan->wave = wave;
|
||||
chan->pos = 0;
|
||||
chan->rightvol = (rightvol * 255);
|
||||
chan->leftvol = (leftvol * 255);
|
||||
chan->rightVolume = (uint8_t)(rightVolume * 255.0f);
|
||||
chan->leftVolume = (uint8_t)(leftVolume * 255.0f);
|
||||
chan->loop = loop;
|
||||
|
||||
// Include in sounds list
|
||||
chan->next = _channels;
|
||||
_channels = chan;
|
||||
chan->next = g_Channels;
|
||||
g_Channels = chan;
|
||||
|
||||
return chan;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// Copyright (C) 2011 Valeriano Alfonso Rodriguez (Kableado)
|
||||
// Copyright (C) 2011-2023 Valeriano Alfonso Rodriguez (Kableado)
|
||||
|
||||
#ifndef _AUDIO_H_
|
||||
#define _AUDIO_H_
|
||||
#ifndef Audio_H
|
||||
#define Audio_H
|
||||
|
||||
/////////////////////////////
|
||||
// Audio_Init
|
||||
@@ -37,7 +37,7 @@ AudioSnd Audio_LoadSound(char *filename);
|
||||
// Audio_PlaySound
|
||||
//
|
||||
// Loads a sound, giving a reference.
|
||||
AudioChn Audio_PlaySound(AudioSnd snd, float leftvol, float rightvol, int loop);
|
||||
AudioChn Audio_PlaySound(AudioSnd snd, float leftVolume, float rightVolume, int loop);
|
||||
|
||||
/////////////////////////////
|
||||
// Audio_StopChan
|
||||
|
||||
436
src/Draw.c
436
src/Draw.c
@@ -1,8 +1,8 @@
|
||||
// Copyright (C) 2011-2015 Valeriano Alfonso Rodriguez (Kableado)
|
||||
// Copyright (C) 2011-2023 Valeriano Alfonso Rodriguez (Kableado)
|
||||
|
||||
#ifdef WIN32
|
||||
|
||||
// Windows
|
||||
// Windows
|
||||
#define _WIN32_WINNT 0x0501
|
||||
#include <GL/gl.h>
|
||||
#include <GL/glext.h>
|
||||
@@ -17,7 +17,7 @@
|
||||
|
||||
#else
|
||||
|
||||
// Linux
|
||||
// Linux + Emscripten
|
||||
#include <math.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
@@ -60,21 +60,21 @@ struct TDrawImage {
|
||||
};
|
||||
|
||||
// Globals
|
||||
static SDL_Window *_window = NULL;
|
||||
static SDL_GLContext _glcontext = NULL;
|
||||
static SDL_Renderer *_renderer = NULL;
|
||||
int _width;
|
||||
int _height;
|
||||
long long proc_t_frame = 33333;
|
||||
long long draw_t_frame = 16667;
|
||||
int _fps = 60;
|
||||
QuadArray2D _quadArray = NULL;
|
||||
DrawImage _currentImg = NULL;
|
||||
float _color[4];
|
||||
static SDL_Window *g_Window = NULL;
|
||||
static SDL_GLContext g_GLContext = NULL;
|
||||
static SDL_Renderer *g_Renderer = NULL;
|
||||
int g_Width;
|
||||
int g_Height;
|
||||
long long g_ProcTFrame = 33333;
|
||||
long long g_DrawTFrame = 16667;
|
||||
int g_FPS = 60;
|
||||
QuadArray2D g_QuadArray = NULL;
|
||||
DrawImage g_CurrentImg = NULL;
|
||||
float g_Color[4];
|
||||
|
||||
#if USE_OpenGLES
|
||||
|
||||
GLuint _whiteTex;
|
||||
GLuint g_WhiteTex;
|
||||
|
||||
GLuint Draw_CompileShader(GLenum type, const char *source) {
|
||||
char *strType = type == GL_VERTEX_SHADER ? "VertexShader"
|
||||
@@ -82,7 +82,7 @@ GLuint Draw_CompileShader(GLenum type, const char *source) {
|
||||
: "unknownShader";
|
||||
GLuint shader = glCreateShader(type);
|
||||
if (shader == 0) {
|
||||
Print("Error creating shader of type: %s\n", strType);
|
||||
Print("ERROR: Creating shader of type: %s\n", strType);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -98,7 +98,7 @@ GLuint Draw_CompileShader(GLenum type, const char *source) {
|
||||
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLogLength);
|
||||
GLchar strInfoLog[infoLogLength + 1];
|
||||
glGetShaderInfoLog(shader, infoLogLength, NULL, strInfoLog);
|
||||
Print("Error compiling shader of type: %s: %s\n", strType, strInfoLog);
|
||||
Print("ERROR: Compiling shader of type: %s: %s\n", strType, strInfoLog);
|
||||
|
||||
glDeleteShader(shader);
|
||||
return 0;
|
||||
@@ -131,16 +131,16 @@ GLuint Draw_BuildProgram(const char *vertexShaderSource, const char *fragmentSha
|
||||
return programObject;
|
||||
}
|
||||
|
||||
GLuint programObject;
|
||||
GLuint g_ProgramObject;
|
||||
|
||||
GLuint vertPosLoc;
|
||||
GLuint vertTexLoc;
|
||||
GLuint vertColorLoc;
|
||||
GLint g_VertPosLoc;
|
||||
GLint g_VertTexLoc;
|
||||
GLint g_VertColorLoc;
|
||||
|
||||
GLuint textureLoc;
|
||||
GLuint projectionMatrixLoc;
|
||||
GLint g_TextureLoc;
|
||||
GLint g_ProjectionMatrixLoc;
|
||||
|
||||
GLuint vertexObject;
|
||||
GLuint g_VertexObject;
|
||||
|
||||
#define Max_Vertices 6000
|
||||
|
||||
@@ -154,7 +154,7 @@ GLuint Draw_UploadGLTexture(int w, int h, unsigned char *pixels);
|
||||
// Draw_Init
|
||||
//
|
||||
// Initializes the game window.
|
||||
int Draw_Init(int width, int height, char *title, int pfps, int fps) {
|
||||
int Draw_Init(int width, int height, char *title, int pFps, int fps) {
|
||||
|
||||
#ifdef WIN32
|
||||
#ifndef ATTACH_PARENT_PROCESS
|
||||
@@ -173,11 +173,11 @@ int Draw_Init(int width, int height, char *title, int pfps, int fps) {
|
||||
#endif // WIN32
|
||||
|
||||
// Set globals
|
||||
proc_t_frame = 1000000 / pfps;
|
||||
draw_t_frame = 1000000 / fps;
|
||||
_fps = fps;
|
||||
_width = width;
|
||||
_height = height;
|
||||
g_ProcTFrame = 1000000 / pFps;
|
||||
g_DrawTFrame = 1000000 / fps;
|
||||
g_FPS = fps;
|
||||
g_Width = width;
|
||||
g_Height = height;
|
||||
|
||||
// Initialize SDL
|
||||
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
|
||||
@@ -187,16 +187,16 @@ int Draw_Init(int width, int height, char *title, int pfps, int fps) {
|
||||
}
|
||||
|
||||
// Initialize video mode
|
||||
_window =
|
||||
g_Window =
|
||||
SDL_CreateWindow(title, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_OPENGL);
|
||||
if (_window == NULL) {
|
||||
if (g_Window == NULL) {
|
||||
Print("Draw_Init: Failure initializing video mode.\n");
|
||||
Print("\tSDL Error: %s\n", SDL_GetError());
|
||||
return (0);
|
||||
}
|
||||
_glcontext = SDL_GL_CreateContext(_window);
|
||||
g_GLContext = SDL_GL_CreateContext(g_Window);
|
||||
|
||||
_renderer = SDL_CreateRenderer(_window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_TARGETTEXTURE);
|
||||
g_Renderer = SDL_CreateRenderer(g_Window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_TARGETTEXTURE);
|
||||
|
||||
Draw_ShowInfo();
|
||||
|
||||
@@ -211,11 +211,11 @@ int Draw_Init(int width, int height, char *title, int pfps, int fps) {
|
||||
|
||||
// Triplebuffer swap
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
SDL_GL_SwapWindow(_window);
|
||||
SDL_GL_SwapWindow(g_Window);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
SDL_GL_SwapWindow(_window);
|
||||
SDL_GL_SwapWindow(g_Window);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
SDL_GL_SwapWindow(_window);
|
||||
SDL_GL_SwapWindow(g_Window);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
glEnableClientState(GL_COLOR_ARRAY);
|
||||
@@ -254,48 +254,48 @@ int Draw_Init(int width, int height, char *title, int pfps, int fps) {
|
||||
" gl_FragColor = texture2D(sTexture, vTexCoord)*vColor; \n"
|
||||
"} \n";
|
||||
|
||||
programObject = Draw_BuildProgram(vertexShaderSource, fragmentShaderSource_20);
|
||||
if (programObject == 0) {
|
||||
programObject = Draw_BuildProgram(vertexShaderSource, fragmentShaderSource_10);
|
||||
g_ProgramObject = Draw_BuildProgram(vertexShaderSource, fragmentShaderSource_20);
|
||||
if (g_ProgramObject == 0) {
|
||||
g_ProgramObject = Draw_BuildProgram(vertexShaderSource, fragmentShaderSource_10);
|
||||
}
|
||||
glUseProgram(programObject);
|
||||
glUseProgram(g_ProgramObject);
|
||||
|
||||
vertPosLoc = glGetAttribLocation(programObject, "aPosition");
|
||||
vertTexLoc = glGetAttribLocation(programObject, "aTexCoord");
|
||||
vertColorLoc = glGetAttribLocation(programObject, "aColor");
|
||||
g_VertPosLoc = glGetAttribLocation(g_ProgramObject, "aPosition");
|
||||
g_VertTexLoc = glGetAttribLocation(g_ProgramObject, "aTexCoord");
|
||||
g_VertColorLoc = glGetAttribLocation(g_ProgramObject, "aColor");
|
||||
|
||||
textureLoc = glGetUniformLocation(programObject, "sTexture");
|
||||
projectionMatrixLoc = glGetUniformLocation(programObject, "sProjectionMatrix");
|
||||
g_TextureLoc = glGetUniformLocation(g_ProgramObject, "sTexture");
|
||||
g_ProjectionMatrixLoc = glGetUniformLocation(g_ProgramObject, "sProjectionMatrix");
|
||||
|
||||
glUniform1i(textureLoc, 0);
|
||||
glUniform1i(g_TextureLoc, 0);
|
||||
|
||||
glGenBuffers(1, &vertexObject);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vertexObject);
|
||||
glGenBuffers(1, &g_VertexObject);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, g_VertexObject);
|
||||
glBufferData(GL_ARRAY_BUFFER, Vertex2D_Length * sizeof(float) * Max_Vertices, NULL, GL_DYNAMIC_DRAW);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vertexObject);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, g_VertexObject);
|
||||
|
||||
glVertexAttribPointer(
|
||||
vertPosLoc, 2, GL_FLOAT, GL_FALSE, Vertex2D_Length * sizeof(float), (void *)(0 * sizeof(float)));
|
||||
g_VertPosLoc, 2, GL_FLOAT, GL_FALSE, Vertex2D_Length * sizeof(float), (void *)(0 * sizeof(float)));
|
||||
glVertexAttribPointer(
|
||||
vertTexLoc, 2, GL_FLOAT, GL_FALSE, Vertex2D_Length * sizeof(float), (void *)(2 * sizeof(float)));
|
||||
g_VertTexLoc, 2, GL_FLOAT, GL_FALSE, Vertex2D_Length * sizeof(float), (void *)(2 * sizeof(float)));
|
||||
glVertexAttribPointer(
|
||||
vertColorLoc, 4, GL_FLOAT, GL_FALSE, Vertex2D_Length * sizeof(float), (void *)(4 * sizeof(float)));
|
||||
g_VertColorLoc, 4, GL_FLOAT, GL_FALSE, Vertex2D_Length * sizeof(float), (void *)(4 * sizeof(float)));
|
||||
|
||||
glEnableVertexAttribArray(vertPosLoc);
|
||||
glEnableVertexAttribArray(vertTexLoc);
|
||||
glEnableVertexAttribArray(vertColorLoc);
|
||||
glEnableVertexAttribArray(g_VertPosLoc);
|
||||
glEnableVertexAttribArray(g_VertTexLoc);
|
||||
glEnableVertexAttribArray(g_VertColorLoc);
|
||||
|
||||
unsigned char whiteTexData[4] = {255, 255, 255, 255};
|
||||
_whiteTex = Draw_UploadGLTexture(1, 1, whiteTexData);
|
||||
g_WhiteTex = Draw_UploadGLTexture(1, 1, whiteTexData);
|
||||
|
||||
#endif
|
||||
|
||||
// Set the proyection (2D)
|
||||
glViewport(0, 0, _width, _height);
|
||||
// Set the projection (2D)
|
||||
glViewport(0, 0, g_Width, g_Height);
|
||||
float projectionMatrix[16] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
|
||||
projectionMatrix[0] = (2.0f / _width);
|
||||
projectionMatrix[5] = -(2.0f / _height);
|
||||
projectionMatrix[0] = (2.0f / (float)g_Width);
|
||||
projectionMatrix[5] = -(2.0f / (float)g_Height);
|
||||
projectionMatrix[10] = -0.001f;
|
||||
projectionMatrix[3] = -1.0f;
|
||||
projectionMatrix[7] = 1.0f;
|
||||
@@ -306,7 +306,7 @@ int Draw_Init(int width, int height, char *title, int pfps, int fps) {
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
|
||||
// Initialize the triangle array
|
||||
_quadArray = QuadArray2D_Create(400);
|
||||
g_QuadArray = QuadArray2D_Create(400);
|
||||
|
||||
Draw_SetColor(1.0f, 1.0f, 1.0f, 1.0f);
|
||||
|
||||
@@ -327,10 +327,6 @@ void Draw_ShowInfo() {
|
||||
Print(" Renderer: %s\n", str);
|
||||
str = (char *)glGetString(GL_VERSION);
|
||||
Print(" Version: %s\n", str);
|
||||
GLint major, minor;
|
||||
glGetIntegerv(GL_MAJOR_VERSION, &major);
|
||||
glGetIntegerv(GL_MINOR_VERSION, &minor);
|
||||
Print(" Version : %d.%d\n", major, minor);
|
||||
str = (char *)glGetString(GL_SHADING_LANGUAGE_VERSION);
|
||||
Print(" Shader Version: %s\n", str);
|
||||
Print("*********************************\n");
|
||||
@@ -362,7 +358,7 @@ void Draw_SetMatrix(float matrix[16]) {
|
||||
glLoadMatrixf(tempMatrix);
|
||||
#endif
|
||||
#if USE_OpenGLES
|
||||
glUniformMatrix4fv(projectionMatrixLoc, 1, GL_FALSE, matrix);
|
||||
glUniformMatrix4fv(g_ProjectionMatrixLoc, 1, GL_FALSE, matrix);
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -397,32 +393,33 @@ GLuint Draw_UploadGLTexture(int w, int h, unsigned char *pixels) {
|
||||
//
|
||||
// Performs all the queued draw actions.
|
||||
void Draw_Flush() {
|
||||
if (_currentImg == NULL || _quadArray->nVertex <= 0) {
|
||||
if (g_CurrentImg == NULL || g_QuadArray->nVertex <= 0) {
|
||||
return;
|
||||
}
|
||||
if (_currentImg->tex == -1) {
|
||||
_currentImg->tex = Draw_UploadGLTexture(_currentImg->w, _currentImg->h, _currentImg->data);
|
||||
if (g_CurrentImg->tex == -1) {
|
||||
g_CurrentImg->tex = Draw_UploadGLTexture(g_CurrentImg->w, g_CurrentImg->h, g_CurrentImg->data);
|
||||
}
|
||||
|
||||
#if USE_OpenGL
|
||||
// Draw the quad array
|
||||
glBindTexture(GL_TEXTURE_2D, _currentImg->tex);
|
||||
glColorPointer(4, GL_FLOAT, Vertex2D_Length * sizeof(float), (GLvoid *)(_quadArray->vertexData + 4));
|
||||
glTexCoordPointer(2, GL_FLOAT, Vertex2D_Length * sizeof(float), (GLvoid *)(_quadArray->vertexData + 2));
|
||||
glVertexPointer(2, GL_FLOAT, Vertex2D_Length * sizeof(float), (GLvoid *)(_quadArray->vertexData));
|
||||
glDrawArrays(GL_TRIANGLES, 0, _quadArray->nVertex);
|
||||
glBindTexture(GL_TEXTURE_2D, g_CurrentImg->tex);
|
||||
glColorPointer(4, GL_FLOAT, Vertex2D_Length * sizeof(float), (GLvoid *)(g_QuadArray->vertexData + 4));
|
||||
glTexCoordPointer(2, GL_FLOAT, Vertex2D_Length * sizeof(float), (GLvoid *)(g_QuadArray->vertexData + 2));
|
||||
glVertexPointer(2, GL_FLOAT, Vertex2D_Length * sizeof(float), (GLvoid *)(g_QuadArray->vertexData));
|
||||
glDrawArrays(GL_TRIANGLES, 0, g_QuadArray->nVertex);
|
||||
|
||||
#else
|
||||
|
||||
// Draw the quad array
|
||||
glBindTexture(GL_TEXTURE_2D, _currentImg->tex);
|
||||
glBufferSubData(GL_ARRAY_BUFFER, 0, Vertex2D_Length * sizeof(float) * _quadArray->nVertex, _quadArray->vertexData);
|
||||
glDrawArrays(GL_TRIANGLES, 0, _quadArray->nVertex);
|
||||
glBindTexture(GL_TEXTURE_2D, g_CurrentImg->tex);
|
||||
glBufferSubData(
|
||||
GL_ARRAY_BUFFER, 0, Vertex2D_Length * sizeof(float) * g_QuadArray->nVertex, g_QuadArray->vertexData);
|
||||
glDrawArrays(GL_TRIANGLES, 0, g_QuadArray->nVertex);
|
||||
|
||||
#endif
|
||||
|
||||
// Empty it
|
||||
QuadArray2D_Clean(_quadArray);
|
||||
QuadArray2D_Clean(g_QuadArray);
|
||||
}
|
||||
|
||||
/////////////////////////////
|
||||
@@ -435,35 +432,37 @@ void Draw_Clean(unsigned char r, unsigned char g, unsigned char b) {
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
#else
|
||||
Draw_Flush();
|
||||
float fr = r / 255.0f;
|
||||
float fg = g / 255.0f;
|
||||
float fb = b / 255.0f;
|
||||
float fr = (float)r / 255.0f;
|
||||
float fg = (float)g / 255.0f;
|
||||
float fb = (float)b / 255.0f;
|
||||
float fWidth = (float)g_Width;
|
||||
float fHeight = (float)g_Height;
|
||||
GLfloat vVertices[] = {
|
||||
0.0, 0.0, // Position 0
|
||||
0.0, 0.0, // TexCoord 0
|
||||
fr, fg, fb, 1.0, // Color
|
||||
0.0f, 0.0f, // Position 0
|
||||
0.0f, 0.0f, // TexCoord 0
|
||||
fr, fg, fb, 1.0f, // Color
|
||||
|
||||
0.0, _height, // Position 1
|
||||
0.0, 1.0, // TexCoord 1
|
||||
fr, fg, fb, 1.0, // Color
|
||||
0.0f, fHeight, // Position 1
|
||||
0.0f, 1.0f, // TexCoord 1
|
||||
fr, fg, fb, 1.0f, // Color
|
||||
|
||||
_width, _height, // Position 2
|
||||
1.0, 1.0, // TexCoord 2
|
||||
fr, fg, fb, 1.0, // Color
|
||||
fWidth, fHeight, // Position 2
|
||||
1.0f, 1.0f, // TexCoord 2
|
||||
fr, fg, fb, 1.0f, // Color
|
||||
|
||||
_width, _height, // Position 2
|
||||
1.0, 1.0, // TexCoord 2
|
||||
fr, fg, fb, 1.0, // Color
|
||||
fWidth, fHeight, // Position 2
|
||||
1.0f, 1.0f, // TexCoord 2
|
||||
fr, fg, fb, 1.0f, // Color
|
||||
|
||||
_width, 0.0, // Position 3
|
||||
1.0, 0.0, // TexCoord 3
|
||||
fr, fg, fb, 1.0, // Color
|
||||
fWidth, 0.0f, // Position 3
|
||||
1.0f, 0.0f, // TexCoord 3
|
||||
fr, fg, fb, 1.0f, // Color
|
||||
|
||||
0.0, 0.0, // Position 0
|
||||
0.0, 0.0, // TexCoord 0
|
||||
fr, fg, fb, 1.0, // Color
|
||||
0.0f, 0.0f, // Position 0
|
||||
0.0f, 0.0f, // TexCoord 0
|
||||
fr, fg, fb, 1.0f, // Color
|
||||
};
|
||||
glBindTexture(GL_TEXTURE_2D, _whiteTex);
|
||||
glBindTexture(GL_TEXTURE_2D, g_WhiteTex);
|
||||
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vVertices), vVertices);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 6);
|
||||
#endif
|
||||
@@ -472,13 +471,13 @@ void Draw_Clean(unsigned char r, unsigned char g, unsigned char b) {
|
||||
/////////////////////////////
|
||||
// Draw_LoopIteration
|
||||
//
|
||||
// One iteracion of the loop updating the game window.
|
||||
void (*_proc_func)(void *data) = NULL;
|
||||
void (*_draw_func)(void *data, float f) = NULL;
|
||||
void *_data = NULL;
|
||||
int _draw_looping = 0;
|
||||
int _draw_exitoverrided = 0;
|
||||
long long _accTime;
|
||||
// One iteration of the loop updating the game window.
|
||||
void (*g_ProcFunc)(void *data) = NULL;
|
||||
void (*g_DrawFunc)(void *data, float f) = NULL;
|
||||
void *g_Data = NULL;
|
||||
int g_DrawLooping = 0;
|
||||
int g_DrawExitOverride = 0;
|
||||
long long g_AccumulatedTime;
|
||||
int Draw_LoopIteration() {
|
||||
SDL_Event event;
|
||||
|
||||
@@ -494,10 +493,10 @@ int Draw_LoopIteration() {
|
||||
}
|
||||
}
|
||||
if (event.type == SDL_MOUSEMOTION) {
|
||||
Input_SetPointerPosition(event.motion.x / (float)_width, event.motion.y / (float)_height);
|
||||
Input_SetPointerPosition(event.motion.x / (float)g_Width, event.motion.y / (float)g_Height);
|
||||
}
|
||||
if (event.type == SDL_MOUSEBUTTONDOWN) {
|
||||
Input_SetPointerPosition(event.button.x / (float)_width, event.button.y / (float)_height);
|
||||
Input_SetPointerPosition(event.button.x / (float)g_Width, event.button.y / (float)g_Height);
|
||||
Input_SetPointerDown(1);
|
||||
}
|
||||
if (event.type == SDL_FINGERMOTION) {
|
||||
@@ -508,7 +507,7 @@ int Draw_LoopIteration() {
|
||||
Input_SetPointerDown(1);
|
||||
}
|
||||
if (event.type == SDL_MOUSEBUTTONUP) {
|
||||
Input_SetPointerPosition(event.button.x / (float)_width, event.button.y / (float)_height);
|
||||
Input_SetPointerPosition(event.button.x / (float)g_Width, event.button.y / (float)g_Height);
|
||||
Input_SetPointerDown(0);
|
||||
}
|
||||
if (event.type == SDL_FINGERUP) {
|
||||
@@ -520,41 +519,41 @@ int Draw_LoopIteration() {
|
||||
while (SDL_PollEvent(&event)) {
|
||||
if (event.type == SDL_QUIT) {
|
||||
Input_SetKey(InputKey_Exit, 1);
|
||||
if (!_draw_exitoverrided) {
|
||||
_draw_looping = 0;
|
||||
if (!g_DrawExitOverride) {
|
||||
g_DrawLooping = 0;
|
||||
}
|
||||
}
|
||||
if (event.type == SDL_KEYDOWN) {
|
||||
if (event.key.keysym.scancode == SDL_SCANCODE_ESCAPE) {
|
||||
Input_SetKey(InputKey_Exit, 1);
|
||||
if (!_draw_exitoverrided) {
|
||||
_draw_looping = 0;
|
||||
if (!g_DrawExitOverride) {
|
||||
g_DrawLooping = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (event.type == SDL_MOUSEMOTION) {
|
||||
Input_SetPointerPosition(event.motion.x / (float)_width, event.motion.y / (float)_height);
|
||||
Input_SetPointerPosition((float)event.motion.x / (float)g_Width, (float)event.motion.y / (float)g_Height);
|
||||
}
|
||||
if (event.type == SDL_MOUSEBUTTONDOWN) {
|
||||
Input_SetPointerPosition(event.button.x / (float)_width, event.button.y / (float)_height);
|
||||
Input_SetPointerPosition((float)event.button.x / (float)g_Width, (float)event.button.y / (float)g_Height);
|
||||
Input_SetPointerDown(1);
|
||||
}
|
||||
if (event.type == SDL_MOUSEBUTTONUP) {
|
||||
Input_SetPointerPosition(event.button.x / (float)_width, event.button.y / (float)_height);
|
||||
Input_SetPointerPosition((float)event.button.x / (float)g_Width, (float)event.button.y / (float)g_Height);
|
||||
Input_SetPointerDown(0);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
// Process
|
||||
if (_proc_func) {
|
||||
if (_accTime > 100000) {
|
||||
_accTime = 100000;
|
||||
if (g_ProcFunc) {
|
||||
if (g_AccumulatedTime > 100000) {
|
||||
g_AccumulatedTime = 100000;
|
||||
}
|
||||
while (_accTime >= proc_t_frame && _draw_looping) {
|
||||
while (g_AccumulatedTime >= g_ProcTFrame && g_DrawLooping) {
|
||||
Input_Frame();
|
||||
_proc_func(_data);
|
||||
_accTime -= proc_t_frame;
|
||||
g_ProcFunc(g_Data);
|
||||
g_AccumulatedTime -= g_ProcTFrame;
|
||||
Input_PostFrame();
|
||||
}
|
||||
}
|
||||
@@ -563,13 +562,13 @@ int Draw_LoopIteration() {
|
||||
Audio_Frame();
|
||||
|
||||
// Draw
|
||||
SDL_GL_SwapWindow(_window);
|
||||
if (_draw_func) {
|
||||
_draw_func(_data, (float)_accTime / (float)proc_t_frame);
|
||||
SDL_GL_SwapWindow(g_Window);
|
||||
if (g_DrawFunc) {
|
||||
g_DrawFunc(g_Data, (float)g_AccumulatedTime / (float)g_ProcTFrame);
|
||||
Draw_Flush();
|
||||
}
|
||||
|
||||
return _draw_looping;
|
||||
return g_DrawLooping;
|
||||
}
|
||||
|
||||
#ifdef EMSCRIPTEN
|
||||
@@ -580,7 +579,7 @@ void Draw_LoopIterationAux() {
|
||||
|
||||
// Update time
|
||||
_procTime2 = Time_GetTime();
|
||||
_accTime += _procTime2 - _procTime1;
|
||||
g_AccumulatedTime += _procTime2 - _procTime1;
|
||||
_procTime1 = _procTime2;
|
||||
}
|
||||
#endif
|
||||
@@ -591,35 +590,35 @@ void Draw_LoopIterationAux() {
|
||||
// Loops updating the game window.
|
||||
void Draw_Loop(void (*proc)(void *data), void (*draw)(void *data, float f), void *data) {
|
||||
|
||||
_proc_func = proc;
|
||||
_draw_func = draw;
|
||||
_data = data;
|
||||
if (_draw_looping) {
|
||||
g_ProcFunc = proc;
|
||||
g_DrawFunc = draw;
|
||||
g_Data = data;
|
||||
if (g_DrawLooping) {
|
||||
return;
|
||||
}
|
||||
_draw_looping = 1;
|
||||
g_DrawLooping = 1;
|
||||
#ifndef EMSCRIPTEN
|
||||
long long procTime1, procTime2, drawTime1, drawTime2;
|
||||
_accTime = proc_t_frame;
|
||||
g_AccumulatedTime = g_ProcTFrame;
|
||||
procTime1 = drawTime1 = Time_GetTime();
|
||||
while (Draw_LoopIteration()) {
|
||||
|
||||
// Wait to round draw_t_frame
|
||||
// Wait to round g_DrawTFrame
|
||||
drawTime2 = Time_GetTime();
|
||||
Time_Pause(draw_t_frame - (drawTime2 - drawTime1));
|
||||
Time_Pause(g_DrawTFrame - (drawTime2 - drawTime1));
|
||||
drawTime2 = Time_GetTime();
|
||||
drawTime1 = drawTime2;
|
||||
|
||||
// Update time
|
||||
procTime2 = Time_GetTime();
|
||||
_accTime += procTime2 - procTime1;
|
||||
g_AccumulatedTime += procTime2 - procTime1;
|
||||
procTime1 = procTime2;
|
||||
}
|
||||
#else
|
||||
_accTime = proc_t_frame;
|
||||
_procTime1 = Time_GetTime();
|
||||
if (_fps <= 50) {
|
||||
emscripten_set_main_loop(Draw_LoopIterationAux, _fps, 1);
|
||||
g_AccumulatedTime = g_ProcTFrame;
|
||||
_procTime1 = Time_GetTime();
|
||||
if (g_FPS <= 50) {
|
||||
emscripten_set_main_loop(Draw_LoopIterationAux, g_FPS, 1);
|
||||
} else {
|
||||
emscripten_set_main_loop(Draw_LoopIterationAux, 0, 1);
|
||||
}
|
||||
@@ -632,7 +631,7 @@ void Draw_Loop(void (*proc)(void *data), void (*draw)(void *data, float f), void
|
||||
// Breaks the drawing loop
|
||||
void Draw_BreakLoop() {
|
||||
#ifndef EMSCRIPTEN
|
||||
_draw_looping = 0;
|
||||
g_DrawLooping = 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -640,7 +639,7 @@ void Draw_BreakLoop() {
|
||||
// Draw_OverrideExit
|
||||
//
|
||||
// Overrides the default exit mechanism
|
||||
void Draw_OverrideExit(int override) { _draw_exitoverrided = override; }
|
||||
void Draw_OverrideExit(int override) { g_DrawExitOverride = override; }
|
||||
|
||||
/////////////////////////////
|
||||
// Draw_CreateImage
|
||||
@@ -664,7 +663,7 @@ DrawImg Draw_CreateImage(int w, int h) {
|
||||
/////////////////////////////
|
||||
// Draw_LoadImage
|
||||
//
|
||||
// Loads a image, giving a reference.
|
||||
// Loads an image, giving a reference.
|
||||
DrawImg Draw_LoadImage(char *filename) {
|
||||
DrawImage image;
|
||||
|
||||
@@ -737,17 +736,17 @@ int Draw_GetFlip(DrawImg img) {
|
||||
// Draw_DrawImg
|
||||
//
|
||||
// Draws an image.
|
||||
void Draw_DrawImg(DrawImg img, int x, int y, float scale[2]) {
|
||||
void Draw_DrawImg(DrawImg img, int x, int y, const float scale[2]) {
|
||||
DrawImage image = img;
|
||||
float x1, x2, y1, y2;
|
||||
float u1 = 0.0f, u2 = 1.0f;
|
||||
float v1 = 0.0f, v2 = 1.0f;
|
||||
|
||||
// Prepare screen coordinates
|
||||
x1 = x + (image->x * scale[0]);
|
||||
y1 = y + (image->y * scale[1]);
|
||||
x2 = x1 + (image->w * scale[0]);
|
||||
y2 = y1 + (image->h * scale[1]);
|
||||
x1 = (float)x + ((float)image->x * scale[0]);
|
||||
y1 = (float)y + ((float)image->y * scale[1]);
|
||||
x2 = x1 + ((float)image->w * scale[0]);
|
||||
y2 = y1 + ((float)image->h * scale[1]);
|
||||
|
||||
// Apply flipping
|
||||
if (image->flip & 1) {
|
||||
@@ -762,11 +761,11 @@ void Draw_DrawImg(DrawImg img, int x, int y, float scale[2]) {
|
||||
}
|
||||
|
||||
// Draw a quad
|
||||
if (_currentImg != image) {
|
||||
if (g_CurrentImg != image) {
|
||||
Draw_Flush();
|
||||
_currentImg = image;
|
||||
g_CurrentImg = image;
|
||||
}
|
||||
QuadArray2D_AddQuad(_quadArray, x1, y1, u1, v1, x2, y2, u2, v2, _color);
|
||||
QuadArray2D_AddQuad(g_QuadArray, x1, y1, u1, v1, x2, y2, u2, v2, g_Color);
|
||||
}
|
||||
|
||||
/////////////////////////////
|
||||
@@ -775,13 +774,13 @@ void Draw_DrawImg(DrawImg img, int x, int y, float scale[2]) {
|
||||
// Draws an image, resizing.
|
||||
void Draw_DrawImgResized(DrawImg img, int x, int y, float w, float h) {
|
||||
DrawImage image = img;
|
||||
int x1, x2, y1, y2;
|
||||
float x1, x2, y1, y2;
|
||||
float u1 = 0.0f, u2 = 1.0f;
|
||||
float v1 = 0.0f, v2 = 1.0f;
|
||||
|
||||
// Prepare
|
||||
x1 = x + image->x;
|
||||
y1 = y + image->y;
|
||||
x1 = (float)x + (float)image->x;
|
||||
y1 = (float)y + (float)image->y;
|
||||
x2 = x1 + w;
|
||||
y2 = y1 + h;
|
||||
|
||||
@@ -798,36 +797,36 @@ void Draw_DrawImgResized(DrawImg img, int x, int y, float w, float h) {
|
||||
}
|
||||
|
||||
// Draw a quad
|
||||
if (_currentImg != image) {
|
||||
if (g_CurrentImg != image) {
|
||||
Draw_Flush();
|
||||
_currentImg = image;
|
||||
g_CurrentImg = image;
|
||||
}
|
||||
QuadArray2D_AddQuad(_quadArray, x1, y1, u1, v1, x2, y2, u2, v2, _color);
|
||||
QuadArray2D_AddQuad(g_QuadArray, x1, y1, u1, v1, x2, y2, u2, v2, g_Color);
|
||||
}
|
||||
|
||||
/////////////////////////////
|
||||
// Draw_DrawImgPart
|
||||
//
|
||||
// Draws an image part.
|
||||
void Draw_DrawImgPart(DrawImg img, int x, int y, int w, int h, int i, int j, float scale[2]) {
|
||||
void Draw_DrawImgPart(DrawImg img, int x, int y, int w, int h, int i, int j, const float scale[2]) {
|
||||
DrawImage image = img;
|
||||
int x1, x2, y1, y2;
|
||||
float x1, x2, y1, y2;
|
||||
float us, u1, u2;
|
||||
float vs, v1, v2;
|
||||
|
||||
// Prepare screen coordinates
|
||||
x1 = x + (image->x * scale[0]);
|
||||
y1 = y + (image->y * scale[1]);
|
||||
x2 = x1 + (w * scale[0]);
|
||||
y2 = y1 + (h * scale[1]);
|
||||
x1 = (float)x + ((float)image->x * scale[0]);
|
||||
y1 = (float)y + ((float)image->y * scale[1]);
|
||||
x2 = x1 + ((float)w * scale[0]);
|
||||
y2 = y1 + ((float)h * scale[1]);
|
||||
|
||||
// Prepare image coordinates
|
||||
us = 1.0f / image->w;
|
||||
u1 = us * i * w;
|
||||
u2 = u1 + (us * w);
|
||||
vs = 1.0f / image->h;
|
||||
v1 = vs * j * h;
|
||||
v2 = v1 + (vs * h);
|
||||
us = 1.0f / (float)image->w;
|
||||
u1 = us * (float)(i * w);
|
||||
u2 = u1 + (us * (float)w);
|
||||
vs = 1.0f / (float)image->h;
|
||||
v1 = vs * (float)(j * h);
|
||||
v2 = v1 + (vs * (float)h);
|
||||
|
||||
// Apply flipping
|
||||
if (image->flip & 1) {
|
||||
@@ -842,33 +841,33 @@ void Draw_DrawImgPart(DrawImg img, int x, int y, int w, int h, int i, int j, flo
|
||||
}
|
||||
|
||||
// Draw a quad
|
||||
if (_currentImg != image) {
|
||||
if (g_CurrentImg != image) {
|
||||
Draw_Flush();
|
||||
_currentImg = image;
|
||||
g_CurrentImg = image;
|
||||
}
|
||||
QuadArray2D_AddQuad(_quadArray, x1, y1, u1, v1, x2, y2, u2, v2, _color);
|
||||
QuadArray2D_AddQuad(g_QuadArray, x1, y1, u1, v1, x2, y2, u2, v2, g_Color);
|
||||
}
|
||||
|
||||
/////////////////////////////
|
||||
// Draw_DrawImgPartHoriz
|
||||
//
|
||||
// Draws an image part horizontally.
|
||||
void Draw_DrawImgPartHoriz(DrawImg img, int x, int y, int w, int i, float scale[2]) {
|
||||
void Draw_DrawImgPartHoriz(DrawImg img, int x, int y, int w, int i, const float scale[2]) {
|
||||
DrawImage image = img;
|
||||
int x1, x2, y1, y2;
|
||||
float x1, x2, y1, y2;
|
||||
float us, u1, u2;
|
||||
float v1 = 0.0f, v2 = 1.0f;
|
||||
|
||||
// Prepare screen coordinates
|
||||
x1 = x + (image->x * scale[0]);
|
||||
y1 = y + (image->y * scale[1]);
|
||||
x2 = x1 + (w * scale[0]);
|
||||
y2 = y1 + (image->h * scale[1]);
|
||||
x1 = (float)x + ((float)image->x * scale[0]);
|
||||
y1 = (float)y + ((float)image->y * scale[1]);
|
||||
x2 = x1 + ((float)w * scale[0]);
|
||||
y2 = y1 + ((float)image->h * scale[1]);
|
||||
|
||||
// Prepare image coordinates
|
||||
us = 1.0f / image->w;
|
||||
u1 = us * i * w;
|
||||
u2 = u1 + us * w;
|
||||
us = 1.0f / (float)image->w;
|
||||
u1 = us * (float)(i * w);
|
||||
u2 = u1 + (us * (float)w);
|
||||
|
||||
// Apply flipping
|
||||
if (image->flip & 1) {
|
||||
@@ -883,11 +882,11 @@ void Draw_DrawImgPartHoriz(DrawImg img, int x, int y, int w, int i, float scale[
|
||||
}
|
||||
|
||||
// Draw a quad
|
||||
if (_currentImg != image) {
|
||||
if (g_CurrentImg != image) {
|
||||
Draw_Flush();
|
||||
_currentImg = image;
|
||||
g_CurrentImg = image;
|
||||
}
|
||||
QuadArray2D_AddQuad(_quadArray, x1, y1, u1, v1, x2, y2, u2, v2, _color);
|
||||
QuadArray2D_AddQuad(g_QuadArray, x1, y1, u1, v1, x2, y2, u2, v2, g_Color);
|
||||
}
|
||||
|
||||
/////////////////////////////
|
||||
@@ -895,28 +894,29 @@ void Draw_DrawImgPartHoriz(DrawImg img, int x, int y, int w, int i, float scale[
|
||||
//
|
||||
//
|
||||
void Draw_ImgParallax(
|
||||
DrawImg img, int imgSize[2], int imgOffset[2], float parallaxFactor[2], int gamePos[2], int gameSize[2]) {
|
||||
int paralaxPos[2];
|
||||
int mult[2];
|
||||
DrawImg img, int imgSize[2], const int imgOffset[2], const float parallaxFactor[2], const int gamePos[2],
|
||||
const int gameSize[2]) {
|
||||
int parallaxPos[2];
|
||||
int multiply[2];
|
||||
int x, y;
|
||||
|
||||
paralaxPos[0] = (gamePos[0] * parallaxFactor[0]) + imgOffset[0];
|
||||
paralaxPos[1] = (gamePos[1] * parallaxFactor[1]) + imgOffset[1];
|
||||
parallaxPos[0] = (int)((float)gamePos[0] * parallaxFactor[0]) + imgOffset[0];
|
||||
parallaxPos[1] = (int)((float)gamePos[1] * parallaxFactor[1]) + imgOffset[1];
|
||||
|
||||
mult[0] = floor(paralaxPos[0] / imgSize[0]);
|
||||
if (paralaxPos[0] < 0) {
|
||||
mult[0]--;
|
||||
multiply[0] = (int)floorf((float)parallaxPos[0] / (float)imgSize[0]);
|
||||
if (parallaxPos[0] < 0) {
|
||||
multiply[0]--;
|
||||
}
|
||||
mult[1] = floor(paralaxPos[1] / imgSize[1]);
|
||||
if (paralaxPos[1] < 0) {
|
||||
mult[1]--;
|
||||
multiply[1] = (int)floorf((float)parallaxPos[1] / (float)imgSize[1]);
|
||||
if (parallaxPos[1] < 0) {
|
||||
multiply[1]--;
|
||||
}
|
||||
|
||||
y = (mult[1] * imgSize[1]) - paralaxPos[1];
|
||||
y = (multiply[1] * imgSize[1]) - parallaxPos[1];
|
||||
while (y < gameSize[1]) {
|
||||
x = (mult[0] * imgSize[0]) - paralaxPos[0];
|
||||
x = (multiply[0] * imgSize[0]) - parallaxPos[0];
|
||||
while (x < gameSize[0]) {
|
||||
Draw_DrawImgResized(img, x, y, imgSize[0], imgSize[1]);
|
||||
Draw_DrawImgResized(img, x, y, (float)imgSize[0], (float)imgSize[1]);
|
||||
x += imgSize[0];
|
||||
}
|
||||
y += imgSize[1];
|
||||
@@ -928,10 +928,10 @@ void Draw_ImgParallax(
|
||||
//
|
||||
//
|
||||
void Draw_SetColor(float r, float g, float b, float a) {
|
||||
_color[0] = r;
|
||||
_color[1] = g;
|
||||
_color[2] = b;
|
||||
_color[3] = a;
|
||||
g_Color[0] = r;
|
||||
g_Color[1] = g;
|
||||
g_Color[2] = b;
|
||||
g_Color[3] = a;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////
|
||||
@@ -948,7 +948,7 @@ typedef struct {
|
||||
/////////////////////////////
|
||||
// Draw_DefaultImage
|
||||
//
|
||||
// Creates a image with the default font.
|
||||
// Creates an image with the default font.
|
||||
#include "FontData.h"
|
||||
DrawImage Draw_DefaultFontImage(unsigned char r, unsigned char g, unsigned char b, unsigned char a) {
|
||||
DrawImage img;
|
||||
@@ -965,7 +965,7 @@ DrawImage Draw_DefaultFontImage(unsigned char r, unsigned char g, unsigned char
|
||||
img->data[offset + 0] = r;
|
||||
img->data[offset + 1] = g;
|
||||
img->data[offset + 2] = b;
|
||||
if (((fontdata_8x8[c * 8 + y] >> (7 - x)) & 0x01) == 1) {
|
||||
if (((fontData_8x8[c * 8 + y] >> (7 - x)) & 0x01) == 1) {
|
||||
img->data[offset + 3] = a;
|
||||
} else {
|
||||
img->data[offset + 3] = 0x00;
|
||||
@@ -1020,7 +1020,7 @@ DrawFnt Draw_LoadFont(char *fichero, int min, int max) {
|
||||
/////////////////////////////
|
||||
// Draw_FontScale
|
||||
//
|
||||
void Draw_FontScale(DrawFnt f, float scale[2]) {
|
||||
void Draw_FontScale(DrawFnt f, const float scale[2]) {
|
||||
DrawFont *font = f;
|
||||
|
||||
font->scale[0] = scale[0];
|
||||
@@ -1041,7 +1041,7 @@ void Draw_DrawText(DrawFnt f, char *text, int x, int y) {
|
||||
if ((*ptr) < font->max) {
|
||||
Draw_DrawImgPartHoriz(font->img, x, y, font->w, (*ptr) - font->min, font->scale);
|
||||
}
|
||||
x += font->w * font->scale[0];
|
||||
x += (int)((float)font->w * font->scale[0]);
|
||||
ptr++;
|
||||
}
|
||||
}
|
||||
@@ -1095,34 +1095,34 @@ void Draw_SaveRGBAToPNG(char *filename, unsigned char *data, int width, int heig
|
||||
}
|
||||
|
||||
/////////////////////////////
|
||||
// Draw_SaveScreenshoot
|
||||
// Draw_SaveScreenshot
|
||||
//
|
||||
//
|
||||
void Draw_SaveScreenshoot(char *filename) {
|
||||
void Draw_SaveScreenshot(char *filename) {
|
||||
#if USE_OpenGL
|
||||
unsigned char *pixelData;
|
||||
unsigned char *image_line;
|
||||
int i, half_height, line_size;
|
||||
|
||||
pixelData = malloc(_width * _height * 4);
|
||||
pixelData = malloc(g_Width * g_Height * 4);
|
||||
|
||||
glReadPixels(0, 0, _width, _height, GL_RGBA, GL_UNSIGNED_BYTE, pixelData);
|
||||
glReadPixels(0, 0, g_Width, g_Height, GL_RGBA, GL_UNSIGNED_BYTE, pixelData);
|
||||
|
||||
// Flip the image data
|
||||
line_size = _width * 4;
|
||||
half_height = _height / 2;
|
||||
line_size = g_Width * 4;
|
||||
half_height = g_Height / 2;
|
||||
image_line = malloc(line_size);
|
||||
for (i = 0; i < half_height; i++) {
|
||||
memcpy(image_line, pixelData + i * line_size, line_size);
|
||||
memcpy(pixelData + i * line_size, pixelData + (_height - (i + 1)) * line_size, line_size);
|
||||
memcpy(pixelData + (_height - (i + 1)) * line_size, image_line, line_size);
|
||||
memcpy(pixelData + i * line_size, pixelData + (g_Height - (i + 1)) * line_size, line_size);
|
||||
memcpy(pixelData + (g_Height - (i + 1)) * line_size, image_line, line_size);
|
||||
}
|
||||
|
||||
// Save the image
|
||||
if (EndsWith(filename, ".bmp") || EndsWith(filename, ".BMP")) {
|
||||
Draw_SaveRGBAToBMP(filename, pixelData, _width, _height);
|
||||
Draw_SaveRGBAToBMP(filename, pixelData, g_Width, g_Height);
|
||||
} else if (EndsWith(filename, ".png") || EndsWith(filename, ".PNG")) {
|
||||
Draw_SaveRGBAToPNG(filename, pixelData, _width, _height);
|
||||
Draw_SaveRGBAToPNG(filename, pixelData, g_Width, g_Height);
|
||||
}
|
||||
|
||||
// Cleanup
|
||||
|
||||
26
src/Draw.h
26
src/Draw.h
@@ -1,13 +1,13 @@
|
||||
// Copyright (C) 2011-2015 Valeriano Alfonso Rodriguez (Kableado)
|
||||
// Copyright (C) 2011-2023 Valeriano Alfonso Rodriguez (Kableado)
|
||||
|
||||
#ifndef _DRAW_H_
|
||||
#define _DRAW_H_
|
||||
#ifndef Draw_H
|
||||
#define Draw_H
|
||||
|
||||
/////////////////////////////
|
||||
// Draw_Init
|
||||
//
|
||||
// Initializes the game window.
|
||||
int Draw_Init(int width, int height, char *title, int pfps, int fps);
|
||||
int Draw_Init(int width, int height, char *title, int pFps, int fps);
|
||||
|
||||
/////////////////////////////
|
||||
// Draw_Clean
|
||||
@@ -42,7 +42,7 @@ void Draw_Flush();
|
||||
////////////////////////////////////////////////
|
||||
// DrawImg //
|
||||
/////////////
|
||||
// Reference to a image.
|
||||
// Reference to an image.
|
||||
typedef void *DrawImg;
|
||||
|
||||
/////////////////////////////
|
||||
@@ -53,7 +53,7 @@ DrawImg Draw_CreateImage(int w, int h);
|
||||
/////////////////////////////
|
||||
// Draw_LoadImage
|
||||
//
|
||||
// Loads a image, giving a reference.
|
||||
// Loads an image, giving a reference.
|
||||
DrawImg Draw_LoadImage(char *filename);
|
||||
|
||||
/////////////////////////////
|
||||
@@ -82,7 +82,7 @@ int Draw_GetFlip(DrawImg img);
|
||||
// Draw_DrawImg
|
||||
//
|
||||
// Draws an image.
|
||||
void Draw_DrawImg(DrawImg img, int x, int y, float scale[2]);
|
||||
void Draw_DrawImg(DrawImg img, int x, int y, const float scale[2]);
|
||||
|
||||
/////////////////////////////
|
||||
// Draw_DrawImgResized
|
||||
@@ -94,20 +94,20 @@ void Draw_DrawImgResized(DrawImg img, int x, int y, float w, float h);
|
||||
// Draw_DrawImgPart
|
||||
//
|
||||
// Draws an image part.
|
||||
void Draw_DrawImgPart(DrawImg img, int x, int y, int w, int h, int i, int j, float scale[2]);
|
||||
void Draw_DrawImgPart(DrawImg img, int x, int y, int w, int h, int i, int j, const float scale[2]);
|
||||
|
||||
/////////////////////////////
|
||||
// Draw_DrawImgPartHoriz
|
||||
//
|
||||
// Draws an image part horizontally.
|
||||
void Draw_DrawImgPartHoriz(DrawImg img, int x, int y, int w, int i, float scale[2]);
|
||||
void Draw_DrawImgPartHoriz(DrawImg img, int x, int y, int w, int i, const float scale[2]);
|
||||
|
||||
/////////////////////////////
|
||||
// Draw_ImgParallax
|
||||
//
|
||||
//
|
||||
void Draw_ImgParallax(
|
||||
DrawImg img, int imgSize[2], int imgOffset[2], float parallaxFactor[2], int gamePos[2], int gameSize[2]);
|
||||
DrawImg img, int imgSize[2], const int imgOffset[2], const float parallaxFactor[2], const int gamePos[2], const int gameSize[2]);
|
||||
|
||||
/////////////////////////////
|
||||
// Draw_SetColor
|
||||
@@ -136,7 +136,7 @@ DrawFnt Draw_LoadFont(char *fichero, int min, int max);
|
||||
/////////////////////////////
|
||||
// Draw_FontScale
|
||||
//
|
||||
void Draw_FontScale(DrawFnt f, float scale[2]);
|
||||
void Draw_FontScale(DrawFnt f, const float scale[2]);
|
||||
|
||||
/////////////////////////////
|
||||
// Draw_DrawText
|
||||
@@ -157,10 +157,10 @@ void Draw_SaveRGBAToBMP(char *filename, unsigned char *data, int width, int heig
|
||||
void Draw_SaveRGBAToPNG(char *filename, unsigned char *data, int width, int height);
|
||||
|
||||
/////////////////////////////
|
||||
// Draw_SaveScreenshoot
|
||||
// Draw_SaveScreenshot
|
||||
//
|
||||
//
|
||||
void Draw_SaveScreenshoot(char *filename);
|
||||
void Draw_SaveScreenshot(char *filename);
|
||||
|
||||
/////////////////////////////
|
||||
// Draw_ShowCursor
|
||||
|
||||
101
src/Entity.c
101
src/Entity.c
@@ -1,4 +1,4 @@
|
||||
// Copyright (C) 2011-2014 Valeriano Alfonso Rodriguez (Kableado)
|
||||
// Copyright (C) 2011-2023 Valeriano Alfonso Rodriguez (Kableado)
|
||||
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
@@ -16,29 +16,38 @@
|
||||
#define EntityIntFlag_UpdateColor 8
|
||||
#define EntityIntFlag_UpdatedScale 16
|
||||
|
||||
/////////////////////////////
|
||||
// Entity_GetFree
|
||||
//
|
||||
//
|
||||
Entity g_FreeEntity = NULL;
|
||||
Entity Entity_GetFree() {
|
||||
Entity e;
|
||||
|
||||
if (!g_FreeEntity) {
|
||||
// Allocate a big block of entities
|
||||
int n = 1024, i;
|
||||
TEntity *newEntities = malloc(sizeof(TEntity) * n);
|
||||
for (i = 0; i < n; i++) {
|
||||
if (i < (n - 1)) {
|
||||
newEntities[i].next = &newEntities[i + 1];
|
||||
} else {
|
||||
newEntities[i].next = NULL;
|
||||
}
|
||||
}
|
||||
g_FreeEntity = newEntities;
|
||||
}
|
||||
e = g_FreeEntity;
|
||||
g_FreeEntity = e->next;
|
||||
return e;
|
||||
}
|
||||
|
||||
/////////////////////////////
|
||||
// Entity_New
|
||||
//
|
||||
//
|
||||
Entity _free_entity = NULL;
|
||||
Entity Entity_New() {
|
||||
Entity e;
|
||||
|
||||
if (!_free_entity) {
|
||||
// Allocate a big block of entities
|
||||
int n = 1024, i;
|
||||
TEntity *newEnts = malloc(sizeof(TEntity) * n);
|
||||
for (i = 0; i < n; i++) {
|
||||
if (i < (n - 1)) {
|
||||
newEnts[i].next = &newEnts[i + 1];
|
||||
} else {
|
||||
newEnts[i].next = NULL;
|
||||
}
|
||||
}
|
||||
_free_entity = newEnts;
|
||||
}
|
||||
e = _free_entity;
|
||||
_free_entity = e->next;
|
||||
Entity e = Entity_GetFree();
|
||||
|
||||
e->base = NULL;
|
||||
e->type = 0;
|
||||
@@ -114,8 +123,8 @@ void Entity_Destroy(Entity e) {
|
||||
if (e->ondelete) {
|
||||
e->ondelete(e);
|
||||
}
|
||||
e->next = _free_entity;
|
||||
_free_entity = e;
|
||||
e->next = g_FreeEntity;
|
||||
g_FreeEntity = e;
|
||||
}
|
||||
|
||||
/////////////////////////////
|
||||
@@ -255,7 +264,7 @@ void Entity_Draw(Entity e, int x, int y, float f) {
|
||||
}
|
||||
if (e->internalFlags & EntityIntFlag_UpdatedPos) {
|
||||
vec2_interpol(fPos, e->pos0, e->pos, f);
|
||||
AnimPlay_Draw(&e->anim, fPos[0] + x, fPos[1] + y, scale);
|
||||
AnimPlay_Draw(&e->anim, (int)(fPos[0] + x), fPos[1] + y, scale);
|
||||
} else {
|
||||
AnimPlay_Draw(&e->anim, e->pos[0] + x, e->pos[1] + y, scale);
|
||||
}
|
||||
@@ -357,15 +366,15 @@ void Entity_PostProcess(Entity e, int ft) {
|
||||
// CollisionInfo_New
|
||||
//
|
||||
//
|
||||
CollisionInfo _free_collInfo = NULL;
|
||||
CollisionInfo CollisionInfo_New(int responseType, Entity ent1, Entity ent2, float t, vec2 n, int applyFriction) {
|
||||
CollisionInfo g_FreeCollisionInfo = NULL;
|
||||
CollisionInfo CollisionInfo_New(int responseType, Entity ent1, Entity ent2, float t, const vec2 n, int applyFriction) {
|
||||
CollisionInfo collInfo;
|
||||
|
||||
if (!_free_collInfo) {
|
||||
if (!g_FreeCollisionInfo) {
|
||||
collInfo = malloc(sizeof(TCollisionInfo));
|
||||
} else {
|
||||
collInfo = _free_collInfo;
|
||||
_free_collInfo = collInfo->next;
|
||||
collInfo = g_FreeCollisionInfo;
|
||||
g_FreeCollisionInfo = collInfo->next;
|
||||
}
|
||||
collInfo->next = NULL;
|
||||
|
||||
@@ -391,10 +400,10 @@ void CollisionInfo_Destroy(CollisionInfo *collInfoRef) {
|
||||
CollisionInfo collInfo = collInfoRef[0];
|
||||
CollisionInfo nextCollInfo;
|
||||
while (collInfo != NULL) {
|
||||
nextCollInfo = collInfo->next;
|
||||
collInfo->next = _free_collInfo;
|
||||
_free_collInfo = collInfo;
|
||||
collInfo = nextCollInfo;
|
||||
nextCollInfo = collInfo->next;
|
||||
collInfo->next = g_FreeCollisionInfo;
|
||||
g_FreeCollisionInfo = collInfo;
|
||||
collInfo = nextCollInfo;
|
||||
}
|
||||
collInfoRef[0] = NULL;
|
||||
}
|
||||
@@ -533,7 +542,7 @@ int Entity_CheckCollision(Entity ent1, Entity ent2, CollisionInfo *collInfoRef)
|
||||
|
||||
// Circle-Circle test from ent1
|
||||
vec2_minus(vel, ent1->vel, ent2->vel);
|
||||
if (Colision_CircleCircle(ent1->pos, ent1->radius, vel, ent2->pos, ent2->radius, &t, n)) {
|
||||
if (Collision_CircleCircle(ent1->pos, ent1->radius, vel, ent2->pos, ent2->radius, &t, n)) {
|
||||
CollisionInfo_Add(collInfoRef, CollisionResponse_Circle, ent1, ent2, t, n, 0);
|
||||
return (1);
|
||||
}
|
||||
@@ -544,7 +553,7 @@ int Entity_CheckCollision(Entity ent1, Entity ent2, CollisionInfo *collInfoRef)
|
||||
// Entity_CollisionResponseCircle
|
||||
//
|
||||
// Normal response to a collision between circles.
|
||||
void Entity_CollisionResponseCircle(Entity b1, Entity b2, float t, vec2 n) {
|
||||
void Entity_CollisionResponseCircle(Entity b1, Entity b2, float t, const vec2 n) {
|
||||
float moment;
|
||||
vec2 temp;
|
||||
float elast;
|
||||
@@ -716,7 +725,7 @@ void Entity_GetPos(Entity e, vec2 pos) { vec2_copy(pos, e->pos); }
|
||||
// Entity_SetPos
|
||||
//
|
||||
//
|
||||
void Entity_SetPos(Entity e, vec2 pos) {
|
||||
void Entity_SetPos(Entity e, const vec2 pos) {
|
||||
vec2_copy(e->pos, pos);
|
||||
vec2_copy(e->oldpos, pos);
|
||||
vec2_copy(e->pos0, pos);
|
||||
@@ -727,7 +736,7 @@ void Entity_SetPos(Entity e, vec2 pos) {
|
||||
// Entity_AddPos
|
||||
//
|
||||
//
|
||||
void Entity_AddPos(Entity e, vec2 pos) {
|
||||
void Entity_AddPos(Entity e, const vec2 pos) {
|
||||
vec2_plus(e->pos, e->pos, pos);
|
||||
vec2_copy(e->oldpos, e->pos);
|
||||
vec2_copy(e->pos0, e->pos);
|
||||
@@ -738,7 +747,7 @@ void Entity_AddPos(Entity e, vec2 pos) {
|
||||
// Entity_UpdatePos
|
||||
//
|
||||
//
|
||||
void Entity_UpdatePos(Entity e, vec2 pos) {
|
||||
void Entity_UpdatePos(Entity e, const vec2 pos) {
|
||||
|
||||
// Mark the update of the position.
|
||||
vec2_copy(e->oldpos, e->pos);
|
||||
@@ -750,7 +759,7 @@ void Entity_UpdatePos(Entity e, vec2 pos) {
|
||||
/////////////////////////////
|
||||
// Entity_AddVel
|
||||
//
|
||||
void Entity_AddVel(Entity e, vec2 vel) {
|
||||
void Entity_AddVel(Entity e, const vec2 vel) {
|
||||
vec2_plus(e->vel, e->vel, vel);
|
||||
Entity_CalcBBox(e);
|
||||
}
|
||||
@@ -758,7 +767,7 @@ void Entity_AddVel(Entity e, vec2 vel) {
|
||||
/////////////////////////////
|
||||
// Entity_SetVel
|
||||
//
|
||||
void Entity_SetVel(Entity e, vec2 vel) {
|
||||
void Entity_SetVel(Entity e, const vec2 vel) {
|
||||
vec2_copy(e->vel, vel);
|
||||
Entity_CalcBBox(e);
|
||||
}
|
||||
@@ -783,7 +792,7 @@ void Entity_SetVelV(Entity e, float v) {
|
||||
// Entity_AddVelLimit
|
||||
//
|
||||
//
|
||||
void Entity_AddVelLimit(Entity e, vec2 vel, float limit) {
|
||||
void Entity_AddVelLimit(Entity e, const vec2 vel, float limit) {
|
||||
float vlen_orig, vlen;
|
||||
vec2 dir, vel_temp;
|
||||
|
||||
@@ -915,7 +924,7 @@ void Entity_SetDefaultColor(Entity e, float r, float g, float b, float a) {
|
||||
/////////////////////////////
|
||||
// Entity_SetScale
|
||||
//
|
||||
void Entity_SetScale(Entity e, float scale[2]) {
|
||||
void Entity_SetScale(Entity e, const float scale[2]) {
|
||||
e->scale[0] = scale[0];
|
||||
e->scale[1] = scale[1];
|
||||
e->internalFlags |= EntityIntFlag_UpdatedScale;
|
||||
@@ -933,7 +942,7 @@ void Entity_GetScale(Entity e, float scale[2]) {
|
||||
// Entity_Iluminate
|
||||
//
|
||||
//
|
||||
void Entity_Iluminate(Entity e, Entity *elist, int n) {
|
||||
void Entity_Iluminate(Entity e, Entity *entityList, int n) {
|
||||
int i;
|
||||
vec2 vdist;
|
||||
float qdist, f;
|
||||
@@ -951,15 +960,17 @@ void Entity_Iluminate(Entity e, Entity *elist, int n) {
|
||||
e->color[3] = e->color[3];
|
||||
|
||||
for (i = 0; i < n; i++) {
|
||||
if (e == elist[i] || !(elist[i]->flags & EntityFlag_Light))
|
||||
if (e == entityList[i] || !(entityList[i]->flags & EntityFlag_Light)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
vec2_minus(vdist, e->pos, elist[i]->pos);
|
||||
vec2_minus(vdist, e->pos, entityList[i]->pos);
|
||||
qdist = vec2_dot(vdist, vdist);
|
||||
qrad = elist[i]->light[3] * elist[i]->light[3];
|
||||
qrad = entityList[i]->light[3] * entityList[i]->light[3];
|
||||
if (qdist < qrad) {
|
||||
f = 1.0f - qdist / qrad;
|
||||
Entity_AddColor(e, f * elist[i]->light[0], f * elist[i]->light[1], f * elist[i]->light[2], 0.0f);
|
||||
Entity_AddColor(
|
||||
e, f * entityList[i]->light[0], f * entityList[i]->light[1], f * entityList[i]->light[2], 0.0f);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
31
src/Entity.h
31
src/Entity.h
@@ -1,7 +1,7 @@
|
||||
// Copyright (C) 2011-2014 Valeriano Alfonso Rodriguez (Kableado)
|
||||
// Copyright (C) 2011-2023 Valeriano Alfonso Rodriguez (Kableado)
|
||||
|
||||
#ifndef _ENTITY_H_
|
||||
#define _ENTITY_H_
|
||||
#ifndef Entity_H
|
||||
#define Entity_H
|
||||
|
||||
#include "Anim.h"
|
||||
#include "Draw.h"
|
||||
@@ -80,6 +80,11 @@ struct TEntity {
|
||||
Entity next;
|
||||
};
|
||||
|
||||
/////////////////////////////
|
||||
// Entity_GetFree
|
||||
//
|
||||
Entity Entity_GetFree();
|
||||
|
||||
/////////////////////////////
|
||||
// Entity_New
|
||||
//
|
||||
@@ -153,7 +158,7 @@ struct TCollisionInfo {
|
||||
// CollisionInfo_New
|
||||
//
|
||||
//
|
||||
CollisionInfo CollisionInfo_New(int responseType, Entity ent1, Entity ent2, float t, vec2 n, int applyFriction);
|
||||
CollisionInfo CollisionInfo_New(int responseType, Entity ent1, Entity ent2, float t, const vec2 n, int applyFriction);
|
||||
|
||||
/////////////////////////////
|
||||
// CollisionInfo_Destroy
|
||||
@@ -184,7 +189,7 @@ int Entity_CheckCollision(Entity ent1, Entity ent2, CollisionInfo *collInfoRef);
|
||||
// Entity_CollisionResponseClircle
|
||||
//
|
||||
// Normal response to a collision of spheres.
|
||||
void Entity_CollisionResponseCircle(Entity b1, Entity b2, float t, vec2 n);
|
||||
void Entity_CollisionResponseCircle(Entity b1, Entity b2, float t, const vec2 n);
|
||||
|
||||
/////////////////////////////
|
||||
// Entity_CollisionResponseLine
|
||||
@@ -210,9 +215,9 @@ void Entity_Overlaps(Entity b1, Entity b2);
|
||||
// Entity_UpdatePos
|
||||
//
|
||||
void Entity_GetPos(Entity e, vec2 pos);
|
||||
void Entity_SetPos(Entity e, vec2 pos);
|
||||
void Entity_AddPos(Entity e, vec2 pos);
|
||||
void Entity_UpdatePos(Entity e, vec2 pos);
|
||||
void Entity_SetPos(Entity e, const vec2 pos);
|
||||
void Entity_AddPos(Entity e, const vec2 pos);
|
||||
void Entity_UpdatePos(Entity e, const vec2 pos);
|
||||
|
||||
/////////////////////////////
|
||||
// Entity_AddVel
|
||||
@@ -223,11 +228,11 @@ void Entity_UpdatePos(Entity e, vec2 pos);
|
||||
// Entity_AddVelLimitH
|
||||
// Entity_AddVelLimitH
|
||||
//
|
||||
void Entity_AddVel(Entity e, vec2 vel);
|
||||
void Entity_SetVel(Entity e, vec2 vel);
|
||||
void Entity_AddVel(Entity e, const vec2 vel);
|
||||
void Entity_SetVel(Entity e, const vec2 vel);
|
||||
void Entity_SetVelH(Entity e, float v);
|
||||
void Entity_SetVelV(Entity e, float v);
|
||||
void Entity_AddVelLimit(Entity e, vec2 vel, float limit);
|
||||
void Entity_AddVelLimit(Entity e, const vec2 vel, float limit);
|
||||
void Entity_AddVelLimitH(Entity e, float v, float limit);
|
||||
void Entity_AddVelLimitV(Entity e, float v, float limit);
|
||||
|
||||
@@ -248,13 +253,13 @@ void Entity_SetDefaultColor(Entity e, float r, float g, float b, float a);
|
||||
// Entity_SetScale
|
||||
// Entity_GetScale
|
||||
//
|
||||
void Entity_SetScale(Entity e, float scale[2]);
|
||||
void Entity_SetScale(Entity e, const float scale[2]);
|
||||
void Entity_GetScale(Entity e, float scale[2]);
|
||||
|
||||
/////////////////////////////
|
||||
// Entity_Iluminate
|
||||
//
|
||||
void Entity_Iluminate(Entity e, Entity *elist, int n);
|
||||
void Entity_Iluminate(Entity e, Entity *entityList, int n);
|
||||
|
||||
/////////////////////////////
|
||||
// Entity_MarkUpdateLight
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// Copyright (C) 2011 Valeriano Alfonso Rodriguez (Kableado)
|
||||
// Copyright (C) 2011-2023 Valeriano Alfonso Rodriguez (Kableado)
|
||||
|
||||
static unsigned char fontdata_8x8[2048] = {
|
||||
static unsigned char fontData_8x8[2048] = {
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //
|
||||
|
||||
458
src/GameLib.c
458
src/GameLib.c
@@ -1,38 +1,29 @@
|
||||
// Copyright (C) 2011 Valeriano Alfonso Rodriguez (Kableado)
|
||||
// Copyright (C) 2011-2023 Valeriano Alfonso Rodriguez (Kableado)
|
||||
|
||||
#include <SDL.h>
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "Anim.h"
|
||||
#include "Audio.h"
|
||||
#include "Draw.h"
|
||||
#include "Entity.h"
|
||||
#include "Input.h"
|
||||
#include "TimeUtils.h"
|
||||
#include "Util.h"
|
||||
|
||||
#include "GameLib.h"
|
||||
|
||||
// Globals
|
||||
Entity *_entity = NULL;
|
||||
int *_entity_flag = NULL;
|
||||
int _n_entities = 0;
|
||||
int _n_entities_res = 0;
|
||||
int _entities_lock = 0;
|
||||
int _entities_compactate = 0;
|
||||
void (*_gameproc)() = NULL;
|
||||
void (*_gamepostproc)() = NULL;
|
||||
void (*_gamepredraw)(float f) = NULL;
|
||||
void (*_gamedraw)(float f) = NULL;
|
||||
int _pft;
|
||||
int _game_size[2];
|
||||
int _game_pos0[2];
|
||||
int _game_pos1[2];
|
||||
int _game_posOffset[2];
|
||||
Entity *g_Entity = NULL;
|
||||
int *g_EntityFlag = NULL;
|
||||
int g_NumEntities = 0;
|
||||
int g_NumEntitiesAllocated = 0;
|
||||
int g_EntitiesLock = 0;
|
||||
int g_EntitiesCompactate = 0;
|
||||
void (*g_GameProcFunc)() = NULL;
|
||||
void (*g_GamePostProcFunc)() = NULL;
|
||||
void (*g_GamePreDrawFunc)(float f) = NULL;
|
||||
void (*g_GameDrawFunc)(float f) = NULL;
|
||||
int g_ProcFt;
|
||||
int g_GameSize[2];
|
||||
int g_GamePos0[2];
|
||||
int g_GamePos1[2];
|
||||
int g_GamePosOffset[2];
|
||||
|
||||
long long t_proc;
|
||||
long long t_col;
|
||||
@@ -50,17 +41,15 @@ struct TParallaxBackground {
|
||||
float parallaxFactor[2];
|
||||
};
|
||||
#define MaxParallaxBackgrounds 10
|
||||
TParallaxBackground _parallaxBackground[MaxParallaxBackgrounds];
|
||||
int _nParallaxBackgrounds = 0;
|
||||
|
||||
int gamelib_debug = 0;
|
||||
TParallaxBackground g_ParallaxBackground[MaxParallaxBackgrounds];
|
||||
int g_NumParallaxBackgrounds = 0;
|
||||
|
||||
/////////////////////////////
|
||||
// GameLib_Init
|
||||
//
|
||||
// Initializes the game.
|
||||
int GameLib_Init(int w, int h, char *title, int pfps, int fps) {
|
||||
if (!Draw_Init(w, h, title, pfps, fps)) {
|
||||
int GameLib_Init(int w, int h, char *title, int pFps, int fps) {
|
||||
if (!Draw_Init(w, h, title, pFps, fps)) {
|
||||
return (0);
|
||||
}
|
||||
if (!Input_Init()) {
|
||||
@@ -68,14 +57,14 @@ int GameLib_Init(int w, int h, char *title, int pfps, int fps) {
|
||||
}
|
||||
Audio_Init();
|
||||
|
||||
_game_size[0] = w;
|
||||
_game_size[1] = h;
|
||||
_game_pos0[0] = 0;
|
||||
_game_pos0[1] = 0;
|
||||
_game_pos1[0] = 0;
|
||||
_game_pos1[1] = 0;
|
||||
g_GameSize[0] = w;
|
||||
g_GameSize[1] = h;
|
||||
g_GamePos0[0] = 0;
|
||||
g_GamePos0[1] = 0;
|
||||
g_GamePos1[0] = 0;
|
||||
g_GamePos1[1] = 0;
|
||||
|
||||
_pft = 1000 / pfps;
|
||||
g_ProcFt = 1000 / pFps;
|
||||
|
||||
return (1);
|
||||
}
|
||||
@@ -85,37 +74,38 @@ int GameLib_Init(int w, int h, char *title, int pfps, int fps) {
|
||||
//
|
||||
// Adds an entity to the game.
|
||||
void GameLib_AddEntity(Entity e) {
|
||||
if (_n_entities >= _n_entities_res) {
|
||||
if (g_NumEntities >= g_NumEntitiesAllocated) {
|
||||
Entity *entity_aux;
|
||||
int *entity_flag_aux;
|
||||
int i;
|
||||
|
||||
// Grow the array
|
||||
if (_n_entities_res == 0)
|
||||
_n_entities_res = 32;
|
||||
else
|
||||
_n_entities_res *= 2;
|
||||
entity_aux = malloc(sizeof(Entity) * _n_entities_res);
|
||||
entity_flag_aux = malloc(sizeof(int) * _n_entities_res);
|
||||
for (i = 0; i < _n_entities; i++) {
|
||||
entity_aux[i] = _entity[i];
|
||||
entity_flag_aux[i] = _entity_flag[i];
|
||||
if (g_NumEntitiesAllocated == 0) {
|
||||
g_NumEntitiesAllocated = 32;
|
||||
} else {
|
||||
g_NumEntitiesAllocated *= 2;
|
||||
}
|
||||
if (_entity) {
|
||||
free(_entity);
|
||||
free(_entity_flag);
|
||||
entity_aux = malloc(sizeof(Entity) * g_NumEntitiesAllocated);
|
||||
entity_flag_aux = malloc(sizeof(int) * g_NumEntitiesAllocated);
|
||||
for (i = 0; i < g_NumEntities; i++) {
|
||||
entity_aux[i] = g_Entity[i];
|
||||
entity_flag_aux[i] = g_EntityFlag[i];
|
||||
}
|
||||
_entity = entity_aux;
|
||||
_entity_flag = entity_flag_aux;
|
||||
if (g_Entity) {
|
||||
free(g_Entity);
|
||||
free(g_EntityFlag);
|
||||
}
|
||||
g_Entity = entity_aux;
|
||||
g_EntityFlag = entity_flag_aux;
|
||||
}
|
||||
|
||||
// Add the entity
|
||||
_entity[_n_entities] = e;
|
||||
_entity_flag[_n_entities] = 1;
|
||||
_n_entities++;
|
||||
g_Entity[g_NumEntities] = e;
|
||||
g_EntityFlag[g_NumEntities] = 1;
|
||||
g_NumEntities++;
|
||||
|
||||
// Mark for light update
|
||||
Entity_MarkUpdateLight(e, _entity, _n_entities);
|
||||
Entity_MarkUpdateLight(e, g_Entity, g_NumEntities);
|
||||
|
||||
Entity_CalcBBox(e);
|
||||
|
||||
@@ -128,19 +118,19 @@ void GameLib_AddEntity(Entity e) {
|
||||
// removes the reference to the entity.
|
||||
int GameLib_UnrefEntity(Entity e) {
|
||||
int i;
|
||||
for (i = 0; i < _n_entities; i++) {
|
||||
if (e == _entity[i]) {
|
||||
for (i = 0; i < g_NumEntities; i++) {
|
||||
if (e == g_Entity[i]) {
|
||||
// Mark or unref
|
||||
if (_entities_lock) {
|
||||
_entity_flag[i] = -2;
|
||||
if (g_EntitiesLock) {
|
||||
g_EntityFlag[i] = -2;
|
||||
} else {
|
||||
_entity[i] = NULL;
|
||||
_entity_flag[i] = 0;
|
||||
g_Entity[i] = NULL;
|
||||
g_EntityFlag[i] = 0;
|
||||
}
|
||||
_entities_compactate = 1;
|
||||
g_EntitiesCompactate = 1;
|
||||
|
||||
// Mark for light update
|
||||
Entity_MarkUpdateLight(e, _entity, _n_entities);
|
||||
Entity_MarkUpdateLight(e, g_Entity, g_NumEntities);
|
||||
return (i);
|
||||
}
|
||||
}
|
||||
@@ -156,10 +146,10 @@ int GameLib_DelEntity(Entity e) {
|
||||
if ((i = GameLib_UnrefEntity(e)) == -1) {
|
||||
return (0);
|
||||
}
|
||||
if (_entities_lock) {
|
||||
if (g_EntitiesLock) {
|
||||
// Delete latter
|
||||
_entity[i] = e;
|
||||
_entity_flag[i] = -1;
|
||||
g_Entity[i] = e;
|
||||
g_EntityFlag[i] = -1;
|
||||
} else {
|
||||
// Delete now
|
||||
Entity_Destroy(e);
|
||||
@@ -174,23 +164,25 @@ int GameLib_DelEntity(Entity e) {
|
||||
void GameLib_Compactate() {
|
||||
int i, j;
|
||||
j = 0;
|
||||
if (!_entities_compactate)
|
||||
if (!g_EntitiesCompactate) {
|
||||
return;
|
||||
for (i = 0; i < _n_entities; i++) {
|
||||
if (!_entity[i] || _entity_flag[i] == -2)
|
||||
}
|
||||
for (i = 0; i < g_NumEntities; i++) {
|
||||
if (!g_Entity[i] || g_EntityFlag[i] == -2) {
|
||||
continue;
|
||||
if (_entity_flag[i] == -1) {
|
||||
Entity_Destroy(_entity[i]);
|
||||
}
|
||||
if (g_EntityFlag[i] == -1) {
|
||||
Entity_Destroy(g_Entity[i]);
|
||||
continue;
|
||||
}
|
||||
if (i > j) {
|
||||
_entity[j] = _entity[i];
|
||||
_entity_flag[j] = _entity_flag[i];
|
||||
g_Entity[j] = g_Entity[i];
|
||||
g_EntityFlag[j] = g_EntityFlag[i];
|
||||
}
|
||||
j++;
|
||||
}
|
||||
_n_entities = j;
|
||||
_entities_compactate = 0;
|
||||
g_NumEntities = j;
|
||||
g_EntitiesCompactate = 0;
|
||||
}
|
||||
|
||||
/////////////////////////////
|
||||
@@ -203,45 +195,47 @@ void GameLib_ProcLoop(void *data) {
|
||||
long long time;
|
||||
|
||||
// Step the gamePosition
|
||||
_game_pos0[0] = _game_pos1[0] + _game_posOffset[0];
|
||||
_game_pos0[1] = _game_pos1[1] + _game_posOffset[1];
|
||||
g_GamePos0[0] = g_GamePos1[0] + g_GamePosOffset[0];
|
||||
g_GamePos0[1] = g_GamePos1[1] + g_GamePosOffset[1];
|
||||
|
||||
// Process
|
||||
time = Time_GetTime();
|
||||
_entities_lock = 1;
|
||||
if (_gameproc) {
|
||||
_gameproc();
|
||||
g_EntitiesLock = 1;
|
||||
if (g_GameProcFunc) {
|
||||
g_GameProcFunc();
|
||||
}
|
||||
for (i = 0; i < _n_entities; i++) {
|
||||
if (!_entity[i])
|
||||
for (i = 0; i < g_NumEntities; i++) {
|
||||
if (!g_Entity[i]) {
|
||||
continue;
|
||||
Entity_Process(_entity[i], _pft);
|
||||
}
|
||||
Entity_Process(g_Entity[i], g_ProcFt);
|
||||
}
|
||||
GameLib_Compactate();
|
||||
_entities_lock = 0;
|
||||
g_EntitiesLock = 0;
|
||||
t_proc += Time_GetTime() - time;
|
||||
|
||||
// Colisions between entities
|
||||
// Collisions between entities
|
||||
time = Time_GetTime();
|
||||
_entities_lock = 1;
|
||||
g_EntitiesLock = 1;
|
||||
count = 0;
|
||||
do {
|
||||
repeat = 0;
|
||||
CollisionInfo collInfo = NULL;
|
||||
for (i = 0; i < _n_entities; i++) {
|
||||
if (!(_entity[i]->flags & EntityFlag_Collision) || _entity[i]->mass < 0.0f)
|
||||
continue;
|
||||
if (_entity[i]->vel[0] <= 0.0f && _entity[i]->vel[0] >= -0.0f && _entity[i]->vel[1] <= 0.0f &&
|
||||
_entity[i]->vel[1] >= -0.0f) {
|
||||
for (i = 0; i < g_NumEntities; i++) {
|
||||
if (!(g_Entity[i]->flags & EntityFlag_Collision) || g_Entity[i]->mass < 0.0f) {
|
||||
continue;
|
||||
}
|
||||
for (j = 0; j < _n_entities; j++) {
|
||||
if (i == j || !(_entity[j]->flags & EntityFlag_Collision) ||
|
||||
CollisionInfo_CheckRepetition(collInfo, _entity[i], _entity[j]) ||
|
||||
!Entity_BBoxIntersect(_entity[i], _entity[j])) {
|
||||
if (g_Entity[i]->vel[0] <= 0.0f && g_Entity[i]->vel[0] >= -0.0f && g_Entity[i]->vel[1] <= 0.0f &&
|
||||
g_Entity[i]->vel[1] >= -0.0f) {
|
||||
continue;
|
||||
}
|
||||
for (j = 0; j < g_NumEntities; j++) {
|
||||
if (i == j || !(g_Entity[j]->flags & EntityFlag_Collision) ||
|
||||
CollisionInfo_CheckRepetition(collInfo, g_Entity[i], g_Entity[j]) ||
|
||||
!Entity_BBoxIntersect(g_Entity[i], g_Entity[j])) {
|
||||
continue;
|
||||
}
|
||||
Entity_CheckCollision(_entity[i], _entity[j], &collInfo);
|
||||
Entity_CheckCollision(g_Entity[i], g_Entity[j], &collInfo);
|
||||
}
|
||||
}
|
||||
if (Entity_CollisionInfoResponse(collInfo)) {
|
||||
@@ -253,51 +247,54 @@ void GameLib_ProcLoop(void *data) {
|
||||
|
||||
// Stop remaining collisions
|
||||
if (count == 10) {
|
||||
for (i = 0; i < _n_entities; i++) {
|
||||
if (!(_entity[i]->flags & EntityFlag_Collision) || _entity[i]->mass < 0.0f)
|
||||
for (i = 0; i < g_NumEntities; i++) {
|
||||
if (!(g_Entity[i]->flags & EntityFlag_Collision) || g_Entity[i]->mass < 0.0f) {
|
||||
continue;
|
||||
for (j = 0; j < _n_entities; j++) {
|
||||
if (i == j || !(_entity[j]->flags & EntityFlag_Collision) ||
|
||||
!Entity_BBoxIntersect(_entity[i], _entity[j])) {
|
||||
}
|
||||
for (j = 0; j < g_NumEntities; j++) {
|
||||
if (i == j || !(g_Entity[j]->flags & EntityFlag_Collision) ||
|
||||
!Entity_BBoxIntersect(g_Entity[i], g_Entity[j])) {
|
||||
continue;
|
||||
}
|
||||
if (Entity_CheckCollision(_entity[i], _entity[j], NULL)) {
|
||||
vec2_set(_entity[i]->vel, 0, 0);
|
||||
Entity_CalcBBox(_entity[i]);
|
||||
vec2_set(_entity[j]->vel, 0, 0);
|
||||
Entity_CalcBBox(_entity[j]);
|
||||
if (Entity_CheckCollision(g_Entity[i], g_Entity[j], NULL)) {
|
||||
vec2_set(g_Entity[i]->vel, 0, 0);
|
||||
Entity_CalcBBox(g_Entity[i]);
|
||||
vec2_set(g_Entity[j]->vel, 0, 0);
|
||||
Entity_CalcBBox(g_Entity[j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
GameLib_Compactate();
|
||||
_entities_lock = 0;
|
||||
g_EntitiesLock = 0;
|
||||
t_col += Time_GetTime() - time;
|
||||
|
||||
// Process Overlaps
|
||||
time = Time_GetTime();
|
||||
_entities_lock = 1;
|
||||
for (i = 0; i < _n_entities; i++) {
|
||||
if (!(_entity[i]->flags & EntityFlag_Overlap) || _entity[i]->mass < 0.0f)
|
||||
g_EntitiesLock = 1;
|
||||
for (i = 0; i < g_NumEntities; i++) {
|
||||
if (!(g_Entity[i]->flags & EntityFlag_Overlap) || g_Entity[i]->mass < 0.0f) {
|
||||
continue;
|
||||
for (j = 0; j < _n_entities; j++) {
|
||||
if (!(_entity[j]->flags & EntityFlag_Overlap) || i == j)
|
||||
}
|
||||
for (j = 0; j < g_NumEntities; j++) {
|
||||
if (!(g_Entity[j]->flags & EntityFlag_Overlap) || i == j) {
|
||||
continue;
|
||||
Entity_Overlaps(_entity[i], _entity[j]);
|
||||
}
|
||||
Entity_Overlaps(g_Entity[i], g_Entity[j]);
|
||||
}
|
||||
}
|
||||
GameLib_Compactate();
|
||||
_entities_lock = 0;
|
||||
g_EntitiesLock = 0;
|
||||
t_over += Time_GetTime() - time;
|
||||
|
||||
// Sort
|
||||
int n, n2, swap;
|
||||
n = _n_entities;
|
||||
n = g_NumEntities;
|
||||
do {
|
||||
n2 = 0;
|
||||
for (i = 1; i < n; i++) {
|
||||
Entity ent1 = _entity[i - 1];
|
||||
Entity ent2 = _entity[i];
|
||||
Entity ent1 = g_Entity[i - 1];
|
||||
Entity ent2 = g_Entity[i];
|
||||
swap = 0;
|
||||
if (ent1->zorder > ent2->zorder) {
|
||||
// Lower level
|
||||
@@ -318,10 +315,10 @@ void GameLib_ProcLoop(void *data) {
|
||||
}
|
||||
if (swap) {
|
||||
Entity ent;
|
||||
ent = _entity[i];
|
||||
_entity[i] = _entity[i - 1];
|
||||
_entity[i - 1] = ent;
|
||||
n2 = i;
|
||||
ent = g_Entity[i];
|
||||
g_Entity[i] = g_Entity[i - 1];
|
||||
g_Entity[i - 1] = ent;
|
||||
n2 = i;
|
||||
}
|
||||
}
|
||||
n = n2;
|
||||
@@ -329,18 +326,18 @@ void GameLib_ProcLoop(void *data) {
|
||||
|
||||
// PostProcess
|
||||
time = Time_GetTime();
|
||||
_entities_lock = 1;
|
||||
for (i = 0; i < _n_entities; i++) {
|
||||
Entity_PostProcess(_entity[i], _pft);
|
||||
if (Entity_IsMoving(_entity[i])) {
|
||||
Entity_MarkUpdateLight(_entity[i], _entity, _n_entities);
|
||||
g_EntitiesLock = 1;
|
||||
for (i = 0; i < g_NumEntities; i++) {
|
||||
Entity_PostProcess(g_Entity[i], g_ProcFt);
|
||||
if (Entity_IsMoving(g_Entity[i])) {
|
||||
Entity_MarkUpdateLight(g_Entity[i], g_Entity, g_NumEntities);
|
||||
}
|
||||
}
|
||||
if (_gamepostproc) {
|
||||
_gamepostproc();
|
||||
if (g_GamePostProcFunc) {
|
||||
g_GamePostProcFunc();
|
||||
}
|
||||
GameLib_Compactate();
|
||||
_entities_lock = 0;
|
||||
g_EntitiesLock = 0;
|
||||
t_postproc += Time_GetTime() - time;
|
||||
|
||||
fproc_count++;
|
||||
@@ -360,50 +357,50 @@ void GameLib_DrawLoop(void *data, float f) {
|
||||
time = Time_GetTime();
|
||||
|
||||
// PreDraw
|
||||
if (_gamepredraw) {
|
||||
_gamepredraw(f);
|
||||
if (g_GamePreDrawFunc) {
|
||||
g_GamePreDrawFunc(f);
|
||||
} else {
|
||||
if (_nParallaxBackgrounds == 0) {
|
||||
if (g_NumParallaxBackgrounds == 0) {
|
||||
// Clean screen
|
||||
Draw_Clean(0, 0, 0);
|
||||
}
|
||||
}
|
||||
|
||||
// Draw parallax backgrounds
|
||||
for (i = 0; i < _nParallaxBackgrounds; i++) {
|
||||
for (i = 0; i < g_NumParallaxBackgrounds; i++) {
|
||||
Draw_ImgParallax(
|
||||
_parallaxBackground[i].img,
|
||||
_parallaxBackground[i].imgSize,
|
||||
_parallaxBackground[i].imgOffset,
|
||||
_parallaxBackground[i].parallaxFactor,
|
||||
g_ParallaxBackground[i].img,
|
||||
g_ParallaxBackground[i].imgSize,
|
||||
g_ParallaxBackground[i].imgOffset,
|
||||
g_ParallaxBackground[i].parallaxFactor,
|
||||
game_pos,
|
||||
_game_size);
|
||||
g_GameSize);
|
||||
}
|
||||
|
||||
// Draw entities
|
||||
GameLib_Compactate();
|
||||
for (i = 0; i < _n_entities; i++) {
|
||||
Entity e = _entity[i];
|
||||
for (i = 0; i < g_NumEntities; i++) {
|
||||
Entity e = g_Entity[i];
|
||||
|
||||
// Check visivility
|
||||
if (!Entity_IsVisible(e, game_pos[0], game_pos[1], _game_size[0], _game_size[1])) {
|
||||
// Check visibility
|
||||
if (!Entity_IsVisible(e, game_pos[0], game_pos[1], g_GameSize[0], g_GameSize[1])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Update ilumination of this entity
|
||||
// Update illumination of this entity
|
||||
if (Entity_IsUpdateLight(e)) {
|
||||
Entity_Iluminate(e, _entity, _n_entities);
|
||||
Entity_Iluminate(e, g_Entity, g_NumEntities);
|
||||
}
|
||||
|
||||
Entity_Draw(e, -game_pos[0], -game_pos[1], f);
|
||||
}
|
||||
Draw_SetColor(1, 1, 1, 1);
|
||||
_entities_lock = 1;
|
||||
if (_gamedraw) {
|
||||
_gamedraw(f);
|
||||
g_EntitiesLock = 1;
|
||||
if (g_GameDrawFunc) {
|
||||
g_GameDrawFunc(f);
|
||||
}
|
||||
GameLib_Compactate();
|
||||
_entities_lock = 0;
|
||||
g_EntitiesLock = 0;
|
||||
|
||||
t_draw += Time_GetTime() - time;
|
||||
|
||||
@@ -418,7 +415,7 @@ void GameLib_DrawLoop(void *data, float f) {
|
||||
idx++;
|
||||
snprintf(strFile, 255, "shot-%04d.png", idx);
|
||||
} while (access(strFile, F_OK) != -1);
|
||||
Draw_SaveScreenshoot(strFile);
|
||||
Draw_SaveScreenshot(strFile);
|
||||
Print("Screenshot saved \"%s\"\n", strFile);
|
||||
}
|
||||
#endif // EMSCRIPTEN
|
||||
@@ -430,7 +427,7 @@ void GameLib_DrawLoop(void *data, float f) {
|
||||
Print("t_over.....:%6lldus\n", t_over / fproc_count);
|
||||
Print("t_postproc.:%6lldus\n", t_postproc / fproc_count);
|
||||
Print("t_draw.....:%6lldus\n", t_draw / fdraw_count);
|
||||
Print("n_ents.....:%6lld\n", _n_entities);
|
||||
Print("n_ents.....:%6lld\n", g_NumEntities);
|
||||
t_proc = 0;
|
||||
t_col = 0;
|
||||
t_over = 0;
|
||||
@@ -446,17 +443,17 @@ void GameLib_DrawLoop(void *data, float f) {
|
||||
//
|
||||
// Loops the game.
|
||||
void GameLib_Loop(void (*gameproc)(), void (*gamepostproc)(), void (*gamepredraw)(float f), void (*gamedraw)(float f)) {
|
||||
_gameproc = gameproc;
|
||||
_gamepostproc = gamepostproc;
|
||||
_gamepredraw = gamepredraw;
|
||||
_gamedraw = gamedraw;
|
||||
t_proc = 0;
|
||||
t_col = 0;
|
||||
t_over = 0;
|
||||
t_postproc = 0;
|
||||
t_draw = 0;
|
||||
fproc_count = 0;
|
||||
fdraw_count = 0;
|
||||
g_GameProcFunc = gameproc;
|
||||
g_GamePostProcFunc = gamepostproc;
|
||||
g_GamePreDrawFunc = gamepredraw;
|
||||
g_GameDrawFunc = gamedraw;
|
||||
t_proc = 0;
|
||||
t_col = 0;
|
||||
t_over = 0;
|
||||
t_postproc = 0;
|
||||
t_draw = 0;
|
||||
fproc_count = 0;
|
||||
fdraw_count = 0;
|
||||
Draw_Loop(GameLib_ProcLoop, GameLib_DrawLoop, NULL);
|
||||
}
|
||||
|
||||
@@ -470,30 +467,30 @@ void GameLib_Loop(void (*gameproc)(), void (*gamepostproc)(), void (*gamepredraw
|
||||
//
|
||||
//
|
||||
void GameLib_GetPos(int pos[2]) {
|
||||
pos[0] = _game_pos1[0];
|
||||
pos[1] = _game_pos1[1];
|
||||
pos[0] = g_GamePos1[0];
|
||||
pos[1] = g_GamePos1[1];
|
||||
}
|
||||
void GameLib_SetPos(int pos[2]) {
|
||||
_game_pos0[0] = pos[0];
|
||||
_game_pos0[1] = pos[1];
|
||||
_game_pos1[0] = pos[0];
|
||||
_game_pos1[1] = pos[1];
|
||||
void GameLib_SetPos(const int pos[2]) {
|
||||
g_GamePos0[0] = pos[0];
|
||||
g_GamePos0[1] = pos[1];
|
||||
g_GamePos1[0] = pos[0];
|
||||
g_GamePos1[1] = pos[1];
|
||||
}
|
||||
void GameLib_UpdatePos(int pos[2]) {
|
||||
_game_pos1[0] = pos[0];
|
||||
_game_pos1[1] = pos[1];
|
||||
void GameLib_UpdatePos(const int pos[2]) {
|
||||
g_GamePos1[0] = pos[0];
|
||||
g_GamePos1[1] = pos[1];
|
||||
}
|
||||
void GameLib_GetSize(int size[2]) {
|
||||
size[0] = _game_size[0];
|
||||
size[1] = _game_size[1];
|
||||
size[0] = g_GameSize[0];
|
||||
size[1] = g_GameSize[1];
|
||||
}
|
||||
void GameLib_GetPosInstant(int pos[2], float f) {
|
||||
pos[0] = _game_pos0[0] + f * ((_game_pos1[0] + _game_posOffset[0]) - _game_pos0[0]);
|
||||
pos[1] = _game_pos0[1] + f * ((_game_pos1[1] + _game_posOffset[1]) - _game_pos0[1]);
|
||||
pos[0] = g_GamePos0[0] + f * ((g_GamePos1[0] + g_GamePosOffset[0]) - g_GamePos0[0]);
|
||||
pos[1] = g_GamePos0[1] + f * ((g_GamePos1[1] + g_GamePosOffset[1]) - g_GamePos0[1]);
|
||||
}
|
||||
void GameLib_SetPosOffset(int posOffset[2]) {
|
||||
_game_posOffset[0] = posOffset[0];
|
||||
_game_posOffset[1] = posOffset[1];
|
||||
void GameLib_SetPosOffset(const int posOffset[2]) {
|
||||
g_GamePosOffset[0] = posOffset[0];
|
||||
g_GamePosOffset[1] = posOffset[1];
|
||||
}
|
||||
|
||||
/////////////////////////////
|
||||
@@ -506,11 +503,11 @@ void GameLib_MoveToPos(vec2 pos, float f) {
|
||||
GameLib_MoveToPosH(pos, f);
|
||||
GameLib_MoveToPosV(pos, f);
|
||||
}
|
||||
void GameLib_MoveToPosH(vec2 pos, float f) {
|
||||
_game_pos1[0] = _game_pos1[0] + (pos[0] - (_game_pos1[0] + (_game_size[0] / 2.0f))) * f;
|
||||
void GameLib_MoveToPosH(const vec2 pos, float f) {
|
||||
g_GamePos1[0] = g_GamePos1[0] + (pos[0] - (g_GamePos1[0] + (g_GameSize[0] / 2.0f))) * f;
|
||||
}
|
||||
void GameLib_MoveToPosV(vec2 pos, float f) {
|
||||
_game_pos1[1] = _game_pos1[1] + (pos[1] - (_game_pos1[1] + (_game_size[1] / 2.0f))) * f;
|
||||
void GameLib_MoveToPosV(const vec2 pos, float f) {
|
||||
g_GamePos1[1] = g_GamePos1[1] + (pos[1] - (g_GamePos1[1] + (g_GameSize[1] / 2.0f))) * f;
|
||||
}
|
||||
|
||||
/////////////////////////////
|
||||
@@ -520,12 +517,13 @@ void GameLib_MoveToPosV(vec2 pos, float f) {
|
||||
void GameLib_DelEnts() {
|
||||
int i;
|
||||
|
||||
for (i = 0; i < _n_entities; i++) {
|
||||
if (!_entity[i])
|
||||
for (i = 0; i < g_NumEntities; i++) {
|
||||
if (!g_Entity[i]) {
|
||||
continue;
|
||||
Entity_Destroy(_entity[i]);
|
||||
}
|
||||
Entity_Destroy(g_Entity[i]);
|
||||
}
|
||||
_n_entities = 0;
|
||||
g_NumEntities = 0;
|
||||
}
|
||||
|
||||
/////////////////////////////
|
||||
@@ -534,10 +532,11 @@ void GameLib_DelEnts() {
|
||||
// Iterates every entity.
|
||||
void GameLib_ForEachEnt(int (*func)(Entity ent)) {
|
||||
int i;
|
||||
for (i = 0; i < _n_entities; i++) {
|
||||
if (!_entity[i])
|
||||
for (i = 0; i < g_NumEntities; i++) {
|
||||
if (!g_Entity[i]) {
|
||||
continue;
|
||||
if (!func(_entity[i])) {
|
||||
}
|
||||
if (!func(g_Entity[i])) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -550,11 +549,12 @@ void GameLib_ForEachEnt(int (*func)(Entity ent)) {
|
||||
Entity GameLib_SearchEnt(int (*func)(Entity ent, void *d), void *d) {
|
||||
int i;
|
||||
Entity ent = NULL;
|
||||
for (i = 0; i < _n_entities; i++) {
|
||||
if (!_entity[i])
|
||||
for (i = 0; i < g_NumEntities; i++) {
|
||||
if (!g_Entity[i]) {
|
||||
continue;
|
||||
if (func(_entity[i], d)) {
|
||||
ent = _entity[i];
|
||||
}
|
||||
if (func(g_Entity[i], d)) {
|
||||
ent = g_Entity[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -575,11 +575,11 @@ int GameLib_EntityCustomCheckCollision(Entity ent, vec2 vel) {
|
||||
vec2_copy(ent->vel, vel);
|
||||
Entity_CalcBBox(ent);
|
||||
|
||||
for (j = 0; j < _n_entities; j++) {
|
||||
if (!(_entity[j]->flags & EntityFlag_Collision) || !Entity_BBoxIntersect(ent, _entity[j])) {
|
||||
for (j = 0; j < g_NumEntities; j++) {
|
||||
if (!(g_Entity[j]->flags & EntityFlag_Collision) || !Entity_BBoxIntersect(ent, g_Entity[j])) {
|
||||
continue;
|
||||
}
|
||||
Entity_CheckCollision(ent, _entity[j], &collInfo);
|
||||
Entity_CheckCollision(ent, g_Entity[j], &collInfo);
|
||||
if (collInfo != NULL) {
|
||||
collision = 1;
|
||||
break;
|
||||
@@ -598,16 +598,16 @@ int GameLib_EntityCustomCheckCollision(Entity ent, vec2 vel) {
|
||||
//
|
||||
//
|
||||
void GameLib_PlaySound(AudioSnd snd, int x, int y) {
|
||||
float vleft, vright, dx, dy;
|
||||
float vLeft, vRight, dx, dy;
|
||||
int r, cx, cy, off;
|
||||
|
||||
// Get the screen context
|
||||
cx = _game_pos1[0] + _game_size[0] / 2;
|
||||
cy = _game_pos1[1] + _game_size[1] / 2;
|
||||
if (_game_size[0] > _game_size[1]) {
|
||||
r = _game_size[0] / 2;
|
||||
cx = g_GamePos1[0] + g_GameSize[0] / 2;
|
||||
cy = g_GamePos1[1] + g_GameSize[1] / 2;
|
||||
if (g_GameSize[0] > g_GameSize[1]) {
|
||||
r = g_GameSize[0] / 2;
|
||||
} else {
|
||||
r = _game_size[1] / 2;
|
||||
r = g_GameSize[1] / 2;
|
||||
}
|
||||
r = r * 1.2f;
|
||||
off = r / 10.0f;
|
||||
@@ -615,22 +615,24 @@ void GameLib_PlaySound(AudioSnd snd, int x, int y) {
|
||||
// Calculate volumes
|
||||
dx = x - (cx + off);
|
||||
dy = y - (cy);
|
||||
vright = 1.0f - (sqrtf(dx * dx + dy * dy) / (float)r);
|
||||
vRight = 1.0f - (sqrtf(dx * dx + dy * dy) / (float)r);
|
||||
dx = x - (cx - off);
|
||||
dy = y - (cy);
|
||||
vleft = 1.0f - (sqrtf(dx * dx + dy * dy) / (float)r);
|
||||
vLeft = 1.0f - (sqrtf(dx * dx + dy * dy) / (float)r);
|
||||
|
||||
// Clamp to 0
|
||||
if (vleft < 0.0f)
|
||||
vleft = 0.0f;
|
||||
if (vright < 0.0f)
|
||||
vright = 0.0f;
|
||||
if (vleft <= 0.0f && vright <= 0.0f) {
|
||||
if (vLeft < 0.0f) {
|
||||
vLeft = 0.0f;
|
||||
}
|
||||
if (vRight < 0.0f) {
|
||||
vRight = 0.0f;
|
||||
}
|
||||
if (vLeft <= 0.0f && vRight <= 0.0f) {
|
||||
return;
|
||||
}
|
||||
|
||||
// PLAY!
|
||||
Audio_PlaySound(snd, vleft, vright, 0);
|
||||
Audio_PlaySound(snd, vLeft, vRight, 0);
|
||||
}
|
||||
|
||||
/////////////////////////////
|
||||
@@ -648,9 +650,9 @@ AudioChn GameLib_PlayLoopingSound(AudioSnd snd) {
|
||||
//
|
||||
void GameLib_EntitySetLight(Entity e, float r, float g, float b, float rad) {
|
||||
if (Entity_IsLight(e)) {
|
||||
Entity_MarkUpdateLight(e, _entity, _n_entities);
|
||||
Entity_MarkUpdateLight(e, g_Entity, g_NumEntities);
|
||||
Entity_SetLight(e, r, g, b, rad);
|
||||
Entity_MarkUpdateLight(e, _entity, _n_entities);
|
||||
Entity_MarkUpdateLight(e, g_Entity, g_NumEntities);
|
||||
} else {
|
||||
Entity_SetLight(e, r, g, b, rad);
|
||||
}
|
||||
@@ -663,11 +665,11 @@ void GameLib_EntitySetLight(Entity e, float r, float g, float b, float rad) {
|
||||
void GameLib_ConvertScreenPositionToGamePosition(vec2 screenPos, vec2 gamePos, float f) {
|
||||
int game_pos[2];
|
||||
|
||||
game_pos[0] = _game_pos0[0] + f * ((_game_pos1[0] + _game_posOffset[0]) - _game_pos0[0]);
|
||||
game_pos[1] = _game_pos0[1] + f * ((_game_pos1[1] + _game_posOffset[1]) - _game_pos0[1]);
|
||||
game_pos[0] = g_GamePos0[0] + f * ((g_GamePos1[0] + g_GamePosOffset[0]) - g_GamePos0[0]);
|
||||
game_pos[1] = g_GamePos0[1] + f * ((g_GamePos1[1] + g_GamePosOffset[1]) - g_GamePos0[1]);
|
||||
|
||||
gamePos[0] = (screenPos[0] * _game_size[0]) + game_pos[0];
|
||||
gamePos[1] = (screenPos[1] * _game_size[1]) + game_pos[1];
|
||||
gamePos[0] = (screenPos[0] * g_GameSize[0]) + game_pos[0];
|
||||
gamePos[1] = (screenPos[1] * g_GameSize[1]) + game_pos[1];
|
||||
}
|
||||
|
||||
/////////////////////////////
|
||||
@@ -675,23 +677,23 @@ void GameLib_ConvertScreenPositionToGamePosition(vec2 screenPos, vec2 gamePos, f
|
||||
//
|
||||
//
|
||||
void GameLib_AddParallaxBackground(DrawImg img, int imgSize[2], int imgOffset[2], float parallaxFactor[2]) {
|
||||
int idx = _nParallaxBackgrounds;
|
||||
int idx = g_NumParallaxBackgrounds;
|
||||
if ((idx + 1) >= MaxParallaxBackgrounds) {
|
||||
Print("GameLib: Can't add parallaxBackground, limit reached.");
|
||||
return;
|
||||
}
|
||||
_parallaxBackground[idx].img = img;
|
||||
_parallaxBackground[idx].imgSize[0] = imgSize[0];
|
||||
_parallaxBackground[idx].imgSize[1] = imgSize[1];
|
||||
_parallaxBackground[idx].imgOffset[0] = imgOffset[0];
|
||||
_parallaxBackground[idx].imgOffset[1] = imgOffset[1];
|
||||
_parallaxBackground[idx].parallaxFactor[0] = parallaxFactor[0];
|
||||
_parallaxBackground[idx].parallaxFactor[1] = parallaxFactor[1];
|
||||
_nParallaxBackgrounds++;
|
||||
g_ParallaxBackground[idx].img = img;
|
||||
g_ParallaxBackground[idx].imgSize[0] = imgSize[0];
|
||||
g_ParallaxBackground[idx].imgSize[1] = imgSize[1];
|
||||
g_ParallaxBackground[idx].imgOffset[0] = imgOffset[0];
|
||||
g_ParallaxBackground[idx].imgOffset[1] = imgOffset[1];
|
||||
g_ParallaxBackground[idx].parallaxFactor[0] = parallaxFactor[0];
|
||||
g_ParallaxBackground[idx].parallaxFactor[1] = parallaxFactor[1];
|
||||
g_NumParallaxBackgrounds++;
|
||||
}
|
||||
|
||||
/////////////////////////////
|
||||
// GameLib_CleanParallaxBackgrounds
|
||||
//
|
||||
//
|
||||
void GameLib_CleanParallaxBackgrounds() { _nParallaxBackgrounds = 0; }
|
||||
void GameLib_CleanParallaxBackgrounds() { g_NumParallaxBackgrounds = 0; }
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// Copyright (C) 2011 Valeriano Alfonso Rodriguez (Kableado)
|
||||
// Copyright (C) 2011-2023 Valeriano Alfonso Rodriguez (Kableado)
|
||||
|
||||
#ifndef _GAMELIB_H_
|
||||
#define _GAMELIB_H_
|
||||
#ifndef GameLib_H
|
||||
#define GameLib_H
|
||||
|
||||
#include "Anim.h"
|
||||
#include "Audio.h"
|
||||
@@ -15,7 +15,7 @@
|
||||
// GameLib_Init
|
||||
//
|
||||
// Initializes the game.
|
||||
int GameLib_Init(int w, int h, char *title, int pfps, int fps);
|
||||
int GameLib_Init(int w, int h, char *title, int pFps, int fps);
|
||||
|
||||
/////////////////////////////
|
||||
// GameLib_AddEntity
|
||||
@@ -51,11 +51,11 @@ void GameLib_Loop(void (*gameproc)(), void (*gamepostproc)(), void (*gamepredraw
|
||||
//
|
||||
//
|
||||
void GameLib_GetPos(int pos[2]);
|
||||
void GameLib_SetPos(int pos[2]);
|
||||
void GameLib_UpdatePos(int pos[2]);
|
||||
void GameLib_SetPos(const int pos[2]);
|
||||
void GameLib_UpdatePos(const int pos[2]);
|
||||
void GameLib_GetSize(int size[2]);
|
||||
void GameLib_GetPosInstant(int pos[2], float f);
|
||||
void GameLib_SetPosOffset(int posOffset[2]);
|
||||
void GameLib_SetPosOffset(const int posOffset[2]);
|
||||
|
||||
/////////////////////////////
|
||||
// GameLib_MoveToPos
|
||||
@@ -64,8 +64,8 @@ void GameLib_SetPosOffset(int posOffset[2]);
|
||||
//
|
||||
//
|
||||
void GameLib_MoveToPos(vec2 pos, float f);
|
||||
void GameLib_MoveToPosH(vec2 pos, float f);
|
||||
void GameLib_MoveToPosV(vec2 pos, float f);
|
||||
void GameLib_MoveToPosH(const vec2 pos, float f);
|
||||
void GameLib_MoveToPosV(const vec2 pos, float f);
|
||||
|
||||
/////////////////////////////
|
||||
// GameLib_ForEachEn
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// Copyright (C) 2011 Valeriano Alfonso Rodriguez (Kableado)
|
||||
// Copyright (C) 2011-2023 Valeriano Alfonso Rodriguez (Kableado)
|
||||
|
||||
#include <SDL.h>
|
||||
#include <math.h>
|
||||
@@ -158,13 +158,13 @@ int Input_GetDir(vec2 dir) {
|
||||
Uint8 buttons;
|
||||
int mx, my;
|
||||
float dlen;
|
||||
extern int _width, _height;
|
||||
extern int g_Width, g_Height;
|
||||
|
||||
// Get mouse state
|
||||
buttons = SDL_GetMouseState(&mx, &my);
|
||||
if (buttons) {
|
||||
// Use the mouse
|
||||
vec2_set(dir, mx - (_width / 2), my - (_height / 2.0f));
|
||||
vec2_set(dir, mx - (g_Width / 2), my - (g_Height / 2.0f));
|
||||
dlen = 1.0f / sqrtf(vec2_dot(dir, dir));
|
||||
vec2_scale(dir, dir, dlen);
|
||||
return (1);
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// Copyright (C) 2011 Valeriano Alfonso Rodriguez (Kableado)
|
||||
// Copyright (C) 2011-2023 Valeriano Alfonso Rodriguez (Kableado)
|
||||
|
||||
#ifndef _INPUT_H_
|
||||
#define _INPUT_H_
|
||||
#ifndef Input_H
|
||||
#define Input_H
|
||||
|
||||
#include "Util.h"
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
// Copyright (C) 2013 Valeriano Alfonso Rodriguez (Kableado)
|
||||
// Copyright (C) 2013-2023 Valeriano Alfonso Rodriguez (Kableado)
|
||||
|
||||
#include <math.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
@@ -19,10 +18,12 @@ QuadArray2D QuadArray2D_Create(int resVertex) {
|
||||
}
|
||||
|
||||
void QuadArray2D_Destroy(QuadArray2D *quadArray) {
|
||||
if (!quadArray)
|
||||
if (!quadArray) {
|
||||
return;
|
||||
if (!quadArray[0])
|
||||
}
|
||||
if (!quadArray[0]) {
|
||||
return;
|
||||
}
|
||||
|
||||
free(quadArray[0]->vertexData);
|
||||
free(quadArray[0]);
|
||||
@@ -48,7 +49,7 @@ void QuadArray2D_AddVertex(QuadArray2D quadArray, float v[]) {
|
||||
|
||||
void QuadArray2D_AddQuad(
|
||||
QuadArray2D quadArray, float x0, float y0, float u0, float v0, float x1, float y1, float u1, float v1,
|
||||
float color[]) {
|
||||
const float color[]) {
|
||||
float v[Vertex2D_Length];
|
||||
|
||||
// Set the color
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// Copyright (C) 2013 Valeriano Alfonso Rodriguez (Kableado)
|
||||
// Copyright (C) 2013-2023 Valeriano Alfonso Rodriguez (Kableado)
|
||||
|
||||
#ifndef _QUADARRAY2D_H_
|
||||
#define _QUADARRAY2D_H_
|
||||
#ifndef QuadArray2D_H
|
||||
#define QuadArray2D_H
|
||||
|
||||
// Vertex2D -> (x,y) (u,v) (r,g,b,a)
|
||||
#define Vertex2D_Length 8
|
||||
@@ -26,6 +26,6 @@ void QuadArray2D_AddVertex(QuadArray2D quadArray, float v[]);
|
||||
|
||||
void QuadArray2D_AddQuad(
|
||||
QuadArray2D quadArray, float x0, float y0, float u0, float v0, float x1, float y1, float u1, float v1,
|
||||
float color[]);
|
||||
const float color[]);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
// Copyright (C) 2011-2014 Valeriano Alfonso Rodriguez (Kableado)
|
||||
// Copyright (C) 2011-2023 Valeriano Alfonso Rodriguez (Kableado)
|
||||
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/time.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "TimeUtils.h"
|
||||
@@ -53,10 +50,10 @@ long long Time_GetTime() {
|
||||
usecs = (t.tv_sec * 1000000ll) + (t.tv_usec);
|
||||
return (usecs);
|
||||
}
|
||||
void Time_Pause(int pausa) {
|
||||
void Time_Pause(long long pause) {
|
||||
struct timeval tv;
|
||||
tv.tv_sec = (long long)pausa / 1000000;
|
||||
tv.tv_usec = (long long)pausa % 1000000;
|
||||
tv.tv_sec = (long long)pause / 1000000;
|
||||
tv.tv_usec = (long long)pause % 1000000;
|
||||
select(0, NULL, NULL, NULL, &tv);
|
||||
}
|
||||
#endif // if WIN32
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// Copyright (C) 2011 Valeriano Alfonso Rodriguez (Kableado)
|
||||
// Copyright (C) 2011-2023 Valeriano Alfonso Rodriguez (Kableado)
|
||||
|
||||
#ifndef _TIME_H_
|
||||
#define _TIME_H_
|
||||
#ifndef TimeUtils_H
|
||||
#define TimeUtils_H
|
||||
|
||||
/////////////////////////////
|
||||
// Time_GetTime
|
||||
@@ -13,6 +13,6 @@ long long Time_GetTime();
|
||||
// Time_Pause
|
||||
//
|
||||
// Pauses the execution for t usecs.
|
||||
void Time_Pause(int pausa);
|
||||
void Time_Pause(long long pause);
|
||||
|
||||
#endif
|
||||
|
||||
166
src/Util.c
166
src/Util.c
@@ -1,8 +1,7 @@
|
||||
// Copyright (C) 2011-2021 Valeriano Alfonso Rodriguez (Kableado)
|
||||
// Copyright (C) 2011-2023 Valeriano Alfonso Rodriguez (Kableado)
|
||||
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "Util.h"
|
||||
@@ -11,7 +10,7 @@
|
||||
// Misc
|
||||
//
|
||||
|
||||
float CosineInterpolation(float f) { return (1.0f - cos(f * Pi)) * 0.5f; }
|
||||
float CosineInterpolation(float f) { return (1.0f - cosf(f * Pi)) * 0.5f; }
|
||||
|
||||
int MinimumInt(int i0, int i1) {
|
||||
if (i1 < i0) {
|
||||
@@ -56,24 +55,24 @@ int Rect_PointInsideAny(TRect r[], int rCount, int x, int y) {
|
||||
// SolveQuadratic
|
||||
//
|
||||
// Solves a Quadratic equation using a, b and c coeficients.
|
||||
int SolveQuadratic(float a, float b, float c, float *Rmin, float *Rmax) {
|
||||
int SolveQuadratic(float a, float b, float c, float *RMin, float *RMax) {
|
||||
float root;
|
||||
float divisor;
|
||||
float b2;
|
||||
b2 = b * b;
|
||||
root = b2 - 4.0 * a * c;
|
||||
root = b2 - 4.0f * a * c;
|
||||
if (root < 0) {
|
||||
// Complex
|
||||
return (0);
|
||||
}
|
||||
divisor = (2.0 * a);
|
||||
if (fabs(divisor) == 0.0f) {
|
||||
divisor = (2.0f * a);
|
||||
if (fabsf(divisor) == 0.0f) {
|
||||
// +inf -inf
|
||||
return (0);
|
||||
}
|
||||
root = sqrtf(root);
|
||||
Rmin[0] = (float)((-b - root) / divisor);
|
||||
Rmax[0] = (float)((-b + root) / divisor);
|
||||
RMin[0] = (float)((-b - root) / divisor);
|
||||
RMax[0] = (float)((-b + root) / divisor);
|
||||
return (1);
|
||||
}
|
||||
|
||||
@@ -89,7 +88,7 @@ float vec2_norm(vec2 v) {
|
||||
}
|
||||
|
||||
void vec2_orthogonalize4(vec2 v) {
|
||||
if (fabs(v[0]) > fabs(v[1])) {
|
||||
if (fabsf(v[0]) > fabsf(v[1])) {
|
||||
if (v[0] >= 0) {
|
||||
v[0] = 1.0f;
|
||||
v[1] = 0.0f;
|
||||
@@ -109,9 +108,9 @@ void vec2_orthogonalize4(vec2 v) {
|
||||
}
|
||||
|
||||
void vec2_orthogonalize8(vec2 v) {
|
||||
float diff = fabs(fabs(v[0]) - fabs(v[1]));
|
||||
float diff = fabsf(fabsf(v[0]) - fabsf(v[1]));
|
||||
if (diff > 0.2f) {
|
||||
if (fabs(v[0]) > fabs(v[1])) {
|
||||
if (fabsf(v[0]) > fabsf(v[1])) {
|
||||
if (v[0] >= 0) {
|
||||
v[0] = 1.0f;
|
||||
v[1] = 0.0f;
|
||||
@@ -143,10 +142,10 @@ void vec2_orthogonalize8(vec2 v) {
|
||||
}
|
||||
|
||||
/////////////////////////////
|
||||
// Intersec_RayUnitCircle
|
||||
// Intersect_RayUnitCircle
|
||||
//
|
||||
// Intersection between a ray and a Unit Circle.
|
||||
int Intersec_RayUnitCircle(vec2 orig, vec2 vel, vec2 center, float *t) {
|
||||
int Intersect_RayUnitCircle(const vec2 orig, const vec2 vel, const vec2 center, float *t) {
|
||||
float a, b, c;
|
||||
float Rmin, Rmax;
|
||||
vec2 distv;
|
||||
@@ -155,8 +154,9 @@ int Intersec_RayUnitCircle(vec2 orig, vec2 vel, vec2 center, float *t) {
|
||||
|
||||
// Check if the collision is even posible
|
||||
qlvel = vec2_dot(vel, vel);
|
||||
if (fabs(qlvel) <= 0.0f)
|
||||
if (fabsf(qlvel) <= 0.0f) {
|
||||
return (0);
|
||||
}
|
||||
vec2_minus(distv, orig, center);
|
||||
qdistv = vec2_dot(distv, distv);
|
||||
|
||||
@@ -178,17 +178,17 @@ int Intersec_RayUnitCircle(vec2 orig, vec2 vel, vec2 center, float *t) {
|
||||
}
|
||||
|
||||
/////////////////////////////
|
||||
// Colision_CircleCircle
|
||||
// Collision_CircleCircle
|
||||
//
|
||||
// Colision point of a circle against another circle.
|
||||
int Colision_CircleCircle(vec2 cir1, float rad1, vec2 vel, vec2 cir2, float rad2, float *t, vec2 n) {
|
||||
// Collision point of a circle against another circle.
|
||||
int Collision_CircleCircle(const vec2 cir1, float ra, const vec2 vel, const vec2 cb, float rb, float *t, vec2 n) {
|
||||
vec2 vel_a, orig_a, cen_a, temp;
|
||||
float rads, invrads;
|
||||
float maxx, minx;
|
||||
float maxy, miny;
|
||||
|
||||
// Check if the collision is even posible
|
||||
rads = rad1 + rad2;
|
||||
rads = ra + rb;
|
||||
minx = cir1[0] - rads;
|
||||
maxx = cir1[0] + rads;
|
||||
if (vel[0] > 0) {
|
||||
@@ -196,7 +196,7 @@ int Colision_CircleCircle(vec2 cir1, float rad1, vec2 vel, vec2 cir2, float rad2
|
||||
} else {
|
||||
minx += vel[0];
|
||||
}
|
||||
if (cir2[0] < minx || cir2[0] > maxx)
|
||||
if (cb[0] < minx || cb[0] > maxx)
|
||||
return (0);
|
||||
miny = cir1[1] - rads;
|
||||
maxy = cir1[1] + rads;
|
||||
@@ -205,18 +205,18 @@ int Colision_CircleCircle(vec2 cir1, float rad1, vec2 vel, vec2 cir2, float rad2
|
||||
} else {
|
||||
miny += vel[1];
|
||||
}
|
||||
if (cir2[1] < miny || cir2[1] > maxy)
|
||||
if (cb[1] < miny || cb[1] > maxy)
|
||||
return (0);
|
||||
|
||||
// Convert to a unit circle vs ray
|
||||
invrads = 1.0f / rads;
|
||||
vec2_scale(vel_a, vel, invrads);
|
||||
vec2_scale(orig_a, cir1, invrads);
|
||||
vec2_scale(cen_a, cir2, invrads);
|
||||
if (Intersec_RayUnitCircle(orig_a, vel_a, cen_a, t)) {
|
||||
vec2_scale(cen_a, cb, invrads);
|
||||
if (Intersect_RayUnitCircle(orig_a, vel_a, cen_a, t)) {
|
||||
// Calculate n
|
||||
vec2_scaleadd(temp, cir1, vel, *t);
|
||||
vec2_minus(n, temp, cir2);
|
||||
vec2_minus(n, temp, cb);
|
||||
vec2_scale(n, n, invrads);
|
||||
return (1);
|
||||
}
|
||||
@@ -226,8 +226,8 @@ int Colision_CircleCircle(vec2 cir1, float rad1, vec2 vel, vec2 cir2, float rad2
|
||||
/////////////////////////////
|
||||
// Intersect_RayEdge
|
||||
//
|
||||
// Intersection between a ray and a edge.
|
||||
int Intersect_RayEdge(vec2 pos, vec2 vel, vec2 norm, vec2 edgePos, float len, float *t) {
|
||||
// Intersection between a ray and an edge.
|
||||
int Intersect_RayEdge(const vec2 pos, const vec2 vel, const vec2 norm, const vec2 edgePos, float len, float *t) {
|
||||
vec2 pos2, intersection, perp, edgePos2;
|
||||
float delta, d1, d2, hLen;
|
||||
|
||||
@@ -264,9 +264,9 @@ int Intersect_RayEdge(vec2 pos, vec2 vel, vec2 norm, vec2 edgePos, float len, fl
|
||||
}
|
||||
|
||||
/////////////////////////////
|
||||
// absmod
|
||||
// AbsMod
|
||||
//
|
||||
int absmod(int v, int d) {
|
||||
int AbsMod(int v, int d) {
|
||||
if (v < 0) {
|
||||
v += d * (((v / d) * (-1)) + 1);
|
||||
return (v);
|
||||
@@ -274,7 +274,7 @@ int absmod(int v, int d) {
|
||||
return (v % d);
|
||||
}
|
||||
}
|
||||
float fabsmod(float v, int d) {
|
||||
float AbsModFloat(float v, int d) {
|
||||
if (v < 0) {
|
||||
v += d * ((((int)(v / d)) * (-1)) + 1);
|
||||
return (v);
|
||||
@@ -299,85 +299,89 @@ int IsBigEndian() {
|
||||
// EndsWith
|
||||
//
|
||||
int EndsWith(char *str, char *suffix) {
|
||||
if (!str || !suffix)
|
||||
if (!str || !suffix) {
|
||||
return 0;
|
||||
int lenStr = strlen(str);
|
||||
int lenSuffix = strlen(suffix);
|
||||
if (lenSuffix > lenStr)
|
||||
}
|
||||
size_t lenStr = strlen(str);
|
||||
size_t lenSuffix = strlen(suffix);
|
||||
if (lenSuffix > lenStr) {
|
||||
return 0;
|
||||
}
|
||||
return strncmp(str + lenStr - lenSuffix, suffix, lenSuffix) == 0;
|
||||
}
|
||||
|
||||
/////////////////////////////
|
||||
// Rand
|
||||
//
|
||||
// (LGC++) + cambio de semilla
|
||||
// (LGC++) + Seed change
|
||||
|
||||
#define __seed_n 30
|
||||
#define __seed_a 30
|
||||
#define __seed_b 5
|
||||
#define __seed_c 10
|
||||
#define __seed_d 15
|
||||
// #define __LGC_a 1664525ul
|
||||
// #define __LGC_c 1013904223ul
|
||||
// #define __LGC_m 4294967296ul
|
||||
#define __LGC_a 16807ul
|
||||
#define __LGC_c 2
|
||||
#define __LGC_m 2147483647ul
|
||||
unsigned __seeds[30];
|
||||
int __seed_i = -1;
|
||||
#define g_LGC_Seed_N 30
|
||||
#define g_LGC_Seed_A 30
|
||||
#define g_LGC_Seed_B 5
|
||||
#define g_LGC_Seed_C 10
|
||||
#define g_LGC_Seed_D 15
|
||||
#define g_LGC_A 16807ul
|
||||
#define g_LGC_C 2
|
||||
#define g_LGC_M 2147483647ul
|
||||
unsigned g_LGC_Seeds[30];
|
||||
int g_LGC_Seed_I = -1;
|
||||
|
||||
unsigned __rand_count;
|
||||
unsigned __rand_orig_seed;
|
||||
unsigned g_RandCount;
|
||||
unsigned g_RandOrigSeed;
|
||||
|
||||
void Rand_Seed(unsigned seed) {
|
||||
int i;
|
||||
__seeds[0] = seed;
|
||||
g_LGC_Seeds[0] = seed;
|
||||
for (i = 1; i < 30; i++) {
|
||||
__seeds[i] = (__seeds[i - 1] * __LGC_a + __LGC_c) % __LGC_m;
|
||||
//__seeds[i]=(__seeds[i-1]*__LGC_a+__LGC_c);
|
||||
g_LGC_Seeds[i] = (g_LGC_Seeds[i - 1] * g_LGC_A + g_LGC_C) % g_LGC_M;
|
||||
}
|
||||
__seed_i = 29;
|
||||
g_LGC_Seed_I = 29;
|
||||
|
||||
// Cambio de semilla
|
||||
__rand_count = 0;
|
||||
__rand_orig_seed = seed;
|
||||
g_RandCount = 0;
|
||||
g_RandOrigSeed = seed;
|
||||
}
|
||||
|
||||
unsigned Rand_Get() {
|
||||
unsigned val;
|
||||
int a, b, c, d;
|
||||
|
||||
if (__seed_i == -1) {
|
||||
if (g_LGC_Seed_I == -1) {
|
||||
Rand_Seed(1);
|
||||
}
|
||||
|
||||
a = __seed_i - __seed_a;
|
||||
if (a < 0)
|
||||
a += __seed_n;
|
||||
b = __seed_i - __seed_b;
|
||||
if (b < 0)
|
||||
b += __seed_n;
|
||||
c = __seed_i - __seed_c;
|
||||
if (c < 0)
|
||||
c += __seed_n;
|
||||
d = __seed_i - __seed_d;
|
||||
if (d < 0)
|
||||
d += __seed_n;
|
||||
val = __seeds[a] ^ __seeds[b] ^ __seeds[c] ^ __seeds[d];
|
||||
a = g_LGC_Seed_I - g_LGC_Seed_A;
|
||||
if (a < 0) {
|
||||
a += g_LGC_Seed_N;
|
||||
}
|
||||
b = g_LGC_Seed_I - g_LGC_Seed_B;
|
||||
if (b < 0) {
|
||||
b += g_LGC_Seed_N;
|
||||
}
|
||||
c = g_LGC_Seed_I - g_LGC_Seed_C;
|
||||
if (c < 0) {
|
||||
c += g_LGC_Seed_N;
|
||||
}
|
||||
d = g_LGC_Seed_I - g_LGC_Seed_D;
|
||||
if (d < 0) {
|
||||
d += g_LGC_Seed_N;
|
||||
}
|
||||
val = g_LGC_Seeds[a] ^ g_LGC_Seeds[b] ^ g_LGC_Seeds[c] ^ g_LGC_Seeds[d];
|
||||
|
||||
a = __seed_i - 1;
|
||||
if (a < 0)
|
||||
a = __seed_n - 1;
|
||||
__seeds[__seed_i] = (__seeds[a] * __LGC_a + __LGC_c) % __LGC_m;
|
||||
//__seeds[__seed_i]=(__seeds[a]*__LGC_a+__LGC_c);
|
||||
__seed_i++;
|
||||
if (__seed_i == __seed_n)
|
||||
__seed_i = 0;
|
||||
a = g_LGC_Seed_I - 1;
|
||||
if (a < 0) {
|
||||
a = g_LGC_Seed_N - 1;
|
||||
}
|
||||
g_LGC_Seeds[g_LGC_Seed_I] = (g_LGC_Seeds[a] * g_LGC_A + g_LGC_C) % g_LGC_M;
|
||||
g_LGC_Seed_I++;
|
||||
if (g_LGC_Seed_I == g_LGC_Seed_N) {
|
||||
g_LGC_Seed_I = 0;
|
||||
}
|
||||
|
||||
// Cambio de semilla
|
||||
__rand_count++;
|
||||
if (__rand_count > (1 << 15)) {
|
||||
Rand_Seed(__rand_orig_seed + 1);
|
||||
g_RandCount++;
|
||||
if (g_RandCount > (1 << 15)) {
|
||||
Rand_Seed(g_RandOrigSeed + 1);
|
||||
}
|
||||
|
||||
return (val);
|
||||
@@ -393,7 +397,7 @@ unsigned Rand_GetBetween(int min, int max) {
|
||||
/////////////////////////////
|
||||
// Print
|
||||
//
|
||||
// Prints the formated text
|
||||
// Prints the formatted text
|
||||
int Print(char *fmt, ...) {
|
||||
va_list ap;
|
||||
int n;
|
||||
|
||||
46
src/Util.h
46
src/Util.h
@@ -1,7 +1,7 @@
|
||||
// Copyright (C) 2011-2021 Valeriano Alfonso Rodriguez (Kableado)
|
||||
// Copyright (C) 2011-2023 Valeriano Alfonso Rodriguez (Kableado)
|
||||
|
||||
#ifndef _UTIL_H_
|
||||
#define _UTIL_H_
|
||||
#ifndef Util_H
|
||||
#define Util_H
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <stdlib.h>
|
||||
@@ -37,8 +37,8 @@ int Rect_PointInsideAny(TRect r[], int rCount, int x, int y);
|
||||
/////////////////////////////
|
||||
// SolveQuadratic
|
||||
//
|
||||
// Solves a Quadratic equation using a, b and c coeficients.
|
||||
int SolveQuadratic(float a, float b, float c, float *Rmin, float *Rmax);
|
||||
// Solves a Quadratic equation using a, b and c coefficients.
|
||||
int SolveQuadratic(float a, float b, float c, float *RMin, float *RMax);
|
||||
|
||||
////////////////////////////////////////////////
|
||||
// vec2 //
|
||||
@@ -47,57 +47,57 @@ int SolveQuadratic(float a, float b, float c, float *Rmin, float *Rmax);
|
||||
typedef float vec2[2];
|
||||
#define vec2_set(v, x, y) \
|
||||
(v)[0] = (x); \
|
||||
(v)[1] = (y);
|
||||
(v)[1] = (y)
|
||||
#define vec2_copy(v1, v2) \
|
||||
(v1)[0] = (v2)[0]; \
|
||||
(v1)[1] = (v2)[1];
|
||||
(v1)[1] = (v2)[1]
|
||||
#define vec2_plus(v, v1, v2) \
|
||||
(v)[0] = (v1)[0] + (v2)[0]; \
|
||||
(v)[1] = (v1)[1] + (v2)[1];
|
||||
(v)[1] = (v1)[1] + (v2)[1]
|
||||
#define vec2_minus(v, v1, v2) \
|
||||
(v)[0] = (v1)[0] - (v2)[0]; \
|
||||
(v)[1] = (v1)[1] - (v2)[1];
|
||||
(v)[1] = (v1)[1] - (v2)[1]
|
||||
#define vec2_scale(v, v1, s) \
|
||||
(v)[0] = (v1)[0] * (s); \
|
||||
(v)[1] = (v1)[1] * (s);
|
||||
(v)[1] = (v1)[1] * (s)
|
||||
#define vec2_dot(v1, v2) ((v1)[0] * (v2)[0] + (v1)[1] * (v2)[1])
|
||||
#define vec2_len(v) sqrtf((v)[0] * (v)[0] + (v)[1] * (v)[1])
|
||||
#define vec2_perp(v, n) \
|
||||
(v)[0] = -(n)[1]; \
|
||||
(v)[1] = (n)[0];
|
||||
(v)[1] = (n)[0]
|
||||
#define vec2_scaleadd(v, v1, v2, s) \
|
||||
(v)[0] = (v2)[0] * (s) + (v1)[0]; \
|
||||
(v)[1] = (v2)[1] * (s) + (v1)[1];
|
||||
(v)[1] = (v2)[1] * (s) + (v1)[1]
|
||||
float vec2_norm(vec2 v);
|
||||
#define vec2_interpol(v, v1, v2, f) \
|
||||
(v)[0] = (v1)[0] - f * ((v1)[0] - (v2)[0]); \
|
||||
(v)[1] = (v1)[1] - f * ((v1)[1] - (v2)[1]);
|
||||
(v)[1] = (v1)[1] - f * ((v1)[1] - (v2)[1])
|
||||
void vec2_orthogonalize4(vec2 v);
|
||||
void vec2_orthogonalize8(vec2 v);
|
||||
|
||||
/////////////////////////////
|
||||
// Intersec_RayUnitCircle
|
||||
// Intersect_RayUnitCircle
|
||||
//
|
||||
// Intersection between a ray and a Unit Circle.
|
||||
int Intersec_RayUnitCircle(vec2 orig, vec2 vel, vec2 center, float *t);
|
||||
int Intersect_RayUnitCircle(const vec2 orig, const vec2 vel, const vec2 center, float *t);
|
||||
|
||||
/////////////////////////////
|
||||
// Intersect_CircleCircle
|
||||
// Collision_CircleCircle
|
||||
//
|
||||
// Colision point of a circle against another circle.
|
||||
int Colision_CircleCircle(vec2 cir1, float ra, vec2 vel, vec2 cb, float rb, float *t, vec2 n);
|
||||
// Collision point of a circle against another circle.
|
||||
int Collision_CircleCircle(const vec2 cir1, float ra, const vec2 vel, const vec2 cb, float rb, float *t, vec2 n);
|
||||
|
||||
/////////////////////////////
|
||||
// Intersect_RayEdge
|
||||
//
|
||||
// Intersection between a ray and a edge.
|
||||
int Intersect_RayEdge(vec2 pos, vec2 vel, vec2 norm, vec2 edgePos, float len, float *t);
|
||||
// Intersection between a ray and an edge.
|
||||
int Intersect_RayEdge(const vec2 pos, const vec2 vel, const vec2 norm, const vec2 edgePos, float len, float *t);
|
||||
|
||||
/////////////////////////////
|
||||
// absmod
|
||||
// AbsMod
|
||||
//
|
||||
int absmod(int v, int d);
|
||||
float fabsmod(float v, int d);
|
||||
int AbsMod(int v, int d);
|
||||
float AbsModFloat(float v, int d);
|
||||
|
||||
/////////////////////////////
|
||||
// IsBigEndian
|
||||
|
||||
Reference in New Issue
Block a user