Format code

This commit is contained in:
2016-09-10 10:47:19 +02:00
committed by Valeriano A.R
parent 4d29388734
commit 6818acdd4e
24 changed files with 2171 additions and 2497 deletions

65
.clang-format Normal file
View File

@@ -0,0 +1,65 @@
---
Language: Cpp
# BasedOnStyle: LLVM
AccessModifierOffset: -2
AlignAfterOpenBracket: true
AlignEscapedNewlinesLeft: false
AlignOperands: true
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AllowShortFunctionsOnASingleLine: All
AlwaysBreakAfterDefinitionReturnType: false
AlwaysBreakTemplateDeclarations: false
AlwaysBreakBeforeMultilineStrings: false
BreakBeforeBinaryOperators: None
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
BinPackParameters: true
BinPackArguments: true
ColumnLimit: 80
ConstructorInitializerAllOnOneLineOrOnePerLine: false
ConstructorInitializerIndentWidth: 4
DerivePointerAlignment: false
ExperimentalAutoDetectBinPacking: false
IndentCaseLabels: false
IndentWrappedFunctionNames: false
IndentFunctionDeclarationAfterType: false
MaxEmptyLinesToKeep: 1
KeepEmptyLinesAtTheStartOfBlocks: true
NamespaceIndentation: None
ObjCBlockIndentWidth: 2
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakString: 1000
PenaltyBreakFirstLessLess: 120
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60
PointerAlignment: Right
SpacesBeforeTrailingComments: 1
Cpp11BracedListStyle: true
Standard: Cpp11
IndentWidth: 4
TabWidth: 4
UseTab: true
BreakBeforeBraces: Attach
SpacesInParentheses: false
SpacesInSquareBrackets: false
SpacesInAngles: false
SpaceInEmptyParentheses: false
SpacesInCStyleCastParentheses: false
SpaceAfterCStyleCast: false
SpacesInContainerLiterals: true
SpaceBeforeAssignmentOperators: true
ContinuationIndentWidth: 4
CommentPragmas: '^ IWYU pragma:'
ForEachMacros: [ foreach, Q_FOREACH, BOOST_FOREACH ]
SpaceBeforeParens: ControlStatements
DisableFormat: false
...

View File

@@ -17,20 +17,13 @@ Entity ent_Player;
Entity ent_Platform; Entity ent_Platform;
Entity ent_Block; Entity ent_Block;
int EntityApplyGravity(Entity e) { int EntityApplyGravity(Entity e) {
float grav = 10.0f; float grav = 10.0f;
float vTerminal = 50.0f; float vTerminal = 50.0f;
vec2 vGrav; vec2 vGrav;
// Only apply gravity to some entity types // Only apply gravity to some entity types
if(!( if (!(e->type == Ent_Player || 0)) {
e->type==Ent_Player ||
0
))
{
return (1); return (1);
} }
@@ -38,13 +31,9 @@ int EntityApplyGravity(Entity e){
vec2_set(vGrav, 0.0f, grav); vec2_set(vGrav, 0.0f, grav);
Entity_AddVelLimit(e, vGrav, vTerminal); Entity_AddVelLimit(e, vGrav, vTerminal);
return (1); return (1);
} }
void player_proc(Entity e, int ft) { void player_proc(Entity e, int ft) {
float acel = 8.0f; float acel = 8.0f;
float maxVel = 30.0f; float maxVel = 30.0f;
@@ -52,8 +41,7 @@ void player_proc(Entity e,int ft){
float shootVel = 50.0f; float shootVel = 50.0f;
if (Input_GetKey(InputKey_Jump) == InputKey_Pressed || if (Input_GetKey(InputKey_Jump) == InputKey_Pressed ||
Input_GetKey(InputKey_Up)==InputKey_Pressed) Input_GetKey(InputKey_Up) == InputKey_Pressed) {
{
vec2 jump; vec2 jump;
// Apply jump // Apply jump
@@ -77,28 +65,20 @@ void player_proc(Entity e,int ft){
vec2 right; vec2 right;
// Apply right movement // Apply right movement
vec2_set(right,acel,0.0f) vec2_set(right, acel, 0.0f) Entity_AddVelLimit(e, right, maxVel);
Entity_AddVelLimit(e,right,maxVel);
e->A = 1; e->A = 1;
} }
if (Input_GetKey(InputKey_Action1) == InputKey_Pressed || if (Input_GetKey(InputKey_Action1) == InputKey_Pressed ||
Input_GetKey(InputKey_Action2)==InputKey_Pressed) Input_GetKey(InputKey_Action2) == InputKey_Pressed) {
{
} }
// Scroll View // Scroll View
GameLib_MoveToPos(e->pos, 0.6f); GameLib_MoveToPos(e->pos, 0.6f);
} }
void GameEnts_Init() { void GameEnts_Init() {
///////////////////////////// /////////////////////////////
// Load and initialize media. // Load and initialize media.
// //
@@ -107,7 +87,6 @@ void GameEnts_Init(){
img_platform = Draw_LoadImage("data/platform.png"); img_platform = Draw_LoadImage("data/platform.png");
img_block = Draw_LoadImage("data/block.png"); img_block = Draw_LoadImage("data/block.png");
///////////////////////// /////////////////////////
// Initialize entity types. // Initialize entity types.
// //
@@ -150,6 +129,4 @@ void GameEnts_Init(){
ent_Block->height = 64; ent_Block->height = 64;
ent_Block->fric_static = 0.0f; ent_Block->fric_static = 0.0f;
ent_Block->fric_dynamic = 0.2f; ent_Block->fric_dynamic = 0.2f;
} }

View File

@@ -3,8 +3,6 @@
#ifndef _GAMEENTS_H_ #ifndef _GAMEENTS_H_
#define _GAMEENTS_H_ #define _GAMEENTS_H_
enum { enum {
Ent_Player, Ent_Player,
Ent_Platform, Ent_Platform,
@@ -22,4 +20,3 @@ int EntityApplyGravity(Entity e);
void GameEnts_Init(); void GameEnts_Init();
#endif #endif

View File

@@ -10,7 +10,6 @@
#include "GameEnts.h" #include "GameEnts.h"
#include "GameMap.h" #include "GameMap.h"
int ReadLine(FILE *f, char *line, int max) { int ReadLine(FILE *f, char *line, int max) {
int c; int c;
int i = 0; int i = 0;
@@ -34,7 +33,6 @@ int ReadLine(FILE *f,char *line,int max){
return (i); return (i);
} }
Entity GameMapAux_CreateEnt(Entity ent, int i, int j, int res) { Entity GameMapAux_CreateEnt(Entity ent, int i, int j, int res) {
Entity e; Entity e;
vec2 pos; vec2 pos;
@@ -46,7 +44,6 @@ Entity GameMapAux_CreateEnt(Entity ent,int i,int j,int res){
return (e); return (e);
} }
#define MaxLineLen 1024 #define MaxLineLen 1024
int GameMap_LoadLevel(char *filename, int res) { int GameMap_LoadLevel(char *filename, int res) {
@@ -56,7 +53,6 @@ int GameMap_LoadLevel(char *filename,int res){
int width, height; int width, height;
char *map; char *map;
// Open the file // Open the file
file = fopen(filename, "rb"); file = fopen(filename, "rb");
if (!file) { if (!file) {
@@ -77,7 +73,6 @@ int GameMap_LoadLevel(char *filename,int res){
} while (len > -1); } while (len > -1);
fseek(file, 0, SEEK_SET); fseek(file, 0, SEEK_SET);
// Build the map // Build the map
map = malloc(sizeof(char) * width * height); map = malloc(sizeof(char) * width * height);
memset(map, 0, width * height); memset(map, 0, width * height);
@@ -91,18 +86,14 @@ int GameMap_LoadLevel(char *filename,int res){
j++; j++;
} while (len > -1); } while (len > -1);
// Close the file // Close the file
fclose(file); fclose(file);
// Parse the map // Parse the map
for (j = 0; j < height; j++) { for (j = 0; j < height; j++) {
for (i = 0; i < width; i++) { for (i = 0; i < width; i++) {
Entity ent; Entity ent;
if (MAP(i, j) == 'P') { if (MAP(i, j) == 'P') {
// Player // Player
GameMapAux_CreateEnt(ent_Player, i, j, res); GameMapAux_CreateEnt(ent_Player, i, j, res);
@@ -115,11 +106,9 @@ int GameMap_LoadLevel(char *filename,int res){
// Platform // Platform
ent = GameMapAux_CreateEnt(ent_Platform, i, j, res); ent = GameMapAux_CreateEnt(ent_Platform, i, j, res);
} }
} }
} }
// Cleanup // Cleanup
free(map); free(map);
#undef MAP #undef MAP

View File

@@ -4,5 +4,4 @@
int GameMap_LoadLevel(char *filename, int res); int GameMap_LoadLevel(char *filename, int res);
#endif #endif

View File

@@ -12,7 +12,6 @@ extern int gamelib_debug;
#include "GameEnts.h" #include "GameEnts.h"
#include "GameMap.h" #include "GameMap.h"
DrawFnt font; DrawFnt font;
DrawImg imgBackground; DrawImg imgBackground;
@@ -23,19 +22,13 @@ void MainGame_Text(int x, int y, char *text){
Draw_DrawText(font, text, x, y); Draw_DrawText(font, text, x, y);
} }
void ProcGame(){ void ProcGame() {}
}
void PostProcGame() { void PostProcGame() {
// Apply gravity to every entity // Apply gravity to every entity
GameLib_ForEachEnt(EntityApplyGravity); GameLib_ForEachEnt(EntityApplyGravity);
} }
void PreDrawGame(float f){ void PreDrawGame(float f) {}
void DrawGame(float f) { MainGame_Text(8, 8, "Hello world!"); }
}
void DrawGame(float f){
MainGame_Text(8,8,"Hello world!");
}
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
int i, j; int i, j;
@@ -70,7 +63,8 @@ int main(int argc,char *argv[]){
// Run the world. // Run the world.
// //
GameLib_CleanParallaxBackgrounds(); GameLib_CleanParallaxBackgrounds();
GameLib_AddParallaxBackground(imgBackground, (int[2]){512, 512}, (int[2]){0, 0}, (float[2]){0.5f, 0.0f}); GameLib_AddParallaxBackground(imgBackground, (int[2]){512, 512},
(int[2]){0, 0}, (float[2]){0.5f, 0.0f});
GameLib_Loop(ProcGame, PostProcGame, PreDrawGame, DrawGame); GameLib_Loop(ProcGame, PostProcGame, PreDrawGame, DrawGame);
return (0); return (0);

View File

@@ -6,7 +6,6 @@
#include "Draw.h" #include "Draw.h"
#include "Anim.h" #include "Anim.h"
//////////////////////////////////////////////// ////////////////////////////////////////////////
// Animation // // Animation //
/////////////// ///////////////
@@ -20,7 +19,6 @@ typedef struct {
int time; int time;
} Animation; } Animation;
///////////////////////////// /////////////////////////////
// Anim_LoadAnim // Anim_LoadAnim
// //
@@ -52,7 +50,6 @@ Anim Anim_LoadAnim(char *fichero,int width,int frames,float fps){
return ((Anim)anim); return ((Anim)anim);
} }
///////////////////////////// /////////////////////////////
// Anim_GetTime // Anim_GetTime
// //
@@ -63,7 +60,6 @@ int Anim_GetTime(Anim a){
return (anim->time); return (anim->time);
} }
///////////////////////////// /////////////////////////////
// Anim_GetSize // Anim_GetSize
// //
@@ -76,7 +72,6 @@ void Anim_GetSize(Anim a,int *w,int *h){
Draw_GetSize(anim->img, &waux, h); Draw_GetSize(anim->img, &waux, h);
} }
///////////////////////////// /////////////////////////////
// Anim_SetOffset // Anim_SetOffset
// Anim_GetOffset // Anim_GetOffset
@@ -93,7 +88,6 @@ void Anim_GetOffset(Anim a,int *x,int *y){
Draw_GetOffset(anim->img, x, y); Draw_GetOffset(anim->img, x, y);
} }
///////////////////////////// /////////////////////////////
// Anim_SetFlip // Anim_SetFlip
// Anim_GetFlip // Anim_GetFlip
@@ -110,7 +104,6 @@ int Anim_GetFlip(Anim a){
return Draw_GetFlip(anim->img); return Draw_GetFlip(anim->img);
} }
///////////////////////////// /////////////////////////////
// Anim_Draw // Anim_Draw
// //
@@ -123,7 +116,6 @@ void Anim_Draw(Anim a,int time_ms,int x,int y){
Draw_DrawImgPartHoriz(anim->img, x, y, anim->w, frame); Draw_DrawImgPartHoriz(anim->img, x, y, anim->w, frame);
} }
///////////////////////////// /////////////////////////////
// AnimPlay_Copy // AnimPlay_Copy
// //
@@ -141,7 +133,6 @@ void AnimPlay_Copy(AnimPlay *ad,AnimPlay *ao){
ad->time_ms = ao->time_ms; ad->time_ms = ao->time_ms;
} }
///////////////////////////// /////////////////////////////
// AnimPlay_SetImg // AnimPlay_SetImg
// AnimPlay_SetAnim // AnimPlay_SetAnim
@@ -168,7 +159,8 @@ void AnimPlay_SetAnim(AnimPlay *ap,Anim ani){
ap->imgPart = NULL; ap->imgPart = NULL;
} }
void AnimPlay_SetImgPart(AnimPlay *ap,DrawImg img,int w,int h,int i,int j){ void AnimPlay_SetImgPart(AnimPlay *ap, DrawImg img, int w, int h, int i,
int j) {
ap->anim = NULL; ap->anim = NULL;
ap->time_ms = 0; ap->time_ms = 0;
@@ -181,7 +173,6 @@ void AnimPlay_SetImgPart(AnimPlay *ap,DrawImg img,int w,int h,int i,int j){
ap->j = j; ap->j = j;
} }
///////////////////////////// /////////////////////////////
// AnimPlay_Draw // AnimPlay_Draw
// //
@@ -201,7 +192,6 @@ void AnimPlay_Draw(AnimPlay *ani,int x,int y){
} }
} }
///////////////////////////// /////////////////////////////
// AnimPlay_GetOffset // AnimPlay_GetOffset
// AnimPlay_GetSize // AnimPlay_GetSize
@@ -225,8 +215,7 @@ void AnimPlay_GetSize(AnimPlay *ani,int *w,int *h){
if (ani->anim) { if (ani->anim) {
Anim_GetSize(ani->anim, w, h); Anim_GetSize(ani->anim, w, h);
return; return;
}else } else if (ani->img) {
if(ani->img){
Draw_GetSize(ani->img, w, h); Draw_GetSize(ani->img, w, h);
return; return;
} }
@@ -236,16 +225,11 @@ void AnimPlay_GetSize(AnimPlay *ani,int *w,int *h){
} }
} }
///////////////////////////// /////////////////////////////
// AnimPlay_SetPause // AnimPlay_SetPause
// //
// //
void AnimPlay_SetPause(AnimPlay *ani,int p){ void AnimPlay_SetPause(AnimPlay *ani, int p) { ani->pause = p; }
ani->pause=p;
}
///////////////////////////// /////////////////////////////
// AnimPlay_IncTime // AnimPlay_IncTime
@@ -258,4 +242,3 @@ void AnimPlay_IncTime(AnimPlay *ani,int t){
} }
} }
} }

View File

@@ -5,35 +5,30 @@
#include "Draw.h" #include "Draw.h"
//////////////////////////////////////////////// ////////////////////////////////////////////////
// Anim // // Anim //
////////// //////////
// //
typedef void *Anim; typedef void *Anim;
///////////////////////////// /////////////////////////////
// Anim_LoadAnim // Anim_LoadAnim
// //
// //
Anim Anim_LoadAnim(char *fichero, int width, int frames, float fps); Anim Anim_LoadAnim(char *fichero, int width, int frames, float fps);
///////////////////////////// /////////////////////////////
// Anim_GetTime // Anim_GetTime
// //
// //
int Anim_GetTime(Anim anim); int Anim_GetTime(Anim anim);
///////////////////////////// /////////////////////////////
// Anim_GetSize // Anim_GetSize
// //
// Gets the animation size. // Gets the animation size.
void Anim_GetSize(Anim anim, int *w, int *h); void Anim_GetSize(Anim anim, int *w, int *h);
///////////////////////////// /////////////////////////////
// Anim_SetOffset // Anim_SetOffset
// Anim_GetOffset // Anim_GetOffset
@@ -42,7 +37,6 @@ void Anim_GetSize(Anim anim,int *w,int *h);
void Anim_SetOffset(Anim anim, int x, int y); void Anim_SetOffset(Anim anim, int x, int y);
void Anim_GetOffset(Anim anim, int *x, int *y); void Anim_GetOffset(Anim anim, int *x, int *y);
///////////////////////////// /////////////////////////////
// Anim_SetFlip // Anim_SetFlip
// Draw_GetFlip // Draw_GetFlip
@@ -51,14 +45,12 @@ void Anim_GetOffset(Anim anim,int *x,int *y);
void Anim_SetFlip(Anim anim, int flip); void Anim_SetFlip(Anim anim, int flip);
int Anim_GetFlip(Anim anim); int Anim_GetFlip(Anim anim);
///////////////////////////// /////////////////////////////
// Anim_Draw // Anim_Draw
// //
// //
void Anim_Draw(Anim anim, int time_ms, int x, int y); void Anim_Draw(Anim anim, int time_ms, int x, int y);
//////////////////////////////////////////////// ////////////////////////////////////////////////
// AnimPlay // // AnimPlay //
////////////// //////////////
@@ -75,14 +67,12 @@ typedef struct {
} AnimPlay; } AnimPlay;
///////////////////////////// /////////////////////////////
// AnimPlay_Copy // AnimPlay_Copy
// //
// //
void AnimPlay_Copy(AnimPlay *ad, AnimPlay *ao); void AnimPlay_Copy(AnimPlay *ad, AnimPlay *ao);
///////////////////////////// /////////////////////////////
// AnimPlay_SetImg // AnimPlay_SetImg
// AnimPlay_SetAnim // AnimPlay_SetAnim
@@ -93,14 +83,12 @@ void AnimPlay_SetImg(AnimPlay *ap,DrawImg img);
void AnimPlay_SetAnim(AnimPlay *ap, Anim ani); void AnimPlay_SetAnim(AnimPlay *ap, Anim ani);
void AnimPlay_SetImgPart(AnimPlay *ap, DrawImg img, int w, int h, int i, int j); void AnimPlay_SetImgPart(AnimPlay *ap, DrawImg img, int w, int h, int i, int j);
///////////////////////////// /////////////////////////////
// AnimPlay_Draw // AnimPlay_Draw
// //
// //
void AnimPlay_Draw(AnimPlay *ani, int x, int y); void AnimPlay_Draw(AnimPlay *ani, int x, int y);
///////////////////////////// /////////////////////////////
// AnimPlay_GetOffset // AnimPlay_GetOffset
// AnimPlay_GetSize // AnimPlay_GetSize
@@ -109,7 +97,6 @@ void AnimPlay_Draw(AnimPlay *ani,int x,int y);
void AnimPlay_GetOffset(AnimPlay *ani, int *x, int *y); void AnimPlay_GetOffset(AnimPlay *ani, int *x, int *y);
void AnimPlay_GetSize(AnimPlay *ani, int *w, int *h); void AnimPlay_GetSize(AnimPlay *ani, int *w, int *h);
///////////////////////////// /////////////////////////////
// AnimPlay_SetPause // AnimPlay_SetPause
// //
@@ -122,5 +109,4 @@ void AnimPlay_SetPause(AnimPlay *ani,int p);
// //
void AnimPlay_IncTime(AnimPlay *ani, int t); void AnimPlay_IncTime(AnimPlay *ani, int t);
#endif #endif

View File

@@ -14,7 +14,6 @@
static void Audio_MixerCallback(void *ud, Uint8 *stream, int l); static void Audio_MixerCallback(void *ud, Uint8 *stream, int l);
//////////////////////////////////////////////// ////////////////////////////////////////////////
// AudioWave // // AudioWave //
/////////////// ///////////////
@@ -32,7 +31,6 @@ struct TAudioWave {
}; };
AudioWave _waves = NULL; AudioWave _waves = NULL;
//////////////////////////////////////////////// ////////////////////////////////////////////////
// AudioChan // // AudioChan //
/////////////// ///////////////
@@ -83,10 +81,7 @@ int Audio_Init(){
} }
// Asert results // Asert results
if( as2.format != AUDIO_S16SYS || if (as2.format != AUDIO_S16SYS || as2.freq != 44100 || as2.channels != 2) {
as2.freq != 44100 ||
as2.channels != 2 )
{
Print("Audio_Init: Failure opening audio. (44.1Khz/16b/2c).\n"); Print("Audio_Init: Failure opening audio. (44.1Khz/16b/2c).\n");
SDL_CloseAudio(); SDL_CloseAudio();
return (0); return (0);
@@ -98,7 +93,6 @@ int Audio_Init(){
return (1); return (1);
} }
///////////////////////////// /////////////////////////////
// Audio_MixerCallback // Audio_MixerCallback
// //
@@ -180,7 +174,8 @@ static void Audio_MixerCallback(void *ud,Uint8 *stream,int l){
// Next sample // Next sample
ptr_out += 2; ptr_out += 2;
if(ptr_wave>=(((signed short *)wave->buffer)+(wave->len-1))){ if (ptr_wave >=
(((signed short *)wave->buffer) + (wave->len - 1))) {
ptr_wave = ((signed short *)wave->buffer); ptr_wave = ((signed short *)wave->buffer);
} else { } else {
ptr_wave++; ptr_wave++;
@@ -201,15 +196,11 @@ static void Audio_MixerCallback(void *ud,Uint8 *stream,int l){
} }
} }
///////////////////////////// /////////////////////////////
// Audio_Frame // Audio_Frame
// //
// Notify a frame update to the audio subsystem. // Notify a frame update to the audio subsystem.
void Audio_Frame(){ void Audio_Frame() {}
}
///////////////////////////// /////////////////////////////
// Audio_LoadSound // Audio_LoadSound
@@ -279,7 +270,9 @@ AudioSnd Audio_LoadSound(char *filename){
// Skip no "data" blocks // Skip no "data" blocks
do { do {
int lenRead = fread(id, 1, sizeof(char) * 4, f); int lenRead = fread(id, 1, sizeof(char) * 4, f);
if(lenRead<4){ break; } if (lenRead < 4) {
break;
}
if (strcmp(id, "data")) { if (strcmp(id, "data")) {
fread(&dataSize, 1, sizeof(int), f); fread(&dataSize, 1, sizeof(int), f);
fseek(f, dataSize, SEEK_CUR); fseek(f, dataSize, SEEK_CUR);
@@ -316,14 +309,12 @@ AudioSnd Audio_LoadSound(char *filename){
return (wave); return (wave);
} }
///////////////////////////// /////////////////////////////
// Audio_PlaySound // Audio_PlaySound
// //
// Loads a sound, giving a reference. // Loads a sound, giving a reference.
AudioChn Audio_PlaySound(AudioSnd snd, AudioChn Audio_PlaySound(AudioSnd snd, float leftvol, float rightvol,
float leftvol, float rightvol,int loop) int loop) {
{
AudioChan chan; AudioChan chan;
AudioWave wave; AudioWave wave;
if (!snd) { if (!snd) {
@@ -357,7 +348,6 @@ AudioChn Audio_PlaySound(AudioSnd snd,
return chan; return chan;
} }
///////////////////////////// /////////////////////////////
// Audio_StopChan // Audio_StopChan
// //
@@ -365,8 +355,9 @@ AudioChn Audio_PlaySound(AudioSnd snd,
void Audio_StopChan(AudioChn c) { void Audio_StopChan(AudioChn c) {
AudioChan chan; AudioChan chan;
chan = c; chan = c;
if(c==NULL){return;} if (c == NULL) {
return;
}
chan->loop = 0; chan->loop = 0;
chan->wave = NULL; chan->wave = NULL;
} }

View File

@@ -3,49 +3,41 @@
#ifndef _AUDIO_H_ #ifndef _AUDIO_H_
#define _AUDIO_H_ #define _AUDIO_H_
///////////////////////////// /////////////////////////////
// Audio_Init // Audio_Init
// //
// Initializes the game audio. // Initializes the game audio.
int Audio_Init(); int Audio_Init();
///////////////////////////// /////////////////////////////
// Audio_Frame // Audio_Frame
// //
// Notify a frame update to the audio subsystem. // Notify a frame update to the audio subsystem.
void Audio_Frame(); void Audio_Frame();
//////////////////////////////////////////////// ////////////////////////////////////////////////
// AudioSnd // // AudioSnd //
////////////// //////////////
// Reference to a sound. // Reference to a sound.
typedef void *AudioSnd; typedef void *AudioSnd;
//////////////////////////////////////////////// ////////////////////////////////////////////////
// AudioChn // // AudioChn //
////////////// //////////////
// Reference to a playing sound. // Reference to a playing sound.
typedef void *AudioChn; typedef void *AudioChn;
///////////////////////////// /////////////////////////////
// Audio_LoadSound // Audio_LoadSound
// //
// Loads a sound, giving a reference. // Loads a sound, giving a reference.
AudioSnd Audio_LoadSound(char *filename); AudioSnd Audio_LoadSound(char *filename);
///////////////////////////// /////////////////////////////
// Audio_PlaySound // Audio_PlaySound
// //
// Loads a sound, giving a reference. // Loads a sound, giving a reference.
AudioChn Audio_PlaySound(AudioSnd snd, AudioChn Audio_PlaySound(AudioSnd snd, float leftvol, float rightvol, int loop);
float leftvol, float rightvol,int loop);
///////////////////////////// /////////////////////////////
// Audio_StopChan // Audio_StopChan
@@ -53,5 +45,4 @@ AudioChn Audio_PlaySound(AudioSnd snd,
// Stops an audio chanel // Stops an audio chanel
void Audio_StopChan(AudioChn chan); void Audio_StopChan(AudioChn chan);
#endif #endif

View File

@@ -51,7 +51,6 @@
#include "Input.h" #include "Input.h"
#include "Draw.h" #include "Draw.h"
//////////////////////////////////////////////// ////////////////////////////////////////////////
// DrawImage // // DrawImage //
/////////////// ///////////////
@@ -65,8 +64,6 @@ struct TDrawImage {
GLuint tex; GLuint tex;
}; };
// Globals // Globals
SDL_Surface *_screen = NULL; SDL_Surface *_screen = NULL;
int _width; int _width;
@@ -103,14 +100,13 @@ GLuint Draw_CompileShader(GLenum type, const char *source){
return shader; return shader;
} }
GLuint Draw_BuildProgram(const char *vertexShaderSource,
GLuint Draw_BuildProgram( const char *fragmentShaderSource) {
const char *vertexShaderSource,
const char *fragmentShaderSource)
{
// Compile shaders // Compile shaders
GLuint vertexShader = Draw_CompileShader(GL_VERTEX_SHADER, vertexShaderSource); GLuint vertexShader =
GLuint fragmentShader = Draw_CompileShader(GL_FRAGMENT_SHADER, fragmentShaderSource); Draw_CompileShader(GL_VERTEX_SHADER, vertexShaderSource);
GLuint fragmentShader =
Draw_CompileShader(GL_FRAGMENT_SHADER, fragmentShaderSource);
if (vertexShader == 0 || fragmentShader == 0) { if (vertexShader == 0 || fragmentShader == 0) {
return 0; return 0;
} }
@@ -124,8 +120,7 @@ GLuint Draw_BuildProgram(
// check if the program linked successfully // check if the program linked successfully
GLint linked; GLint linked;
glGetProgramiv(programObject, GL_LINK_STATUS, &linked); glGetProgramiv(programObject, GL_LINK_STATUS, &linked);
if (!linked) if (!linked) {
{
glDeleteProgram(programObject); glDeleteProgram(programObject);
return 0; return 0;
} }
@@ -199,7 +194,6 @@ int Draw_Init(int width,int height,char *title,int pfps,int fps){
SDL_GL_SwapBuffers(); SDL_GL_SwapBuffers();
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
glEnableClientState(GL_COLOR_ARRAY); glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY); glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_VERTEX_ARRAY);
@@ -208,8 +202,7 @@ int Draw_Init(int width,int height,char *title,int pfps,int fps){
#if USE_OpenGLES #if USE_OpenGLES
const char vertexShaderSource[] = const char vertexShaderSource[] = "attribute vec4 aPosition; \n"
"attribute vec4 aPosition; \n"
"attribute vec2 aTexCoord; \n" "attribute vec2 aTexCoord; \n"
"attribute vec4 aColor; \n" "attribute vec4 aColor; \n"
"varying vec2 vTexCoord; \n" "varying vec2 vTexCoord; \n"
@@ -231,9 +224,7 @@ int Draw_Init(int width,int height,char *title,int pfps,int fps){
" gl_FragColor = texture2D(sTexture, vTexCoord)*vColor; \n" " gl_FragColor = texture2D(sTexture, vTexCoord)*vColor; \n"
"} \n"; "} \n";
programObject=Draw_BuildProgram( programObject = Draw_BuildProgram(vertexShaderSource, fragmentShaderSource);
vertexShaderSource,
fragmentShaderSource);
glUseProgram(programObject); glUseProgram(programObject);
vertPosLoc = glGetAttribLocation(programObject, "aPosition"); vertPosLoc = glGetAttribLocation(programObject, "aPosition");
@@ -241,23 +232,28 @@ int Draw_Init(int width,int height,char *title,int pfps,int fps){
vertColorLoc = glGetAttribLocation(programObject, "aColor"); vertColorLoc = glGetAttribLocation(programObject, "aColor");
textureLoc = glGetUniformLocation(programObject, "sTexture"); textureLoc = glGetUniformLocation(programObject, "sTexture");
projectionMatrixLoc = glGetUniformLocation(programObject, "sProjectionMatrix"); projectionMatrixLoc =
glGetUniformLocation(programObject, "sProjectionMatrix");
glUniform1i(textureLoc, 0); glUniform1i(textureLoc, 0);
glGenBuffers(1, &vertexObject); glGenBuffers(1, &vertexObject);
glBindBuffer(GL_ARRAY_BUFFER, vertexObject); glBindBuffer(GL_ARRAY_BUFFER, vertexObject);
glBufferData(GL_ARRAY_BUFFER, Vertex2D_Length*sizeof(float)*Max_Vertices, glBufferData(GL_ARRAY_BUFFER,
NULL, GL_DYNAMIC_DRAW); Vertex2D_Length * sizeof(float) * Max_Vertices, NULL,
GL_DYNAMIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, vertexObject); glBindBuffer(GL_ARRAY_BUFFER, vertexObject);
glVertexAttribPointer(vertPosLoc, 2, GL_FLOAT, GL_FALSE, glVertexAttribPointer(vertPosLoc, 2, GL_FLOAT, GL_FALSE,
Vertex2D_Length*sizeof(float), (void*)(0*sizeof(float))); Vertex2D_Length * sizeof(float),
(void *)(0 * sizeof(float)));
glVertexAttribPointer(vertTexLoc, 2, GL_FLOAT, GL_FALSE, glVertexAttribPointer(vertTexLoc, 2, GL_FLOAT, GL_FALSE,
Vertex2D_Length*sizeof(float), (void*)(2*sizeof(float))); Vertex2D_Length * sizeof(float),
(void *)(2 * sizeof(float)));
glVertexAttribPointer(vertColorLoc, 4, GL_FLOAT, GL_FALSE, glVertexAttribPointer(vertColorLoc, 4, GL_FLOAT, GL_FALSE,
Vertex2D_Length*sizeof(float), (void*)(4*sizeof(float))); Vertex2D_Length * sizeof(float),
(void *)(4 * sizeof(float)));
glEnableVertexAttribArray(vertPosLoc); glEnableVertexAttribArray(vertPosLoc);
glEnableVertexAttribArray(vertTexLoc); glEnableVertexAttribArray(vertTexLoc);
@@ -270,11 +266,8 @@ int Draw_Init(int width,int height,char *title,int pfps,int fps){
// Set the proyection (2D) // Set the proyection (2D)
glViewport(0, 0, _width, _height); glViewport(0, 0, _width, _height);
float projectionMatrix[16] = { float projectionMatrix[16] = {1, 0, 0, 0, 0, 1, 0, 0,
1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1 };
projectionMatrix[0] = (2.0f / _width); projectionMatrix[0] = (2.0f / _width);
projectionMatrix[5] = -(2.0f / _height); projectionMatrix[5] = -(2.0f / _height);
projectionMatrix[10] = -0.001f; projectionMatrix[10] = -0.001f;
@@ -294,7 +287,6 @@ int Draw_Init(int width,int height,char *title,int pfps,int fps){
return (1); return (1);
} }
///////////////////////////// /////////////////////////////
// Draw_ShowInfo // Draw_ShowInfo
// //
@@ -312,27 +304,23 @@ void Draw_ShowInfo(){
Print("*********************************\n"); Print("*********************************\n");
} }
///////////////////////////// /////////////////////////////
// Draw_SetMatrix // Draw_SetMatrix
// //
// Sets the render matrix // Sets the render matrix
void Draw_SetMatrix(float matrix[16]) { void Draw_SetMatrix(float matrix[16]) {
#if USE_OpenGL #if USE_OpenGL
float tempMatrix[16] = { float tempMatrix[16] = {matrix[0], matrix[4], matrix[8], matrix[12],
matrix[0], matrix[4], matrix[ 8], matrix[12],
matrix[1], matrix[5], matrix[9], matrix[13], matrix[1], matrix[5], matrix[9], matrix[13],
matrix[2], matrix[6], matrix[10], matrix[14], matrix[2], matrix[6], matrix[10], matrix[14],
matrix[3], matrix[7], matrix[11], matrix[15]}; matrix[3], matrix[7], matrix[11], matrix[15]};
glLoadMatrixf(tempMatrix); glLoadMatrixf(tempMatrix);
#endif #endif
#if USE_OpenGLES #if USE_OpenGLES
glUniformMatrix4fv(projectionMatrixLoc, glUniformMatrix4fv(projectionMatrixLoc, 1, GL_FALSE, matrix);
1, GL_FALSE, matrix);
#endif #endif
} }
///////////////////////////// /////////////////////////////
// Draw_UploadGLTexture // Draw_UploadGLTexture
// //
@@ -354,14 +342,12 @@ GLuint Draw_UploadGLTexture(int w, int h, unsigned char *pixels){
glPixelStorei(GL_UNPACK_ROW_LENGTH, w); glPixelStorei(GL_UNPACK_ROW_LENGTH, w);
#endif #endif
glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE,
w, h, 0, pixels);
GL_RGBA, GL_UNSIGNED_BYTE, pixels);
return (tex); return (tex);
} }
///////////////////////////// /////////////////////////////
// Draw_Flush // Draw_Flush
// //
@@ -371,7 +357,8 @@ void Draw_Flush(){
return; return;
} }
if (_currentImg->tex == -1) { if (_currentImg->tex == -1) {
_currentImg->tex=Draw_UploadGLTexture(_currentImg->w, _currentImg->h, _currentImg->data); _currentImg->tex = Draw_UploadGLTexture(_currentImg->w, _currentImg->h,
_currentImg->data);
} }
#if USE_OpenGL #if USE_OpenGL
@@ -400,16 +387,11 @@ void Draw_Flush(){
QuadArray2D_Clean(_quadArray); QuadArray2D_Clean(_quadArray);
} }
///////////////////////////// /////////////////////////////
// Draw_Clean // Draw_Clean
// //
// Cleans the game window. // Cleans the game window.
void Draw_Clean( void Draw_Clean(unsigned char r, unsigned char g, unsigned char b) {
unsigned char r,
unsigned char g,
unsigned char b)
{
#ifndef EMSCRIPTEN #ifndef EMSCRIPTEN
glClearColor(r / 255.0f, g / 255.0f, b / 255.0f, 1.0f); glClearColor(r / 255.0f, g / 255.0f, b / 255.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
@@ -449,7 +431,6 @@ void Draw_Clean(
#endif #endif
} }
///////////////////////////// /////////////////////////////
// Draw_LoopIteration // Draw_LoopIteration
// //
@@ -475,49 +456,36 @@ int Draw_LoopIteration(){
} }
} }
if (event.type == SDL_MOUSEMOTION) { if (event.type == SDL_MOUSEMOTION) {
Input_SetPointerPosition( Input_SetPointerPosition(event.motion.x / (float)_width,
event.motion.x/(float)_width,
event.motion.y / (float)_height); event.motion.y / (float)_height);
} }
if (event.type == SDL_MOUSEBUTTONDOWN) { if (event.type == SDL_MOUSEBUTTONDOWN) {
Input_SetPointerPosition( Input_SetPointerPosition(event.button.x / (float)_width,
event.button.x/(float)_width,
event.button.y / (float)_height); event.button.y / (float)_height);
Input_SetPointerDown(1); Input_SetPointerDown(1);
} }
if (event.type == SDL_FINGERMOTION) { if (event.type == SDL_FINGERMOTION) {
Input_SetPointerPosition( Input_SetPointerPosition(event.tfinger.x, event.tfinger.y);
event.tfinger.x,
event.tfinger.y);
} }
if (event.type == SDL_FINGERDOWN) { if (event.type == SDL_FINGERDOWN) {
Input_SetPointerPosition( Input_SetPointerPosition(event.tfinger.x, event.tfinger.y);
event.tfinger.x,
event.tfinger.y);
Input_SetPointerDown(1); Input_SetPointerDown(1);
} }
if (event.type == SDL_TOUCHBUTTONDOWN) { if (event.type == SDL_TOUCHBUTTONDOWN) {
Input_SetPointerPosition( Input_SetPointerPosition(event.tfinger.x, event.tfinger.y);
event.tfinger.x,
event.tfinger.y);
Input_SetPointerDown(1); Input_SetPointerDown(1);
} }
if (event.type == SDL_MOUSEBUTTONUP) { if (event.type == SDL_MOUSEBUTTONUP) {
Input_SetPointerPosition( Input_SetPointerPosition(event.button.x / (float)_width,
event.button.x/(float)_width,
event.button.y / (float)_height); event.button.y / (float)_height);
Input_SetPointerDown(0); Input_SetPointerDown(0);
} }
if (event.type == SDL_FINGERUP) { if (event.type == SDL_FINGERUP) {
Input_SetPointerPosition( Input_SetPointerPosition(event.tfinger.x, event.tfinger.y);
event.tfinger.x,
event.tfinger.y);
Input_SetPointerDown(0); Input_SetPointerDown(0);
} }
if (event.type == SDL_TOUCHBUTTONUP) { if (event.type == SDL_TOUCHBUTTONUP) {
Input_SetPointerPosition( Input_SetPointerPosition(event.tfinger.x, event.tfinger.y);
event.tfinger.x,
event.tfinger.y);
Input_SetPointerDown(0); Input_SetPointerDown(0);
} }
} }
@@ -538,19 +506,16 @@ int Draw_LoopIteration(){
} }
} }
if (event.type == SDL_MOUSEMOTION) { if (event.type == SDL_MOUSEMOTION) {
Input_SetPointerPosition( Input_SetPointerPosition(event.motion.x / (float)_width,
event.motion.x/(float)_width,
event.motion.y / (float)_height); event.motion.y / (float)_height);
} }
if (event.type == SDL_MOUSEBUTTONDOWN) { if (event.type == SDL_MOUSEBUTTONDOWN) {
Input_SetPointerPosition( Input_SetPointerPosition(event.button.x / (float)_width,
event.button.x/(float)_width,
event.button.y / (float)_height); event.button.y / (float)_height);
Input_SetPointerDown(1); Input_SetPointerDown(1);
} }
if (event.type == SDL_MOUSEBUTTONUP) { if (event.type == SDL_MOUSEBUTTONUP) {
Input_SetPointerPosition( Input_SetPointerPosition(event.button.x / (float)_width,
event.button.x/(float)_width,
event.button.y / (float)_height); event.button.y / (float)_height);
Input_SetPointerDown(0); Input_SetPointerDown(0);
} }
@@ -616,16 +581,15 @@ void Draw_LoopIterationAux(){
// Draw_Loop // Draw_Loop
// //
// Loops updating the game window. // Loops updating the game window.
void Draw_Loop( void Draw_Loop(void (*proc)(void *data), void (*draw)(void *data, float f),
void (*proc)(void *data), void *data) {
void (*draw)(void *data,float f),
void *data)
{
_proc_func = proc; _proc_func = proc;
_draw_func = draw; _draw_func = draw;
_data = data; _data = data;
if(_draw_looping){return;} if (_draw_looping) {
return;
}
_draw_looping = 1; _draw_looping = 1;
#ifndef EMSCRIPTEN #ifndef EMSCRIPTEN
long long procTime1, procTime2, drawTime1, drawTime2; long long procTime1, procTime2, drawTime1, drawTime2;
@@ -655,7 +619,6 @@ void Draw_Loop(
#endif #endif
} }
///////////////////////////// /////////////////////////////
// Draw_BreakLoop // Draw_BreakLoop
// //
@@ -666,15 +629,11 @@ void Draw_BreakLoop(){
#endif #endif
} }
///////////////////////////// /////////////////////////////
// Draw_OverrideExit // Draw_OverrideExit
// //
// Overrides the default exit mechanism // Overrides the default exit mechanism
void Draw_OverrideExit(int override){ void Draw_OverrideExit(int override) { _draw_exitoverrided = override; }
_draw_exitoverrided=override;
}
///////////////////////////// /////////////////////////////
// Draw_CreateImage // Draw_CreateImage
@@ -695,7 +654,6 @@ DrawImg Draw_CreateImage(int w,int h){
return ((DrawImg)image); return ((DrawImg)image);
} }
///////////////////////////// /////////////////////////////
// Draw_LoadImage // Draw_LoadImage
// //
@@ -706,13 +664,12 @@ DrawImg Draw_LoadImage(char *filename){
// Try loading PNG images // Try loading PNG images
if (EndsWith(filename, ".png") || EndsWith(filename, ".PNG")) { if (EndsWith(filename, ".png") || EndsWith(filename, ".PNG")) {
image = malloc(sizeof(TDrawImage)); image = malloc(sizeof(TDrawImage));
unsigned error = lodepng_decode32_file( unsigned error =
&image->data, lodepng_decode32_file(&image->data, (unsigned *)&image->w,
(unsigned*)&image->w, (unsigned *)&image->h, filename);
(unsigned*)&image->h,
filename);
if (error) { if (error) {
Print("Draw_LoadImage: PNG decoder error %u: %s on file %s\n", error, lodepng_error_text(error),filename); Print("Draw_LoadImage: PNG decoder error %u: %s on file %s\n",
error, lodepng_error_text(error), filename);
return (NULL); return (NULL);
} }
image->x = -(int)(image->w / 2); image->x = -(int)(image->w / 2);
@@ -726,7 +683,6 @@ DrawImg Draw_LoadImage(char *filename){
return (NULL); return (NULL);
} }
///////////////////////////// /////////////////////////////
// Draw_GetSize // Draw_GetSize
// //
@@ -739,7 +695,6 @@ void Draw_GetSize(DrawImg img,int *w,int *h){
*h = image->h; *h = image->h;
} }
///////////////////////////// /////////////////////////////
// Draw_SetOffset // Draw_SetOffset
// Draw_GetOffset // Draw_GetOffset
@@ -760,7 +715,6 @@ void Draw_GetOffset(DrawImg img,int *x,int *y){
*y = image->y; *y = image->y;
} }
///////////////////////////// /////////////////////////////
// Draw_SetFlip // Draw_SetFlip
// Draw_GetFlip // Draw_GetFlip
@@ -775,7 +729,6 @@ int Draw_GetFlip(DrawImg img){
return image->flip; return image->flip;
} }
///////////////////////////// /////////////////////////////
// Draw_DrawImg // Draw_DrawImg
// //
@@ -794,10 +747,14 @@ void Draw_DrawImg(DrawImg img,int x,int y){
// Apply flipping // Apply flipping
if (image->flip & 1) { if (image->flip & 1) {
float t=u1; u1=u2; u2=t; float t = u1;
u1 = u2;
u2 = t;
} }
if (image->flip & 2) { if (image->flip & 2) {
float t=v1; v1=v2; v2=t; float t = v1;
v1 = v2;
v2 = t;
} }
// Draw a quad // Draw a quad
@@ -805,13 +762,9 @@ void Draw_DrawImg(DrawImg img,int x,int y){
Draw_Flush(); Draw_Flush();
_currentImg = image; _currentImg = image;
} }
QuadArray2D_AddQuad(_quadArray, QuadArray2D_AddQuad(_quadArray, x1, y1, u1, v1, x2, y2, u2, v2, _color);
x1,y1,u1,v1,
x2,y2,u2,v2,
_color);
} }
///////////////////////////// /////////////////////////////
// Draw_DrawImgResized // Draw_DrawImgResized
// //
@@ -830,10 +783,14 @@ void Draw_DrawImgResized(DrawImg img,int x,int y,float w,float h){
// Apply flipping // Apply flipping
if (image->flip & 1) { if (image->flip & 1) {
float t=u1; u1=u2; u2=t; float t = u1;
u1 = u2;
u2 = t;
} }
if (image->flip & 2) { if (image->flip & 2) {
float t=v1; v1=v2; v2=t; float t = v1;
v1 = v2;
v2 = t;
} }
// Draw a quad // Draw a quad
@@ -841,13 +798,9 @@ void Draw_DrawImgResized(DrawImg img,int x,int y,float w,float h){
Draw_Flush(); Draw_Flush();
_currentImg = image; _currentImg = image;
} }
QuadArray2D_AddQuad(_quadArray, QuadArray2D_AddQuad(_quadArray, x1, y1, u1, v1, x2, y2, u2, v2, _color);
x1,y1,u1,v1,
x2,y2,u2,v2,
_color);
} }
///////////////////////////// /////////////////////////////
// Draw_DrawImgPart // Draw_DrawImgPart
// //
@@ -872,10 +825,14 @@ void Draw_DrawImgPart(DrawImg img,int x,int y,int w,int h,int i,int j){
// Apply flipping // Apply flipping
if (image->flip & 1) { if (image->flip & 1) {
float t=u1; u1=u2; u2=t; float t = u1;
u1 = u2;
u2 = t;
} }
if (image->flip & 2) { if (image->flip & 2) {
float t=v1; v1=v2; v2=t; float t = v1;
v1 = v2;
v2 = t;
} }
// Draw a quad // Draw a quad
@@ -883,13 +840,9 @@ void Draw_DrawImgPart(DrawImg img,int x,int y,int w,int h,int i,int j){
Draw_Flush(); Draw_Flush();
_currentImg = image; _currentImg = image;
} }
QuadArray2D_AddQuad(_quadArray, QuadArray2D_AddQuad(_quadArray, x1, y1, u1, v1, x2, y2, u2, v2, _color);
x1,y1,u1,v1,
x2,y2,u2,v2,
_color);
} }
///////////////////////////// /////////////////////////////
// Draw_DrawImgPartHoriz // Draw_DrawImgPartHoriz
// //
@@ -911,10 +864,14 @@ void Draw_DrawImgPartHoriz(DrawImg img,int x,int y,int w,int i){
// Apply flipping // Apply flipping
if (image->flip & 1) { if (image->flip & 1) {
float t=u1; u1=u2; u2=t; float t = u1;
u1 = u2;
u2 = t;
} }
if (image->flip & 2) { if (image->flip & 2) {
float t=v1; v1=v2; v2=t; float t = v1;
v1 = v2;
v2 = t;
} }
// Draw a quad // Draw a quad
@@ -922,18 +879,16 @@ void Draw_DrawImgPartHoriz(DrawImg img,int x,int y,int w,int i){
Draw_Flush(); Draw_Flush();
_currentImg = image; _currentImg = image;
} }
QuadArray2D_AddQuad(_quadArray, QuadArray2D_AddQuad(_quadArray, x1, y1, u1, v1, x2, y2, u2, v2, _color);
x1,y1,u1,v1,
x2,y2,u2,v2,
_color);
} }
///////////////////////////// /////////////////////////////
// Draw_ImgParallax // Draw_ImgParallax
// //
// //
void Draw_ImgParallax(DrawImg img, int imgSize[2], int imgOffset[2], float parallaxFactor[2], int gamePos[2], int gameSize[2]){ void Draw_ImgParallax(DrawImg img, int imgSize[2], int imgOffset[2],
float parallaxFactor[2], int gamePos[2],
int gameSize[2]) {
int paralaxPos[2]; int paralaxPos[2];
int mult[2]; int mult[2];
int x, y; int x, y;
@@ -942,9 +897,13 @@ void Draw_ImgParallax(DrawImg img, int imgSize[2], int imgOffset[2], float paral
paralaxPos[1] = (gamePos[1] * parallaxFactor[1]) + imgOffset[1]; paralaxPos[1] = (gamePos[1] * parallaxFactor[1]) + imgOffset[1];
mult[0] = floor(paralaxPos[0] / imgSize[0]); mult[0] = floor(paralaxPos[0] / imgSize[0]);
if(paralaxPos[0] < 0 ){ mult[0]--; } if (paralaxPos[0] < 0) {
mult[0]--;
}
mult[1] = floor(paralaxPos[1] / imgSize[1]); mult[1] = floor(paralaxPos[1] / imgSize[1]);
if(paralaxPos[1] < 0 ){ mult[1]--; } if (paralaxPos[1] < 0) {
mult[1]--;
}
y = (mult[1] * imgSize[1]) - paralaxPos[1]; y = (mult[1] * imgSize[1]) - paralaxPos[1];
while (y < gameSize[1]) { while (y < gameSize[1]) {
@@ -957,7 +916,6 @@ void Draw_ImgParallax(DrawImg img, int imgSize[2], int imgOffset[2], float paral
} }
} }
///////////////////////////// /////////////////////////////
// Draw_SetColor // Draw_SetColor
// //
@@ -969,7 +927,6 @@ void Draw_SetColor(float r,float g,float b,float a){
_color[3] = a; _color[3] = a;
} }
//////////////////////////////////////////////// ////////////////////////////////////////////////
// DrawFnt // // DrawFnt //
///////////// /////////////
@@ -980,18 +937,13 @@ typedef struct {
int min, max; int min, max;
} DrawFont; } DrawFont;
///////////////////////////// /////////////////////////////
// Draw_DefaultImage // Draw_DefaultImage
// //
// Creates a image with the default font. // Creates a image with the default font.
#include "FontData.h" #include "FontData.h"
DrawImage Draw_DefaultFontImage( DrawImage Draw_DefaultFontImage(unsigned char r, unsigned char g,
unsigned char r, unsigned char b, unsigned char a) {
unsigned char g,
unsigned char b,
unsigned char a)
{
DrawImage img; DrawImage img;
int x, y, c; int x, y, c;
@@ -1018,17 +970,12 @@ DrawImage Draw_DefaultFontImage(
return (img); return (img);
} }
///////////////////////////// /////////////////////////////
// Draw_DefaultFont // Draw_DefaultFont
// //
// Creates the default font. // Creates the default font.
DrawFnt Draw_DefaultFont( DrawFnt Draw_DefaultFont(unsigned char r, unsigned char g, unsigned char b,
unsigned char r, unsigned char a) {
unsigned char g,
unsigned char b,
unsigned char a)
{
DrawFont *font; DrawFont *font;
// Create the default font // Create the default font
@@ -1060,7 +1007,6 @@ DrawFnt Draw_LoadFont(char *fichero,int min,int max){
return ((DrawFnt)font); return ((DrawFnt)font);
} }
///////////////////////////// /////////////////////////////
// Draw_DrawText // Draw_DrawText
// //
@@ -1080,7 +1026,6 @@ void Draw_DrawText(DrawFnt f,char *text,int x,int y){
} }
} }
///////////////////////////// /////////////////////////////
// Draw_SaveScreenshoot // Draw_SaveScreenshoot
// //
@@ -1092,17 +1037,15 @@ void Draw_SaveScreenshoot(char *filename){
int i, half_height, line_size; int i, half_height, line_size;
// Create the surface // Create the surface
surf = SDL_CreateRGBSurface(SDL_SWSURFACE, surf = SDL_CreateRGBSurface(SDL_SWSURFACE, _width, _height, 32, 0, 0, 0, 0);
_width, _height, 32,0,0,0,0);
surf->format->Amask = 0xFF000000; surf->format->Amask = 0xFF000000;
surf->format->Ashift = 24; surf->format->Ashift = 24;
SDL_SetAlpha(surf, SDL_SRCALPHA, 255); SDL_SetAlpha(surf, SDL_SRCALPHA, 255);
// Get the screenshot // Get the screenshot
SDL_LockSurface(surf); SDL_LockSurface(surf);
glReadPixels(0, 0, glReadPixels(0, 0, _width, _height, GL_RGBA, GL_UNSIGNED_BYTE,
_width, _height, GL_RGBA, surf->pixels);
GL_UNSIGNED_BYTE, surf->pixels);
SDL_UnlockSurface(surf); SDL_UnlockSurface(surf);
// Flip the image data // Flip the image data
@@ -1111,8 +1054,10 @@ void Draw_SaveScreenshoot(char *filename){
image_line = malloc(line_size); image_line = malloc(line_size);
for (i = 0; i < half_height; i++) { for (i = 0; i < half_height; i++) {
memcpy(image_line, surf->pixels + i * line_size, line_size); memcpy(image_line, surf->pixels + i * line_size, line_size);
memcpy(surf->pixels+i*line_size,surf->pixels+(_height-(i+1))*line_size,line_size); memcpy(surf->pixels + i * line_size,
memcpy(surf->pixels+(_height-(i+1))*line_size,image_line,line_size); surf->pixels + (_height - (i + 1)) * line_size, line_size);
memcpy(surf->pixels + (_height - (i + 1)) * line_size, image_line,
line_size);
} }
// Swap RGB to BGR // Swap RGB to BGR
@@ -1140,6 +1085,3 @@ void Draw_SaveScreenshoot(char *filename){
SDL_FreeSurface(surf); SDL_FreeSurface(surf);
#endif #endif
} }

View File

@@ -3,82 +3,66 @@
#ifndef _DRAW_H_ #ifndef _DRAW_H_
#define _DRAW_H_ #define _DRAW_H_
///////////////////////////// /////////////////////////////
// Draw_Init // Draw_Init
// //
// Initializes the game window. // 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 // Draw_Clean
// //
// Cleans the game window. // Cleans the game window.
void Draw_Clean( void Draw_Clean(unsigned char r, unsigned char g, unsigned char b);
unsigned char r,
unsigned char g,
unsigned char b);
///////////////////////////// /////////////////////////////
// Draw_Loop // Draw_Loop
// //
// Loops updating the game window. // Loops updating the game window.
void Draw_Loop( void Draw_Loop(void (*proc)(void *data), void (*draw)(void *data, float f),
void (*proc)(void *data),
void (*draw)(void *data,float f),
void *data); void *data);
///////////////////////////// /////////////////////////////
// Draw_BreakLoop // Draw_BreakLoop
// //
// Breaks the drawing loop // Breaks the drawing loop
void Draw_BreakLoop(); void Draw_BreakLoop();
///////////////////////////// /////////////////////////////
// Draw_OverrideExit // Draw_OverrideExit
// //
// Overrides the default exit mechanism // Overrides the default exit mechanism
void Draw_OverrideExit(int override); void Draw_OverrideExit(int override);
///////////////////////////// /////////////////////////////
// Draw_Flush // Draw_Flush
// //
// Performs all the queued draw actions. // Performs all the queued draw actions.
void Draw_Flush(); void Draw_Flush();
//////////////////////////////////////////////// ////////////////////////////////////////////////
// DrawImg // // DrawImg //
///////////// /////////////
// Reference to a image. // Reference to a image.
typedef void *DrawImg; typedef void *DrawImg;
///////////////////////////// /////////////////////////////
// Draw_CreateImage // Draw_CreateImage
// //
DrawImg Draw_CreateImage(int w, int h); DrawImg Draw_CreateImage(int w, int h);
///////////////////////////// /////////////////////////////
// Draw_LoadImage // Draw_LoadImage
// //
// Loads a image, giving a reference. // Loads a image, giving a reference.
DrawImg Draw_LoadImage(char *filename); DrawImg Draw_LoadImage(char *filename);
///////////////////////////// /////////////////////////////
// Draw_GetSize // Draw_GetSize
// //
// Gets the image size. // Gets the image size.
void Draw_GetSize(DrawImg img, int *w, int *h); void Draw_GetSize(DrawImg img, int *w, int *h);
///////////////////////////// /////////////////////////////
// Draw_SetOffset // Draw_SetOffset
// Draw_GetOffset // Draw_GetOffset
@@ -87,7 +71,6 @@ void Draw_GetSize(DrawImg img,int *w,int *h);
void Draw_SetOffset(DrawImg img, int x, int y); void Draw_SetOffset(DrawImg img, int x, int y);
void Draw_GetOffset(DrawImg img, int *x, int *y); void Draw_GetOffset(DrawImg img, int *x, int *y);
///////////////////////////// /////////////////////////////
// Draw_SetFlip // Draw_SetFlip
// Draw_GetFlip // Draw_GetFlip
@@ -96,41 +79,36 @@ void Draw_GetOffset(DrawImg img,int *x,int *y);
void Draw_SetFlip(DrawImg img, int flip); void Draw_SetFlip(DrawImg img, int flip);
int Draw_GetFlip(DrawImg img); int Draw_GetFlip(DrawImg img);
///////////////////////////// /////////////////////////////
// Draw_DrawImg // Draw_DrawImg
// //
// Draws an image. // Draws an image.
void Draw_DrawImg(DrawImg img, int x, int y); void Draw_DrawImg(DrawImg img, int x, int y);
///////////////////////////// /////////////////////////////
// Draw_DrawImgResized // Draw_DrawImgResized
// //
// Draws an image, resizing. // Draws an image, resizing.
void Draw_DrawImgResized(DrawImg img, int x, int y, float w, float h); void Draw_DrawImgResized(DrawImg img, int x, int y, float w, float h);
///////////////////////////// /////////////////////////////
// Draw_DrawImgPart // Draw_DrawImgPart
// //
// Draws an image part. // Draws an image part.
void Draw_DrawImgPart(DrawImg img, int x, int y, int w, int h, int i, int j); void Draw_DrawImgPart(DrawImg img, int x, int y, int w, int h, int i, int j);
///////////////////////////// /////////////////////////////
// Draw_DrawImgPartHoriz // Draw_DrawImgPartHoriz
// //
// Draws an image part horizontally. // Draws an image part horizontally.
void Draw_DrawImgPartHoriz(DrawImg img, int x, int y, int w, int i); void Draw_DrawImgPartHoriz(DrawImg img, int x, int y, int w, int i);
///////////////////////////// /////////////////////////////
// Draw_ImgParallax // Draw_ImgParallax
// //
// //
void Draw_ImgParallax(DrawImg img, int imgSize[2], int imgOffset[2], float parallaxFactor[2], int gamePos[2], int gameSize[2]); void Draw_ImgParallax(DrawImg img, int imgSize[2], int imgOffset[2],
float parallaxFactor[2], int gamePos[2], int gameSize[2]);
///////////////////////////// /////////////////////////////
// Draw_SetColor // Draw_SetColor
@@ -138,44 +116,35 @@ void Draw_ImgParallax(DrawImg img, int imgSize[2], int imgOffset[2], float paral
// //
void Draw_SetColor(float r, float g, float b, float a); void Draw_SetColor(float r, float g, float b, float a);
//////////////////////////////////////////////// ////////////////////////////////////////////////
// DrawFnt // // DrawFnt //
///////////// /////////////
// Reference to a Font. // Reference to a Font.
typedef void *DrawFnt; typedef void *DrawFnt;
///////////////////////////// /////////////////////////////
// Draw_DefaultFont // Draw_DefaultFont
// //
// Creates the default font. // Creates the default font.
DrawFnt Draw_DefaultFont( DrawFnt Draw_DefaultFont(unsigned char r, unsigned char g, unsigned char b,
unsigned char r,
unsigned char g,
unsigned char b,
unsigned char a); unsigned char a);
///////////////////////////// /////////////////////////////
// Draw_LoadFont // Draw_LoadFont
// //
// Load a font from a file. // Load a font from a file.
DrawFnt Draw_LoadFont(char *fichero, int min, int max); DrawFnt Draw_LoadFont(char *fichero, int min, int max);
///////////////////////////// /////////////////////////////
// Draw_DrawText // Draw_DrawText
// //
// Draws text using a font // Draws text using a font
void Draw_DrawText(DrawFnt f, char *text, int x, int y); void Draw_DrawText(DrawFnt f, char *text, int x, int y);
///////////////////////////// /////////////////////////////
// Draw_SaveScreenshoot // Draw_SaveScreenshoot
// //
// //
void Draw_SaveScreenshoot(char *filename); void Draw_SaveScreenshoot(char *filename);
#endif #endif

View File

@@ -10,15 +10,11 @@
#include "Entity.h" #include "Entity.h"
#define EntityIntFlag_UpdateLight 1 #define EntityIntFlag_UpdateLight 1
#define EntityIntFlag_UpdatedPos 2 #define EntityIntFlag_UpdatedPos 2
#define EntityIntFlag_UpdatedColor 4 #define EntityIntFlag_UpdatedColor 4
#define EntityIntFlag_UpdateColor 8 #define EntityIntFlag_UpdateColor 8
///////////////////////////// /////////////////////////////
// Entity_New // Entity_New
// //
@@ -82,8 +78,8 @@ Entity Entity_New(){
e->color[0] = e->color[1] = e->color[2] = e->color[3] = 1.0f; e->color[0] = e->color[1] = e->color[2] = e->color[3] = 1.0f;
e->light[0] = e->light[1] = e->light[2] = e->light[3] = 1.0f; e->light[0] = e->light[1] = e->light[2] = e->light[3] = 1.0f;
e->defaultColor[0]=e->defaultColor[1]= e->defaultColor[0] = e->defaultColor[1] = e->defaultColor[2] =
e->defaultColor[2]=e->defaultColor[3]=1.0f; e->defaultColor[3] = 1.0f;
e->oncopy = NULL; e->oncopy = NULL;
e->oninit = NULL; e->oninit = NULL;
@@ -104,7 +100,6 @@ Entity Entity_New(){
return (e); return (e);
} }
///////////////////////////// /////////////////////////////
// Entity_Init // Entity_Init
// //
@@ -114,7 +109,6 @@ Entity Entity_Init(Entity e){
} }
} }
///////////////////////////// /////////////////////////////
// Entity_Destroy // Entity_Destroy
// //
@@ -127,7 +121,6 @@ void Entity_Destroy(Entity e){
_free_entity = e; _free_entity = e;
} }
///////////////////////////// /////////////////////////////
// Entity_Copy // Entity_Copy
// //
@@ -198,7 +191,6 @@ Entity Entity_Copy(Entity e){
return (n); return (n);
} }
///////////////////////////// /////////////////////////////
// Entity_CalcBBox // Entity_CalcBBox
// //
@@ -224,21 +216,18 @@ void Entity_CalcBBox(Entity e){
} }
} }
///////////////////////////// /////////////////////////////
// Entity_BBoxIntersect // Entity_BBoxIntersect
// //
// //
int Entity_BBoxIntersect(Entity ent1, Entity ent2) { int Entity_BBoxIntersect(Entity ent1, Entity ent2) {
if (ent1->maxX >= ent2->minX && ent1->minX <= ent2->maxX && if (ent1->maxX >= ent2->minX && ent1->minX <= ent2->maxX &&
ent1->maxY>=ent2->minY && ent1->minY<=ent2->maxY ) ent1->maxY >= ent2->minY && ent1->minY <= ent2->maxY) {
{
return (1); return (1);
} }
return (0); return (0);
} }
///////////////////////////// /////////////////////////////
// Entity_Draw // Entity_Draw
// //
@@ -246,12 +235,10 @@ int Entity_BBoxIntersect(Entity ent1,Entity ent2){
void Entity_Draw(Entity e, int x, int y, float f) { void Entity_Draw(Entity e, int x, int y, float f) {
vec2 fPos; vec2 fPos;
if (e->internalFlags & EntityIntFlag_UpdatedColor) { if (e->internalFlags & EntityIntFlag_UpdatedColor) {
Draw_SetColor( Draw_SetColor(e->color0[0] - f * (e->color0[0] - e->color[0]),
e->color0[0]-f*(e->color0[0]-e->color[0]),
e->color0[1] - f * (e->color0[1] - e->color[1]), e->color0[1] - f * (e->color0[1] - e->color[1]),
e->color0[2] - f * (e->color0[2] - e->color[2]), e->color0[2] - f * (e->color0[2] - e->color[2]),
e->color0[3]-f*(e->color0[3]-e->color[3]) e->color0[3] - f * (e->color0[3] - e->color[3]));
);
} else { } else {
Draw_SetColor(e->color[0], e->color[1], e->color[2], e->color[3]); Draw_SetColor(e->color[0], e->color[1], e->color[2], e->color[3]);
} }
@@ -263,7 +250,6 @@ void Entity_Draw(Entity e,int x,int y,float f){
} }
} }
///////////////////////////// /////////////////////////////
// Entity_IsVisible // Entity_IsVisible
// //
@@ -280,18 +266,13 @@ int Entity_IsVisible(Entity e,int x,int y,int w,int h){
ymin = y - ih; ymin = y - ih;
ymax = y + h + ih; ymax = y + h + ih;
if(e->pos[0]<xmin || if (e->pos[0] < xmin || e->pos[0] > xmax || e->pos[1] < ymin ||
e->pos[0]>xmax || e->pos[1] > ymax) {
e->pos[1]<ymin ||
e->pos[1]>ymax)
{
return (0); return (0);
} }
return (1); return (1);
} }
///////////////////////////// /////////////////////////////
// Entity_Process // Entity_Process
// //
@@ -305,7 +286,6 @@ void Entity_Process(Entity b,int ft){
} }
} }
///////////////////////////// /////////////////////////////
// Entity_PostProcess // Entity_PostProcess
// //
@@ -335,8 +315,8 @@ void Entity_PostProcess(Entity e,int ft){
vec2_set(e->vel, 0, 0); vec2_set(e->vel, 0, 0);
} else { } else {
// Apply dynamic friction // Apply dynamic friction
vec2_scale(e->vel,e->vel, vec2_scale(e->vel, e->vel, 1.0f - (e->backFric_dynamic +
1.0f-(e->backFric_dynamic+(e->backFric_static/len))); (e->backFric_static / len)));
} }
// Mark the update of the position. // Mark the update of the position.
@@ -355,13 +335,13 @@ void Entity_PostProcess(Entity e,int ft){
AnimPlay_IncTime(&e->anim, ft); AnimPlay_IncTime(&e->anim, ft);
} }
///////////////////////////// /////////////////////////////
// CollisionInfo_New // CollisionInfo_New
// //
// //
CollisionInfo _free_collInfo = NULL; CollisionInfo _free_collInfo = NULL;
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, vec2 n, int applyFriction) {
CollisionInfo collInfo; CollisionInfo collInfo;
if (!_free_collInfo) { if (!_free_collInfo) {
@@ -382,13 +362,14 @@ CollisionInfo CollisionInfo_New(int responseType,Entity ent1,Entity ent2,float t
return collInfo; return collInfo;
} }
///////////////////////////// /////////////////////////////
// CollisionInfo_Destroy // CollisionInfo_Destroy
// //
// //
void CollisionInfo_Destroy(CollisionInfo *collInfoRef) { void CollisionInfo_Destroy(CollisionInfo *collInfoRef) {
if(collInfoRef==NULL || collInfoRef[0]==NULL){return;} if (collInfoRef == NULL || collInfoRef[0] == NULL) {
return;
}
CollisionInfo collInfo = collInfoRef[0]; CollisionInfo collInfo = collInfoRef[0];
CollisionInfo nextCollInfo; CollisionInfo nextCollInfo;
@@ -401,18 +382,20 @@ void CollisionInfo_Destroy(CollisionInfo *collInfoRef){
collInfoRef[0] = NULL; collInfoRef[0] = NULL;
} }
///////////////////////////// /////////////////////////////
// CollisionInfo_Add // CollisionInfo_Add
// //
// //
void CollisionInfo_Add(CollisionInfo *collInfoRef, void CollisionInfo_Add(CollisionInfo *collInfoRef, int responseType,
int responseType,Entity ent1,Entity ent2,float t,vec2 n,int applyFriction) Entity ent1, Entity ent2, float t, vec2 n,
{ int applyFriction) {
if(collInfoRef==NULL){return;} if (collInfoRef == NULL) {
return;
}
CollisionInfo prevCollInfo = NULL; CollisionInfo prevCollInfo = NULL;
CollisionInfo collInfo = collInfoRef[0]; CollisionInfo collInfo = collInfoRef[0];
CollisionInfo newCollInfo=CollisionInfo_New(responseType,ent1,ent2,t,n,applyFriction); CollisionInfo newCollInfo =
CollisionInfo_New(responseType, ent1, ent2, t, n, applyFriction);
while (collInfo != NULL && collInfo->t < t) { while (collInfo != NULL && collInfo->t < t) {
prevCollInfo = collInfo; prevCollInfo = collInfo;
@@ -426,17 +409,15 @@ void CollisionInfo_Add(CollisionInfo *collInfoRef,
newCollInfo->next = collInfo; newCollInfo->next = collInfo;
} }
///////////////////////////// /////////////////////////////
// CollisionInfo_CheckRepetition // CollisionInfo_CheckRepetition
// //
// //
int CollisionInfo_CheckRepetition(CollisionInfo collInfo,Entity ent1,Entity ent2) int CollisionInfo_CheckRepetition(CollisionInfo collInfo, Entity ent1,
{ Entity ent2) {
while (collInfo != NULL) { while (collInfo != NULL) {
if ((collInfo->ent1 == ent1 && collInfo->ent2 == ent2) || if ((collInfo->ent1 == ent1 && collInfo->ent2 == ent2) ||
(collInfo->ent1==ent2 && collInfo->ent2==ent1)) (collInfo->ent1 == ent2 && collInfo->ent2 == ent1)) {
{
return (1); return (1);
} }
collInfo = collInfo->next; collInfo = collInfo->next;
@@ -444,18 +425,17 @@ int CollisionInfo_CheckRepetition(CollisionInfo collInfo,Entity ent1,Entity ent2
return (0); return (0);
} }
///////////////////////////// /////////////////////////////
// Entity_CheckCollisions // Entity_CheckCollisions
// //
// //
int Entity_CheckCollision(Entity ent1,Entity ent2,CollisionInfo *collInfoRef){ int Entity_CheckCollision(Entity ent1, Entity ent2,
CollisionInfo *collInfoRef) {
float t; float t;
vec2 n; vec2 n;
vec2 vel; vec2 vel;
int flags = ent1->flags | ent2->flags; int flags = ent1->flags | ent2->flags;
if (flags & EntityFlag_Block) { if (flags & EntityFlag_Block) {
// One of the entities is a block and none is a platform // One of the entities is a block and none is a platform
Entity ent, ent_block; Entity ent, ent_block;
@@ -467,8 +447,7 @@ int Entity_CheckCollision(Entity ent1,Entity ent2,CollisionInfo *collInfoRef){
if (ent1->mass <= 0.0f && ent2->mass > 0.0f) { if (ent1->mass <= 0.0f && ent2->mass > 0.0f) {
ent = ent2; ent = ent2;
ent_block = ent1; ent_block = ent1;
}else } else if (ent2->mass <= 0.0f && ent1->mass > 0.0f) {
if(ent2->mass<=0.0f && ent1->mass>0.0f){
ent = ent1; ent = ent1;
ent_block = ent2; ent_block = ent2;
} else { } else {
@@ -482,11 +461,11 @@ int Entity_CheckCollision(Entity ent1,Entity ent2,CollisionInfo *collInfoRef){
if (flags & EntityFlag_BlockTop) { if (flags & EntityFlag_BlockTop) {
vec2_set(auxN, 0, -1); vec2_set(auxN, 0, -1);
vec2_scaleadd(p,ent_block->pos,auxN,(ent->height+ent_block->height)/2); vec2_scaleadd(p, ent_block->pos, auxN,
(ent->height + ent_block->height) / 2);
block_len = ent_block->width + ent->width; block_len = ent_block->width + ent->width;
if(Intersect_RayEdge(ent->pos,ent->vel, if (Intersect_RayEdge(ent->pos, ent->vel, auxN, p, block_len,
auxN,p,block_len,&auxT)) &auxT)) {
{
if (auxT < t) { if (auxT < t) {
vec2_copy(n, auxN); vec2_copy(n, auxN);
t = auxT; t = auxT;
@@ -497,11 +476,11 @@ int Entity_CheckCollision(Entity ent1,Entity ent2,CollisionInfo *collInfoRef){
if (flags & EntityFlag_BlockBottom) { if (flags & EntityFlag_BlockBottom) {
vec2_set(auxN, 0, 1); vec2_set(auxN, 0, 1);
vec2_scaleadd(p,ent_block->pos,auxN,(ent->height+ent_block->height)/2); vec2_scaleadd(p, ent_block->pos, auxN,
(ent->height + ent_block->height) / 2);
block_len = ent_block->width + ent->width; block_len = ent_block->width + ent->width;
if(Intersect_RayEdge(ent->pos,ent->vel, if (Intersect_RayEdge(ent->pos, ent->vel, auxN, p, block_len,
auxN,p,block_len,&auxT)) &auxT)) {
{
if (auxT < t) { if (auxT < t) {
vec2_copy(n, auxN); vec2_copy(n, auxN);
t = auxT; t = auxT;
@@ -512,11 +491,11 @@ int Entity_CheckCollision(Entity ent1,Entity ent2,CollisionInfo *collInfoRef){
if (flags & EntityFlag_BlockRight) { if (flags & EntityFlag_BlockRight) {
vec2_set(auxN, 1, 0); vec2_set(auxN, 1, 0);
vec2_scaleadd(p,ent_block->pos,auxN,(ent->width+ent_block->width)/2); vec2_scaleadd(p, ent_block->pos, auxN,
(ent->width + ent_block->width) / 2);
block_len = ent_block->height + ent->height; block_len = ent_block->height + ent->height;
if(Intersect_RayEdge(ent->pos,ent->vel, if (Intersect_RayEdge(ent->pos, ent->vel, auxN, p, block_len,
auxN,p,block_len,&auxT)) &auxT)) {
{
if (auxT < t) { if (auxT < t) {
vec2_copy(n, auxN); vec2_copy(n, auxN);
t = auxT; t = auxT;
@@ -527,11 +506,11 @@ int Entity_CheckCollision(Entity ent1,Entity ent2,CollisionInfo *collInfoRef){
if (flags & EntityFlag_BlockLeft) { if (flags & EntityFlag_BlockLeft) {
vec2_set(auxN, -1, 0); vec2_set(auxN, -1, 0);
vec2_scaleadd(p,ent_block->pos,auxN,(ent->width+ent_block->width)/2); vec2_scaleadd(p, ent_block->pos, auxN,
(ent->width + ent_block->width) / 2);
block_len = ent_block->height + ent->height; block_len = ent_block->height + ent->height;
if(Intersect_RayEdge(ent->pos,ent->vel, if (Intersect_RayEdge(ent->pos, ent->vel, auxN, p, block_len,
auxN,p,block_len,&auxT)) &auxT)) {
{
if (auxT < t) { if (auxT < t) {
vec2_copy(n, auxN); vec2_copy(n, auxN);
t = auxT; t = auxT;
@@ -541,8 +520,8 @@ int Entity_CheckCollision(Entity ent1,Entity ent2,CollisionInfo *collInfoRef){
} }
if (t < 1.0f) { if (t < 1.0f) {
CollisionInfo_Add(collInfoRef, CollisionInfo_Add(collInfoRef, CollisionResponse_Line, ent,
CollisionResponse_Line,ent,ent_block,t,n,applyFriction); ent_block, t, n, applyFriction);
return (1); return (1);
} }
@@ -551,22 +530,20 @@ int Entity_CheckCollision(Entity ent1,Entity ent2,CollisionInfo *collInfoRef){
// Circle-Circle test from ent1 // Circle-Circle test from ent1
vec2_minus(vel, ent1->vel, ent2->vel); vec2_minus(vel, ent1->vel, ent2->vel);
if(Colision_CircleCircle(ent1->pos,ent1->radius,vel,ent2->pos,ent2->radius,&t,n)){ if (Colision_CircleCircle(ent1->pos, ent1->radius, vel, ent2->pos,
CollisionInfo_Add(collInfoRef, ent2->radius, &t, n)) {
CollisionResponse_Circle,ent1,ent2,t,n,0); CollisionInfo_Add(collInfoRef, CollisionResponse_Circle, ent1, ent2, t,
n, 0);
return (1); return (1);
} }
return (0); return (0);
} }
///////////////////////////// /////////////////////////////
// Entity_CollisionResponseCircle // Entity_CollisionResponseCircle
// //
// Normal response to a collision between circles. // Normal response to a collision between circles.
void Entity_CollisionResponseCircle( void Entity_CollisionResponseCircle(Entity b1, Entity b2, float t, vec2 n) {
Entity b1,Entity b2,float t,vec2 n)
{
float moment; float moment;
vec2 temp; vec2 temp;
float elast; float elast;
@@ -578,28 +555,24 @@ void Entity_CollisionResponseCircle(
// Collision between two massed balls // Collision between two massed balls
moment = ((1.0f + elast) * b1->mass * b2->mass * moment = ((1.0f + elast) * b1->mass * b2->mass *
(fabs(vec2_dot(b1->vel,n))+fabs(vec2_dot(b2->vel,n)))) (fabs(vec2_dot(b1->vel, n)) + fabs(vec2_dot(b2->vel, n)))) /
/(b1->mass+b2->mass); (b1->mass + b2->mass);
vec2_scale(temp, n, moment / b1->mass); vec2_scale(temp, n, moment / b1->mass);
vec2_minus(b1->vel, b1->vel, temp); vec2_minus(b1->vel, b1->vel, temp);
Entity_CalcBBox(b1); Entity_CalcBBox(b1);
vec2_scale(temp, n, moment / b2->mass); vec2_scale(temp, n, moment / b2->mass);
vec2_plus(b2->vel, b2->vel, temp); vec2_plus(b2->vel, b2->vel, temp);
Entity_CalcBBox(b2); Entity_CalcBBox(b2);
}else } else if (b1->mass > 0.0f && b2->mass <= 0.0f) {
if(b1->mass>0.0f && b2->mass<=0.0f){
// Collision between a massed ball and a fixed ball // Collision between a massed ball and a fixed ball
moment=(1.0f+b1->elast)* moment = (1.0f + b1->elast) * (vec2_dot(b1->vel, n));
(vec2_dot(b1->vel,n));
vec2_scale(temp, n, moment); vec2_scale(temp, n, moment);
vec2_minus(b1->vel, b1->vel, temp); vec2_minus(b1->vel, b1->vel, temp);
Entity_CalcBBox(b1); Entity_CalcBBox(b1);
}else } else if (b1->mass <= 0.0f && b2->mass > 0.0f) {
if(b1->mass<=0.0f && b2->mass>0.0f){
// Collision between a massed ball and a fixed ball // Collision between a massed ball and a fixed ball
// (imposible, but better safe) // (imposible, but better safe)
moment=(1.0f+b2->elast)* moment = (1.0f + b2->elast) * (vec2_dot(b2->vel, n));
(vec2_dot(b2->vel,n));
vec2_scale(temp, n, moment); vec2_scale(temp, n, moment);
vec2_plus(b2->vel, b2->vel, temp); vec2_plus(b2->vel, b2->vel, temp);
Entity_CalcBBox(b2); Entity_CalcBBox(b2);
@@ -613,14 +586,12 @@ void Entity_CollisionResponseCircle(
} }
} }
///////////////////////////// /////////////////////////////
// Entity_CollisionResponseLine // Entity_CollisionResponseLine
// //
// Normal response to a collision with a line. // Normal response to a collision with a line.
void Entity_CollisionResponseLine( void Entity_CollisionResponseLine(Entity ent, Entity ent2, float t, vec2 norm,
Entity ent,Entity ent2,float t,vec2 norm,int applyFriction) int applyFriction) {
{
vec2 pos2, vel2, velFric, intersection; vec2 pos2, vel2, velFric, intersection;
float dist, fric_static, fric_dynamic, fricLen; float dist, fric_static, fric_dynamic, fricLen;
@@ -650,8 +621,7 @@ void Entity_CollisionResponseLine(
vec2_scaleadd(pos2, intersection, velFric, vec2_scaleadd(pos2, intersection, velFric,
1.0f - (fric_dynamic + (fric_static / fricLen))); 1.0f - (fric_dynamic + (fric_static / fricLen)));
} else { } else {
vec2_scaleadd(pos2,intersection,velFric, vec2_scaleadd(pos2, intersection, velFric, 1.0f - fric_dynamic);
1.0f-fric_dynamic);
} }
} }
} }
@@ -663,7 +633,6 @@ void Entity_CollisionResponseLine(
Entity_CalcBBox(ent); Entity_CalcBBox(ent);
} }
///////////////////////////// /////////////////////////////
// Entity_CollisionInfoResponse // Entity_CollisionInfoResponse
// //
@@ -680,14 +649,16 @@ int Entity_CollisionInfoResponse(CollisionInfo collInfo){
// Check the collision methods // Check the collision methods
if (collInfo->ent1->collision) { if (collInfo->ent1->collision) {
rc=collInfo->ent1->collision(collInfo->ent1,collInfo->ent2,collInfo->t,n1); rc = collInfo->ent1->collision(collInfo->ent1, collInfo->ent2,
collInfo->t, n1);
if (rc == 0) if (rc == 0)
response = 0; response = 0;
if (rc > 1) if (rc > 1)
response = 2; response = 2;
} }
if (collInfo->ent2->collision) { if (collInfo->ent2->collision) {
rc=collInfo->ent2->collision(collInfo->ent2,collInfo->ent1,collInfo->t,n2); rc = collInfo->ent2->collision(collInfo->ent2, collInfo->ent1,
collInfo->t, n2);
if (rc == 0) if (rc == 0)
response = 0; response = 0;
if (rc > 1) if (rc > 1)
@@ -697,16 +668,17 @@ int Entity_CollisionInfoResponse(CollisionInfo collInfo){
// Collision response // Collision response
if (response == 1) { if (response == 1) {
if (collInfo->responseType == CollisionResponse_Line) { if (collInfo->responseType == CollisionResponse_Line) {
Entity_CollisionResponseLine( Entity_CollisionResponseLine(collInfo->ent1, collInfo->ent2,
collInfo->ent1,collInfo->ent2,collInfo->t,collInfo->n,collInfo->applyFriction); collInfo->t, collInfo->n,
}else collInfo->applyFriction);
if(collInfo->responseType==CollisionResponse_Circle){ } else if (collInfo->responseType == CollisionResponse_Circle) {
if (vec2_dot(collInfo->ent1->vel, collInfo->ent1->vel) > if (vec2_dot(collInfo->ent1->vel, collInfo->ent1->vel) >
vec2_dot(collInfo->ent2->vel,collInfo->ent2->vel)) vec2_dot(collInfo->ent2->vel, collInfo->ent2->vel)) {
{ Entity_CollisionResponseCircle(
Entity_CollisionResponseCircle(collInfo->ent1,collInfo->ent2,collInfo->t,n2); collInfo->ent1, collInfo->ent2, collInfo->t, n2);
} else { } else {
Entity_CollisionResponseCircle(collInfo->ent2,collInfo->ent1,collInfo->t,n1); Entity_CollisionResponseCircle(
collInfo->ent2, collInfo->ent1, collInfo->t, n1);
} }
} }
return (1); return (1);
@@ -720,7 +692,6 @@ int Entity_CollisionInfoResponse(CollisionInfo collInfo){
return (0); return (0);
} }
///////////////////////////// /////////////////////////////
// Entity_Overlaps // Entity_Overlaps
// //
@@ -732,30 +703,22 @@ void Entity_Overlaps(Entity b1,Entity b2){
vec2_set(len, fabs(b1->pos[0] - b2->pos[0]), fabs(b1->pos[1] - b2->pos[1])); vec2_set(len, fabs(b1->pos[0] - b2->pos[0]), fabs(b1->pos[1] - b2->pos[1]));
if (b1->overlap) { if (b1->overlap) {
if( len[0]<=b1->radius && if (len[0] <= b1->radius && len[1] <= b1->radius) {
len[1]<=b1->radius)
{
b1->overlap(b1, b2); b1->overlap(b1, b2);
} }
} }
if (b2->overlap) { if (b2->overlap) {
if( len[0]<=b2->radius && if (len[0] <= b2->radius && len[1] <= b2->radius) {
len[1]<=b2->radius)
{
b2->overlap(b2, b1); b2->overlap(b2, b1);
} }
} }
} }
///////////////////////////// /////////////////////////////
// Entity_GetPos // Entity_GetPos
// //
// //
void Entity_GetPos(Entity e,vec2 pos){ void Entity_GetPos(Entity e, vec2 pos) { vec2_copy(pos, e->pos); }
vec2_copy(pos,e->pos);
}
///////////////////////////// /////////////////////////////
// Entity_SetPos // Entity_SetPos
@@ -768,7 +731,6 @@ void Entity_SetPos(Entity e,vec2 pos){
Entity_CalcBBox(e); Entity_CalcBBox(e);
} }
///////////////////////////// /////////////////////////////
// Entity_AddPos // Entity_AddPos
// //
@@ -780,7 +742,6 @@ void Entity_AddPos(Entity e,vec2 pos){
Entity_CalcBBox(e); Entity_CalcBBox(e);
} }
///////////////////////////// /////////////////////////////
// Entity_UpdatePos // Entity_UpdatePos
// //
@@ -794,7 +755,6 @@ void Entity_UpdatePos(Entity e,vec2 pos){
vec2_copy(e->pos, pos); vec2_copy(e->pos, pos);
} }
///////////////////////////// /////////////////////////////
// Entity_AddVel // Entity_AddVel
// //
@@ -803,7 +763,6 @@ void Entity_AddVel(Entity e,vec2 vel){
Entity_CalcBBox(e); Entity_CalcBBox(e);
} }
///////////////////////////// /////////////////////////////
// Entity_SetVel // Entity_SetVel
// //
@@ -812,7 +771,6 @@ void Entity_SetVel(Entity e,vec2 vel){
Entity_CalcBBox(e); Entity_CalcBBox(e);
} }
///////////////////////////// /////////////////////////////
// Entity_SetVelH // Entity_SetVelH
// //
@@ -821,7 +779,6 @@ void Entity_SetVelH(Entity e,float v){
Entity_CalcBBox(e); Entity_CalcBBox(e);
} }
///////////////////////////// /////////////////////////////
// Entity_SetVelV // Entity_SetVelV
// //
@@ -830,7 +787,6 @@ void Entity_SetVelV(Entity e,float v){
Entity_CalcBBox(e); Entity_CalcBBox(e);
} }
///////////////////////////// /////////////////////////////
// Entity_AddVelLimit // Entity_AddVelLimit
// //
@@ -856,7 +812,6 @@ void Entity_AddVelLimit(Entity e,vec2 vel,float limit){
Entity_CalcBBox(e); Entity_CalcBBox(e);
} }
///////////////////////////// /////////////////////////////
// Entity_AddVelLimitH // Entity_AddVelLimitH
// //
@@ -874,7 +829,6 @@ void Entity_AddVelLimitH(Entity e,float v,float limit){
Entity_CalcBBox(e); Entity_CalcBBox(e);
} }
///////////////////////////// /////////////////////////////
// Entity_AddVelLimitH // Entity_AddVelLimitH
// //
@@ -892,7 +846,6 @@ void Entity_AddVelLimitV(Entity e,float v,float limit){
Entity_CalcBBox(e); Entity_CalcBBox(e);
} }
///////////////////////////// /////////////////////////////
// Entity_SetColor // Entity_SetColor
// //
@@ -909,7 +862,6 @@ void Entity_SetColor(Entity e,float r,float g,float b,float a){
e->internalFlags &= ~EntityIntFlag_UpdatedColor; e->internalFlags &= ~EntityIntFlag_UpdatedColor;
} }
///////////////////////////// /////////////////////////////
// Entity_AddColor // Entity_AddColor
// //
@@ -942,7 +894,6 @@ void Entity_MultColor(Entity e,float r,float g,float b,float a){
e->internalFlags |= EntityIntFlag_UpdatedColor; e->internalFlags |= EntityIntFlag_UpdatedColor;
} }
///////////////////////////// /////////////////////////////
// Entity_SetLight // Entity_SetLight
// //
@@ -958,7 +909,6 @@ void Entity_SetLight(Entity e,float r,float g,float b,float rad){
} }
} }
///////////////////////////// /////////////////////////////
// Entity_SetDefaultColor // Entity_SetDefaultColor
// //
@@ -970,7 +920,6 @@ void Entity_SetDefaultColor(Entity e,float r,float g,float b,float a){
e->defaultColor[3] = a; e->defaultColor[3] = a;
} }
///////////////////////////// /////////////////////////////
// Entity_Iluminate // Entity_Iluminate
// //
@@ -982,11 +931,8 @@ void Entity_Iluminate(Entity e,Entity *elist,int n){
float qrad; float qrad;
if (e->flags & EntityFlag_Light) { if (e->flags & EntityFlag_Light) {
Entity_SetColor(e, Entity_SetColor(e, e->defaultColor[0], e->defaultColor[1],
e->defaultColor[0], e->defaultColor[2], e->defaultColor[3]);
e->defaultColor[1],
e->defaultColor[2],
e->defaultColor[3]);
return; return;
} }
@@ -1004,19 +950,13 @@ void Entity_Iluminate(Entity e,Entity *elist,int n){
qrad = elist[i]->light[3] * elist[i]->light[3]; qrad = elist[i]->light[3] * elist[i]->light[3];
if (qdist < qrad) { if (qdist < qrad) {
f = 1.0f - qdist / qrad; f = 1.0f - qdist / qrad;
Entity_AddColor(e, Entity_AddColor(e, f * elist[i]->light[0], f * elist[i]->light[1],
f*elist[i]->light[0], f * elist[i]->light[2], 0.0f);
f*elist[i]->light[1],
f*elist[i]->light[2],
0.0f);
} }
} }
Entity_MultColor(e, Entity_MultColor(e, e->defaultColor[0], e->defaultColor[1],
e->defaultColor[0], e->defaultColor[2], e->defaultColor[3]);
e->defaultColor[1],
e->defaultColor[2],
e->defaultColor[3]);
e->internalFlags &= ~EntityIntFlag_UpdateLight; e->internalFlags &= ~EntityIntFlag_UpdateLight;
if (e->internalFlags & EntityIntFlag_UpdateColor) { if (e->internalFlags & EntityIntFlag_UpdateColor) {
@@ -1028,7 +968,6 @@ void Entity_Iluminate(Entity e,Entity *elist,int n){
} }
} }
///////////////////////////// /////////////////////////////
// Entity_MarkUpdateLight // Entity_MarkUpdateLight
// //
@@ -1053,12 +992,9 @@ void Entity_MarkUpdateLight(Entity e,Entity *elist,int n){
max[1] = e->pos0[1] + e->light[3]; max[1] = e->pos0[1] + e->light[3];
} }
for (i = 0; i < n; i++) { for (i = 0; i < n; i++) {
if( elist[i]!=NULL && if (elist[i] != NULL && min[0] <= elist[i]->pos0[0] &&
min[0]<=elist[i]->pos0[0] && max[0] >= elist[i]->pos0[0] && min[1] <= elist[i]->pos0[1] &&
max[0]>=elist[i]->pos0[0] && max[1] >= elist[i]->pos0[1]) {
min[1]<=elist[i]->pos0[1] &&
max[1]>=elist[i]->pos0[1])
{
elist[i]->internalFlags |= EntityIntFlag_UpdateLight; elist[i]->internalFlags |= EntityIntFlag_UpdateLight;
} }
} }
@@ -1067,14 +1003,10 @@ void Entity_MarkUpdateLight(Entity e,Entity *elist,int n){
} }
} }
///////////////////////////// /////////////////////////////
// Entity_IsLight // Entity_IsLight
// //
int Entity_IsLight(Entity e){ int Entity_IsLight(Entity e) { return (e->flags & EntityFlag_Light); }
return (e->flags&EntityFlag_Light);
}
///////////////////////////// /////////////////////////////
// Entity_IsUpdateLight // Entity_IsUpdateLight
@@ -1083,13 +1015,9 @@ int Entity_IsUpdateLight(Entity e){
return (e->internalFlags & EntityIntFlag_UpdateLight); return (e->internalFlags & EntityIntFlag_UpdateLight);
} }
///////////////////////////// /////////////////////////////
// Entity_IsMoving // Entity_IsMoving
// //
int Entity_IsMoving(Entity e) { int Entity_IsMoving(Entity e) {
return (e->internalFlags & EntityIntFlag_UpdatedPos); return (e->internalFlags & EntityIntFlag_UpdatedPos);
} }

View File

@@ -7,7 +7,6 @@
#include "Draw.h" #include "Draw.h"
#include "Anim.h" #include "Anim.h"
//////////////////////////////////////////////// ////////////////////////////////////////////////
// Entity // Entity
// //
@@ -77,69 +76,58 @@ struct TEntity {
Entity next; Entity next;
}; };
///////////////////////////// /////////////////////////////
// Entity_New // Entity_New
// //
Entity Entity_New(); Entity Entity_New();
///////////////////////////// /////////////////////////////
// Entity_Init // Entity_Init
// //
Entity Entity_Init(Entity e); Entity Entity_Init(Entity e);
///////////////////////////// /////////////////////////////
// Entity_Destroy // Entity_Destroy
// //
void Entity_Destroy(Entity e); void Entity_Destroy(Entity e);
///////////////////////////// /////////////////////////////
// Entity_Copy // Entity_Copy
// //
Entity Entity_Copy(Entity e); Entity Entity_Copy(Entity e);
///////////////////////////// /////////////////////////////
// Entity_CalcBBox // Entity_CalcBBox
// //
// //
void Entity_CalcBBox(Entity e); void Entity_CalcBBox(Entity e);
///////////////////////////// /////////////////////////////
// Entity_BBoxIntersect // Entity_BBoxIntersect
// //
// //
int Entity_BBoxIntersect(Entity ent1, Entity ent2); int Entity_BBoxIntersect(Entity ent1, Entity ent2);
///////////////////////////// /////////////////////////////
// Entity_Draw // Entity_Draw
// //
void Entity_Draw(Entity e, int x, int y, float f); void Entity_Draw(Entity e, int x, int y, float f);
///////////////////////////// /////////////////////////////
// Entity_IsVisible // Entity_IsVisible
// //
int Entity_IsVisible(Entity e, int x, int y, int w, int h); int Entity_IsVisible(Entity e, int x, int y, int w, int h);
///////////////////////////// /////////////////////////////
// Entity_Process // Entity_Process
// //
void Entity_Process(Entity e, int ft); void Entity_Process(Entity e, int ft);
///////////////////////////// /////////////////////////////
// Entity_PostProcess // Entity_PostProcess
// //
void Entity_PostProcess(Entity e, int ft); void Entity_PostProcess(Entity e, int ft);
//////////////////////////////////////////////// ////////////////////////////////////////////////
// CollisionInfo // CollisionInfo
// //
@@ -157,13 +145,12 @@ struct TCollisionInfo {
CollisionInfo next; CollisionInfo next;
}; };
///////////////////////////// /////////////////////////////
// CollisionInfo_New // 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, vec2 n, int applyFriction);
///////////////////////////// /////////////////////////////
// CollisionInfo_Destroy // CollisionInfo_Destroy
@@ -171,21 +158,19 @@ CollisionInfo CollisionInfo_New(int responseType,Entity ent1,Entity ent2,float t
// //
void CollisionInfo_Destroy(CollisionInfo *collInfoRef); void CollisionInfo_Destroy(CollisionInfo *collInfoRef);
///////////////////////////// /////////////////////////////
// CollisionInfo_Add // CollisionInfo_Add
// //
// //
void CollisionInfo_Add(CollisionInfo *collInfo, void CollisionInfo_Add(CollisionInfo *collInfo, int responseType, Entity ent1,
int responseType,Entity ent1,Entity ent2,float t,vec2 n,int applyFriction); Entity ent2, float t, vec2 n, int applyFriction);
///////////////////////////// /////////////////////////////
// CollisionInfo_CheckRepetition // CollisionInfo_CheckRepetition
// //
// //
int CollisionInfo_CheckRepetition(CollisionInfo collInfo,Entity ent1,Entity ent2); int CollisionInfo_CheckRepetition(CollisionInfo collInfo, Entity ent1,
Entity ent2);
///////////////////////////// /////////////////////////////
// Entity_CheckCollision // Entity_CheckCollision
@@ -193,22 +178,18 @@ int CollisionInfo_CheckRepetition(CollisionInfo collInfo,Entity ent1,Entity ent2
// //
int Entity_CheckCollision(Entity ent1, Entity ent2, CollisionInfo *collInfoRef); int Entity_CheckCollision(Entity ent1, Entity ent2, CollisionInfo *collInfoRef);
///////////////////////////// /////////////////////////////
// Entity_CollisionResponseClircle // Entity_CollisionResponseClircle
// //
// Normal response to a collision of spheres. // Normal response to a collision of spheres.
void Entity_CollisionResponseCircle( void Entity_CollisionResponseCircle(Entity b1, Entity b2, float t, vec2 n);
Entity b1,Entity b2,float t,vec2 n);
///////////////////////////// /////////////////////////////
// Entity_CollisionResponseLine // Entity_CollisionResponseLine
// //
// Normal response to a collision with a line. // Normal response to a collision with a line.
void Entity_CollisionResponseLine( void Entity_CollisionResponseLine(Entity ent, Entity ent2, float t, vec2 n,
Entity ent,Entity ent2,float t,vec2 n,int applyFriction); int applyFriction);
///////////////////////////// /////////////////////////////
// Entity_CollisionInfoResponse // Entity_CollisionInfoResponse
@@ -216,141 +197,117 @@ void Entity_CollisionResponseLine(
// //
int Entity_CollisionInfoResponse(CollisionInfo collInfo); int Entity_CollisionInfoResponse(CollisionInfo collInfo);
///////////////////////////// /////////////////////////////
// Entity_Overlaps // Entity_Overlaps
// //
void Entity_Overlaps(Entity b1, Entity b2); void Entity_Overlaps(Entity b1, Entity b2);
///////////////////////////// /////////////////////////////
// Entity_GetPos // Entity_GetPos
// //
void Entity_GetPos(Entity e, vec2 pos); void Entity_GetPos(Entity e, vec2 pos);
///////////////////////////// /////////////////////////////
// Entity_SetPos // Entity_SetPos
// //
// //
void Entity_SetPos(Entity e, vec2 pos); void Entity_SetPos(Entity e, vec2 pos);
///////////////////////////// /////////////////////////////
// Entity_AddPos // Entity_AddPos
// //
// //
void Entity_AddPos(Entity e, vec2 pos); void Entity_AddPos(Entity e, vec2 pos);
///////////////////////////// /////////////////////////////
// Entity_UpdatePos // Entity_UpdatePos
// //
void Entity_UpdatePos(Entity e, vec2 pos); void Entity_UpdatePos(Entity e, vec2 pos);
///////////////////////////// /////////////////////////////
// Entity_AddVel // Entity_AddVel
// //
void Entity_AddVel(Entity e, vec2 vel); void Entity_AddVel(Entity e, vec2 vel);
///////////////////////////// /////////////////////////////
// Entity_SetVel // Entity_SetVel
// //
void Entity_SetVel(Entity e, vec2 vel); void Entity_SetVel(Entity e, vec2 vel);
///////////////////////////// /////////////////////////////
// Entity_SetVelH // Entity_SetVelH
// //
void Entity_SetVelH(Entity e, float v); void Entity_SetVelH(Entity e, float v);
///////////////////////////// /////////////////////////////
// Entity_SetVelV // Entity_SetVelV
// //
void Entity_SetVelV(Entity e, float v); void Entity_SetVelV(Entity e, float v);
///////////////////////////// /////////////////////////////
// Entity_AddVelLimit // Entity_AddVelLimit
// //
void Entity_AddVelLimit(Entity e, vec2 vel, float limit); void Entity_AddVelLimit(Entity e, vec2 vel, float limit);
///////////////////////////// /////////////////////////////
// Entity_AddVelLimitH // Entity_AddVelLimitH
// //
void Entity_AddVelLimitH(Entity e, float v, float limit); void Entity_AddVelLimitH(Entity e, float v, float limit);
///////////////////////////// /////////////////////////////
// Entity_AddVelLimitH // Entity_AddVelLimitH
// //
void Entity_AddVelLimitV(Entity e, float v, float limit); void Entity_AddVelLimitV(Entity e, float v, float limit);
///////////////////////////// /////////////////////////////
// Entity_SetColor // Entity_SetColor
// //
void Entity_SetColor(Entity e, float r, float g, float b, float a); void Entity_SetColor(Entity e, float r, float g, float b, float a);
///////////////////////////// /////////////////////////////
// Entity_AddColor // Entity_AddColor
// //
void Entity_AddColor(Entity e, float r, float g, float b, float a); void Entity_AddColor(Entity e, float r, float g, float b, float a);
///////////////////////////// /////////////////////////////
// Entity_MultColor // Entity_MultColor
// //
// //
void Entity_MultColor(Entity e, float r, float g, float b, float a); void Entity_MultColor(Entity e, float r, float g, float b, float a);
///////////////////////////// /////////////////////////////
// Entity_AddColor // Entity_AddColor
// //
void Entity_SetLight(Entity e, float r, float g, float b, float rad); void Entity_SetLight(Entity e, float r, float g, float b, float rad);
///////////////////////////// /////////////////////////////
// Entity_SetDefaultColor // Entity_SetDefaultColor
// //
void Entity_SetDefaultColor(Entity e, float r, float g, float b, float a); void Entity_SetDefaultColor(Entity e, float r, float g, float b, float a);
///////////////////////////// /////////////////////////////
// Entity_Iluminate // Entity_Iluminate
// //
void Entity_Iluminate(Entity e, Entity *elist, int n); void Entity_Iluminate(Entity e, Entity *elist, int n);
///////////////////////////// /////////////////////////////
// Entity_MarkUpdateLight // Entity_MarkUpdateLight
// //
void Entity_MarkUpdateLight(Entity e, Entity *elist, int n); void Entity_MarkUpdateLight(Entity e, Entity *elist, int n);
///////////////////////////// /////////////////////////////
// Entity_IsLight // Entity_IsLight
// //
int Entity_IsLight(Entity e); int Entity_IsLight(Entity e);
///////////////////////////// /////////////////////////////
// Entity_IsUpdateLight // Entity_IsUpdateLight
// //
int Entity_IsUpdateLight(Entity e); int Entity_IsUpdateLight(Entity e);
///////////////////////////// /////////////////////////////
// Entity_IsMoving // Entity_IsMoving
// //
int Entity_IsMoving(Entity e); int Entity_IsMoving(Entity e);
#endif #endif

View File

@@ -53,7 +53,6 @@ int _nParallaxBackgrounds=0;
int gamelib_debug = 0; int gamelib_debug = 0;
///////////////////////////// /////////////////////////////
// GameLib_Init // GameLib_Init
// //
@@ -79,7 +78,6 @@ int GameLib_Init(int w,int h,char *title,int pfps,int fps){
return (1); return (1);
} }
///////////////////////////// /////////////////////////////
// GameLib_AddEntity // GameLib_AddEntity
// //
@@ -122,7 +120,6 @@ void GameLib_AddEntity(Entity e){
Entity_Init(e); Entity_Init(e);
} }
///////////////////////////// /////////////////////////////
// GameLib_UnrefEntity // GameLib_UnrefEntity
// //
@@ -148,7 +145,6 @@ int GameLib_UnrefEntity(Entity e){
return (-1); return (-1);
} }
///////////////////////////// /////////////////////////////
// GameLib_DelEntity // GameLib_DelEntity
// //
@@ -169,7 +165,6 @@ int GameLib_DelEntity(Entity e){
return (1); return (1);
} }
///////////////////////////// /////////////////////////////
// GameLib_Compactate // GameLib_Compactate
// //
@@ -196,7 +191,6 @@ void GameLib_Compactate(){
_entities_compactate = 0; _entities_compactate = 0;
} }
///////////////////////////// /////////////////////////////
// GameLib_ProcLoop // GameLib_ProcLoop
// //
@@ -233,19 +227,18 @@ void GameLib_ProcLoop(void *data){
repeat = 0; repeat = 0;
CollisionInfo collInfo = NULL; CollisionInfo collInfo = NULL;
for (i = 0; i < _n_entities; i++) { for (i = 0; i < _n_entities; i++) {
if(!(_entity[i]->flags&EntityFlag_Collision) || _entity[i]->mass<0.0f) if (!(_entity[i]->flags & EntityFlag_Collision) ||
_entity[i]->mass < 0.0f)
continue; continue;
if (_entity[i]->vel[0] <= 0.0f && _entity[i]->vel[0] >= -0.0f && 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) _entity[i]->vel[1] <= 0.0f && _entity[i]->vel[1] >= -0.0f) {
{
continue; continue;
} }
for (j = 0; j < _n_entities; j++) { for (j = 0; j < _n_entities; j++) {
if( i==j || if (i == j || !(_entity[j]->flags & EntityFlag_Collision) ||
!(_entity[j]->flags&EntityFlag_Collision) || CollisionInfo_CheckRepetition(collInfo, _entity[i],
CollisionInfo_CheckRepetition(collInfo,_entity[i],_entity[j]) || _entity[j]) ||
!Entity_BBoxIntersect(_entity[i],_entity[j])) !Entity_BBoxIntersect(_entity[i], _entity[j])) {
{
continue; continue;
} }
Entity_CheckCollision(_entity[i], _entity[j], &collInfo); Entity_CheckCollision(_entity[i], _entity[j], &collInfo);
@@ -261,13 +254,12 @@ void GameLib_ProcLoop(void *data){
// Stop remaining collisions // Stop remaining collisions
if (count == 10) { if (count == 10) {
for (i = 0; i < _n_entities; i++) { for (i = 0; i < _n_entities; i++) {
if(!(_entity[i]->flags&EntityFlag_Collision) || _entity[i]->mass<0.0f) if (!(_entity[i]->flags & EntityFlag_Collision) ||
_entity[i]->mass < 0.0f)
continue; continue;
for (j = 0; j < _n_entities; j++) { for (j = 0; j < _n_entities; j++) {
if(i==j || if (i == j || !(_entity[j]->flags & EntityFlag_Collision) ||
!(_entity[j]->flags&EntityFlag_Collision) || !Entity_BBoxIntersect(_entity[i], _entity[j])) {
!Entity_BBoxIntersect(_entity[i],_entity[j]))
{
continue; continue;
} }
if (Entity_CheckCollision(_entity[i], _entity[j], NULL)) { if (Entity_CheckCollision(_entity[i], _entity[j], NULL)) {
@@ -287,7 +279,8 @@ void GameLib_ProcLoop(void *data){
time = Time_GetTime(); time = Time_GetTime();
_entities_lock = 1; _entities_lock = 1;
for (i = 0; i < _n_entities; i++) { for (i = 0; i < _n_entities; i++) {
if(!(_entity[i]->flags&EntityFlag_Overlap) || _entity[i]->mass<0.0f) if (!(_entity[i]->flags & EntityFlag_Overlap) ||
_entity[i]->mass < 0.0f)
continue; continue;
for (j = 0; j < _n_entities; j++) { for (j = 0; j < _n_entities; j++) {
if (!(_entity[j]->flags & EntityFlag_Overlap) || i == j) if (!(_entity[j]->flags & EntityFlag_Overlap) || i == j)
@@ -311,8 +304,7 @@ void GameLib_ProcLoop(void *data){
if (ent1->zorder > ent2->zorder) { if (ent1->zorder > ent2->zorder) {
// Lower level // Lower level
swap = 1; swap = 1;
}else } else if (ent1->zorder < ent2->zorder) {
if(ent1->zorder < ent2->zorder){
// Upper level // Upper level
} else { } else {
// Same level // Same level
@@ -352,7 +344,6 @@ void GameLib_ProcLoop(void *data){
fproc_count++; fproc_count++;
} }
///////////////////////////// /////////////////////////////
// GameLib_DrawLoop // GameLib_DrawLoop
// //
@@ -379,12 +370,9 @@ void GameLib_DrawLoop(void *data, float f){
// Draw parallax backgrounds // Draw parallax backgrounds
for (i = 0; i < _nParallaxBackgrounds; i++) { for (i = 0; i < _nParallaxBackgrounds; i++) {
Draw_ImgParallax( Draw_ImgParallax(
_parallaxBackground[i].img, _parallaxBackground[i].img, _parallaxBackground[i].imgSize,
_parallaxBackground[i].imgSize,
_parallaxBackground[i].imgOffset, _parallaxBackground[i].imgOffset,
_parallaxBackground[i].parallaxFactor, _parallaxBackground[i].parallaxFactor, game_pos, _game_size);
game_pos,
_game_size);
} }
// Draw entities // Draw entities
@@ -393,10 +381,8 @@ void GameLib_DrawLoop(void *data, float f){
Entity e = _entity[i]; Entity e = _entity[i];
// Check visivility // Check visivility
if(!Entity_IsVisible(e, if (!Entity_IsVisible(e, game_pos[0], game_pos[1], _game_size[0],
game_pos[0],game_pos[1], _game_size[1])) {
_game_size[0],_game_size[1]))
{
continue; continue;
} }
@@ -420,8 +406,7 @@ void GameLib_DrawLoop(void *data, float f){
fdraw_count++; fdraw_count++;
if (Input_GetKey(InputKey_DumpProfiling) == InputKey_Pressed && if (Input_GetKey(InputKey_DumpProfiling) == InputKey_Pressed &&
fproc_count>0 && fdraw_count>0) fproc_count > 0 && fdraw_count > 0) {
{
Print("Profiling:::::::::\n"); Print("Profiling:::::::::\n");
Print("t_proc.....:%6lld\n", t_proc / fproc_count); Print("t_proc.....:%6lld\n", t_proc / fproc_count);
Print("t_col......:%6lld\n", t_col / fproc_count); Print("t_col......:%6lld\n", t_col / fproc_count);
@@ -438,17 +423,12 @@ void GameLib_DrawLoop(void *data, float f){
} }
} }
///////////////////////////// /////////////////////////////
// GameLib_Loop // GameLib_Loop
// //
// Loops the game. // Loops the game.
void GameLib_Loop( void GameLib_Loop(void (*gameproc)(), void (*gamepostproc)(),
void (*gameproc)(), void (*gamepredraw)(float f), void (*gamedraw)(float f)) {
void (*gamepostproc)(),
void (*gamepredraw)(float f),
void (*gamedraw)(float f))
{
_gameproc = gameproc; _gameproc = gameproc;
_gamepostproc = gamepostproc; _gamepostproc = gamepostproc;
_gamepredraw = gamepredraw; _gamepredraw = gamepredraw;
@@ -463,7 +443,6 @@ void GameLib_Loop(
Draw_Loop(GameLib_ProcLoop, GameLib_DrawLoop, NULL); Draw_Loop(GameLib_ProcLoop, GameLib_DrawLoop, NULL);
} }
///////////////////////////// /////////////////////////////
// GameLib_GetPos // GameLib_GetPos
// GameLib_SetPos // GameLib_SetPos
@@ -495,9 +474,6 @@ void GameLib_GetPosInstant(int pos[2],float f){
pos[1] = _game_pos0[1] + f * (_game_pos1[1] - _game_pos0[1]); pos[1] = _game_pos0[1] + f * (_game_pos1[1] - _game_pos0[1]);
} }
///////////////////////////// /////////////////////////////
// GameLib_MoveToPos // GameLib_MoveToPos
// GameLib_MoveToPosH // GameLib_MoveToPosH
@@ -509,15 +485,14 @@ void GameLib_MoveToPos(vec2 pos,float f){
GameLib_MoveToPosV(pos, f); GameLib_MoveToPosV(pos, f);
} }
void GameLib_MoveToPosH(vec2 pos, float 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; _game_pos1[0] =
_game_pos1[0] + (pos[0] - (_game_pos1[0] + (_game_size[0] / 2.0f))) * f;
} }
void GameLib_MoveToPosV(vec2 pos, float 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; _game_pos1[1] =
_game_pos1[1] + (pos[1] - (_game_pos1[1] + (_game_size[1] / 2.0f))) * f;
} }
///////////////////////////// /////////////////////////////
// GameLib_ForEachEn // GameLib_ForEachEn
// //
@@ -533,7 +508,6 @@ void GameLib_DelEnts(){
_n_entities = 0; _n_entities = 0;
} }
///////////////////////////// /////////////////////////////
// GameLib_ForEachEn // GameLib_ForEachEn
// //
@@ -549,7 +523,6 @@ void GameLib_ForEachEnt(int (*func)(Entity ent)){
} }
} }
///////////////////////////// /////////////////////////////
// GameLib_SearchEnt // GameLib_SearchEnt
// //
@@ -568,7 +541,6 @@ Entity GameLib_SearchEnt(int (*func)(Entity ent,void *d),void *d){
return ent; return ent;
} }
///////////////////////////// /////////////////////////////
// GameLib_EntityCustomCheckCollision // GameLib_EntityCustomCheckCollision
// //
@@ -585,8 +557,7 @@ int GameLib_EntityCustomCheckCollision(Entity ent,vec2 vel){
for (j = 0; j < _n_entities; j++) { for (j = 0; j < _n_entities; j++) {
if (!(_entity[j]->flags & EntityFlag_Collision) || if (!(_entity[j]->flags & EntityFlag_Collision) ||
!Entity_BBoxIntersect(ent,_entity[j])) !Entity_BBoxIntersect(ent, _entity[j])) {
{
continue; continue;
} }
Entity_CheckCollision(ent, _entity[j], &collInfo); Entity_CheckCollision(ent, _entity[j], &collInfo);
@@ -603,7 +574,6 @@ int GameLib_EntityCustomCheckCollision(Entity ent,vec2 vel){
return collision; return collision;
} }
///////////////////////////// /////////////////////////////
// GameLib_PlaySound // GameLib_PlaySound
// //
@@ -644,7 +614,6 @@ void GameLib_PlaySound(AudioSnd snd,int x,int y){
Audio_PlaySound(snd, vleft, vright, 0); Audio_PlaySound(snd, vleft, vright, 0);
} }
///////////////////////////// /////////////////////////////
// GameLib_PlayLoopingSound // GameLib_PlayLoopingSound
// //
@@ -654,7 +623,6 @@ AudioChn GameLib_PlayLoopingSound(AudioSnd snd){
return Audio_PlaySound(snd, 1.0f, 1.0f, 1); return Audio_PlaySound(snd, 1.0f, 1.0f, 1);
} }
///////////////////////////// /////////////////////////////
// GameLib_EntitySetLight // GameLib_EntitySetLight
// //
@@ -669,14 +637,11 @@ void GameLib_EntitySetLight(Entity e,float r,float g,float b,float rad){
} }
} }
///////////////////////////// /////////////////////////////
// GameLib_ConvertScreenPositionToGamePosition // GameLib_ConvertScreenPositionToGamePosition
// //
// //
void GameLib_ConvertScreenPositionToGamePosition( void GameLib_ConvertScreenPositionToGamePosition(vec2 screenPos, vec2 gamePos) {
vec2 screenPos, vec2 gamePos)
{
float f; float f;
int game_pos[2]; int game_pos[2];
@@ -687,12 +652,12 @@ void GameLib_ConvertScreenPositionToGamePosition(
gamePos[1] = (screenPos[1] * _game_size[1]) + game_pos[1]; gamePos[1] = (screenPos[1] * _game_size[1]) + game_pos[1];
} }
///////////////////////////// /////////////////////////////
// GameLib_AddParallaxBackground // GameLib_AddParallaxBackground
// //
// //
void GameLib_AddParallaxBackground(DrawImg img, int imgSize[2], int imgOffset[2], float parallaxFactor[2]){ void GameLib_AddParallaxBackground(DrawImg img, int imgSize[2],
int imgOffset[2], float parallaxFactor[2]) {
int idx = _nParallaxBackgrounds; int idx = _nParallaxBackgrounds;
if ((idx + 1) >= MaxParallaxBackgrounds) { if ((idx + 1) >= MaxParallaxBackgrounds) {
Print("GameLib: Can't add parallaxBackground, limit reached."); Print("GameLib: Can't add parallaxBackground, limit reached.");
@@ -708,12 +673,8 @@ void GameLib_AddParallaxBackground(DrawImg img, int imgSize[2], int imgOffset[2]
_nParallaxBackgrounds++; _nParallaxBackgrounds++;
} }
///////////////////////////// /////////////////////////////
// GameLib_CleanParallaxBackgrounds // GameLib_CleanParallaxBackgrounds
// //
// //
void GameLib_CleanParallaxBackgrounds(){ void GameLib_CleanParallaxBackgrounds() { _nParallaxBackgrounds = 0; }
_nParallaxBackgrounds=0;
}

View File

@@ -11,45 +11,36 @@
#include "Anim.h" #include "Anim.h"
#include "Entity.h" #include "Entity.h"
///////////////////////////// /////////////////////////////
// GameLib_Init // GameLib_Init
// //
// Initializes the game. // 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 // GameLib_AddEntity
// //
// Adds an entity to the game. // Adds an entity to the game.
void GameLib_AddEntity(Entity e); void GameLib_AddEntity(Entity e);
///////////////////////////// /////////////////////////////
// GameLib_UnrefEntity // GameLib_UnrefEntity
// //
// removes the reference to the entity. // removes the reference to the entity.
int GameLib_UnrefEntity(Entity e); int GameLib_UnrefEntity(Entity e);
///////////////////////////// /////////////////////////////
// GameLib_DelEntity // GameLib_DelEntity
// //
// Adds an entity to the game. // Adds an entity to the game.
int GameLib_DelEntity(Entity e); int GameLib_DelEntity(Entity e);
///////////////////////////// /////////////////////////////
// GameLib_Loop // GameLib_Loop
// //
// Loops the game. // Loops the game.
void GameLib_Loop( void GameLib_Loop(void (*gameproc)(), void (*gamepostproc)(),
void (*gameproc)(), void (*gamepredraw)(float f), void (*gamedraw)(float f));
void (*gamepostproc)(),
void (*gamepredraw)(float f),
void (*gamedraw)(float f));
///////////////////////////// /////////////////////////////
// GameLib_GetPos // GameLib_GetPos
@@ -65,7 +56,6 @@ void GameLib_UpdatePos(int pos[2]);
void GameLib_GetSize(int size[2]); void GameLib_GetSize(int size[2]);
void GameLib_GetPosInstant(int pos[2], float f); void GameLib_GetPosInstant(int pos[2], float f);
///////////////////////////// /////////////////////////////
// GameLib_MoveToPos // GameLib_MoveToPos
// GameLib_MoveToPosH // GameLib_MoveToPosH
@@ -76,70 +66,60 @@ void GameLib_MoveToPos(vec2 pos,float f);
void GameLib_MoveToPosH(vec2 pos, float f); void GameLib_MoveToPosH(vec2 pos, float f);
void GameLib_MoveToPosV(vec2 pos, float f); void GameLib_MoveToPosV(vec2 pos, float f);
///////////////////////////// /////////////////////////////
// GameLib_ForEachEn // GameLib_ForEachEn
// //
// Deletes every entity. // Deletes every entity.
void GameLib_DelEnts(); void GameLib_DelEnts();
///////////////////////////// /////////////////////////////
// GameLib_ForEachEnt // GameLib_ForEachEnt
// //
// Iterates every entity. // Iterates every entity.
void GameLib_ForEachEnt(int (*func)(Entity ent)); void GameLib_ForEachEnt(int (*func)(Entity ent));
///////////////////////////// /////////////////////////////
// GameLib_SearchEnt // GameLib_SearchEnt
// //
// Searches throught the entities. // Searches throught the entities.
Entity GameLib_SearchEnt(int (*func)(Entity ent, void *d), void *d); Entity GameLib_SearchEnt(int (*func)(Entity ent, void *d), void *d);
///////////////////////////// /////////////////////////////
// GameLib_EntityCustomCheckCollision // GameLib_EntityCustomCheckCollision
// //
// //
int GameLib_EntityCustomCheckCollision(Entity ent, vec2 vel); int GameLib_EntityCustomCheckCollision(Entity ent, vec2 vel);
///////////////////////////// /////////////////////////////
// GameLib_PlaySound // GameLib_PlaySound
// //
// Play a sound position aware. // Play a sound position aware.
void GameLib_PlaySound(AudioSnd snd, int x, int y); void GameLib_PlaySound(AudioSnd snd, int x, int y);
///////////////////////////// /////////////////////////////
// GameLib_PlayLoopingSound // GameLib_PlayLoopingSound
// //
// Play a sound looping // Play a sound looping
AudioChn GameLib_PlayLoopingSound(AudioSnd snd); AudioChn GameLib_PlayLoopingSound(AudioSnd snd);
///////////////////////////// /////////////////////////////
// GameLib_EntitySetLight // GameLib_EntitySetLight
// //
// //
void GameLib_EntitySetLight(Entity e, float r, float g, float b, float rad); void GameLib_EntitySetLight(Entity e, float r, float g, float b, float rad);
///////////////////////////// /////////////////////////////
// GameLib_ConvertScreenPositionToGamePosition // GameLib_ConvertScreenPositionToGamePosition
// //
// //
void GameLib_ConvertScreenPositionToGamePosition( void GameLib_ConvertScreenPositionToGamePosition(vec2 screenPos, vec2 gamePos);
vec2 screenPos, vec2 gamePos);
///////////////////////////// /////////////////////////////
// GameLib_AddParallaxBackground // GameLib_AddParallaxBackground
// //
// //
void GameLib_AddParallaxBackground(DrawImg img, int imgSize[2], int imgOffset[2], float parallaxFactor[2]); void GameLib_AddParallaxBackground(DrawImg img, int imgSize[2],
int imgOffset[2], float parallaxFactor[2]);
///////////////////////////// /////////////////////////////
// GameLib_CleanParallaxBackgrounds // GameLib_CleanParallaxBackgrounds

View File

@@ -9,8 +9,6 @@
#include "Util.h" #include "Util.h"
#include "Input.h" #include "Input.h"
// Globals // Globals
InputKeyStatus _keys[InputKey_Max]; InputKeyStatus _keys[InputKey_Max];
@@ -22,7 +20,6 @@ int _clicked=0;
float _clickedPositionX = 0; float _clickedPositionX = 0;
float _clickedPositionY = 0; float _clickedPositionY = 0;
///////////////////////////// /////////////////////////////
// Input_Init // Input_Init
// //
@@ -38,7 +35,6 @@ int Input_Init(){
return (1); return (1);
} }
///////////////////////////// /////////////////////////////
// Input_Frame // Input_Frame
// //
@@ -57,12 +53,12 @@ void Input_Frame(){
Input_SetKey(InputKey_Left, keys[SDLK_LEFT]); Input_SetKey(InputKey_Left, keys[SDLK_LEFT]);
Input_SetKey(InputKey_Right, keys[SDLK_RIGHT]); Input_SetKey(InputKey_Right, keys[SDLK_RIGHT]);
Input_SetKey(InputKey_Jump, keys[SDLK_SPACE]); Input_SetKey(InputKey_Jump, keys[SDLK_SPACE]);
Input_SetKey(InputKey_Continue,keys[SDLK_RETURN]|keys[SDLK_KP_ENTER]|_pointerDown); Input_SetKey(InputKey_Continue,
keys[SDLK_RETURN] | keys[SDLK_KP_ENTER] | _pointerDown);
Input_SetKey(InputKey_DumpProfiling, keys[SDLK_p]); Input_SetKey(InputKey_DumpProfiling, keys[SDLK_p]);
} }
///////////////////////////// /////////////////////////////
// Input_PostFrame // Input_PostFrame
// //
@@ -72,7 +68,6 @@ void Input_PostFrame(){
_clicked = 0; _clicked = 0;
} }
///////////////////////////// /////////////////////////////
// Input_SetKey // Input_SetKey
// //
@@ -89,15 +84,11 @@ void Input_SetKey(InputKey key,int status){
} }
} }
///////////////////////////// /////////////////////////////
// Input_GetKey // Input_GetKey
// //
// Reports a the status of a key. // Reports a the status of a key.
InputKeyStatus Input_GetKey(InputKey key){ InputKeyStatus Input_GetKey(InputKey key) { return (_keys[key]); }
return(_keys[key]);
}
///////////////////////////// /////////////////////////////
// Input_SetPointerPosition // Input_SetPointerPosition
@@ -107,7 +98,6 @@ void Input_SetPointerPosition(float x, float y){
_pointerY = y; _pointerY = y;
} }
///////////////////////////// /////////////////////////////
// Input_SetPointerDown // Input_SetPointerDown
// //
@@ -120,7 +110,6 @@ void Input_SetPointerDown(int pointerDown){
_pointerDown = pointerDown; _pointerDown = pointerDown;
} }
///////////////////////////// /////////////////////////////
// Input_GetPointerPosition // Input_GetPointerPosition
// //
@@ -130,7 +119,6 @@ int Input_GetPointerPosition(vec2 pointer){
return _pointerDown; return _pointerDown;
} }
///////////////////////////// /////////////////////////////
// Input_GetClickedPosition // Input_GetClickedPosition
// //
@@ -140,7 +128,6 @@ int Input_GetClickedPosition(vec2 clickPosition){
return _clicked; return _clicked;
} }
///////////////////////////// /////////////////////////////
// Input_AnyKey // Input_AnyKey
// //
@@ -155,7 +142,6 @@ int Input_AnyKey(){
return (0); return (0);
} }
///////////////////////////// /////////////////////////////
// Input_GetDir // Input_GetDir
// //

View File

@@ -5,28 +5,24 @@
#include "Util.h" #include "Util.h"
///////////////////////////// /////////////////////////////
// Input_Init // Input_Init
// //
// Initializes the game input. // Initializes the game input.
int Input_Init(); int Input_Init();
///////////////////////////// /////////////////////////////
// Input_Frame // Input_Frame
// //
// Notify a frame update to the input subsystem. // Notify a frame update to the input subsystem.
void Input_Frame(); void Input_Frame();
///////////////////////////// /////////////////////////////
// Input_PostFrame // Input_PostFrame
// //
// Notify a frame update end to the input subsystem. // Notify a frame update end to the input subsystem.
void Input_PostFrame(); void Input_PostFrame();
//////////////////////////////////////////////// ////////////////////////////////////////////////
// InputKey // // InputKey //
////////////// //////////////
@@ -47,14 +43,12 @@ typedef enum {
InputKey_Max InputKey_Max
} InputKey; } InputKey;
///////////////////////////// /////////////////////////////
// Input_SetKey // Input_SetKey
// //
// Notify a key press to the input subsystem. // Notify a key press to the input subsystem.
void Input_SetKey(InputKey key, int status); void Input_SetKey(InputKey key, int status);
//////////////////////////////////////////////// ////////////////////////////////////////////////
// InputKeyStatus // // InputKeyStatus //
/////////////////// ///////////////////
@@ -65,50 +59,42 @@ typedef enum {
InputKey_Holded InputKey_Holded
} InputKeyStatus; } InputKeyStatus;
///////////////////////////// /////////////////////////////
// Input_GetKey // Input_GetKey
// //
// Reports a the status of a key. // Reports a the status of a key.
InputKeyStatus Input_GetKey(InputKey key); InputKeyStatus Input_GetKey(InputKey key);
///////////////////////////// /////////////////////////////
// Input_SetPointerPosition // Input_SetPointerPosition
// //
void Input_SetPointerPosition(float x, float y); void Input_SetPointerPosition(float x, float y);
///////////////////////////// /////////////////////////////
// Input_SetPointerDown // Input_SetPointerDown
// //
void Input_SetPointerDown(int pointerDown); void Input_SetPointerDown(int pointerDown);
///////////////////////////// /////////////////////////////
// Input_GetPointerPosition // Input_GetPointerPosition
// //
int Input_GetPointerPosition(vec2 pointer); int Input_GetPointerPosition(vec2 pointer);
///////////////////////////// /////////////////////////////
// Input_GetClickedPosition // Input_GetClickedPosition
// //
int Input_GetClickedPosition(vec2 clickPosition); int Input_GetClickedPosition(vec2 clickPosition);
///////////////////////////// /////////////////////////////
// Input_AnyKey // Input_AnyKey
// //
// //
int Input_AnyKey(); int Input_AnyKey();
///////////////////////////// /////////////////////////////
// Input_GetDir // Input_GetDir
// //
// Reports the direction of the dpad. // Reports the direction of the dpad.
int Input_GetDir(vec2 dir); int Input_GetDir(vec2 dir);
#endif #endif

View File

@@ -6,7 +6,6 @@
#include "QuadArray2D.h" #include "QuadArray2D.h"
QuadArray2D QuadArray2D_Create(int resVertex) { QuadArray2D QuadArray2D_Create(int resVertex) {
QuadArray2D quadArray = NULL; QuadArray2D quadArray = NULL;
@@ -20,24 +19,24 @@ QuadArray2D QuadArray2D_Create(int resVertex){
} }
void QuadArray2D_Destroy(QuadArray2D *quadArray) { void QuadArray2D_Destroy(QuadArray2D *quadArray) {
if(!quadArray) return; if (!quadArray)
if(!quadArray[0]) return; return;
if (!quadArray[0])
return;
free(quadArray[0]->vertexData); free(quadArray[0]->vertexData);
free(quadArray[0]); free(quadArray[0]);
quadArray[0] = NULL; quadArray[0] = NULL;
} }
void QuadArray2D_Clean(QuadArray2D quadArray){ void QuadArray2D_Clean(QuadArray2D quadArray) { quadArray->nVertex = 0; }
quadArray->nVertex=0;
}
void QuadArray2D_AddVertex(QuadArray2D quadArray, float v[]) { void QuadArray2D_AddVertex(QuadArray2D quadArray, float v[]) {
if (quadArray->resVertex <= quadArray->nVertex) { if (quadArray->resVertex <= quadArray->nVertex) {
// Grow vertexData // Grow vertexData
quadArray->resVertex *= 2; quadArray->resVertex *= 2;
float *newVertexData=malloc(sizeof(float)*Vertex2D_Length* float *newVertexData =
quadArray->resVertex); malloc(sizeof(float) * Vertex2D_Length * quadArray->resVertex);
memcpy(newVertexData, quadArray->vertexData, memcpy(newVertexData, quadArray->vertexData,
sizeof(float) * Vertex2D_Length * quadArray->nVertex); sizeof(float) * Vertex2D_Length * quadArray->nVertex);
free(quadArray->vertexData); free(quadArray->vertexData);
@@ -45,18 +44,14 @@ void QuadArray2D_AddVertex(QuadArray2D quadArray,float v[]){
} }
// Add the vertex // Add the vertex
memcpy( memcpy(quadArray->vertexData + (Vertex2D_Length * quadArray->nVertex), v,
quadArray->vertexData+ sizeof(float) * Vertex2D_Length);
(Vertex2D_Length*quadArray->nVertex),
v,sizeof(float)*Vertex2D_Length);
quadArray->nVertex++; quadArray->nVertex++;
} }
void QuadArray2D_AddQuad(QuadArray2D quadArray, void QuadArray2D_AddQuad(QuadArray2D quadArray, float x0, float y0, float u0,
float x0, float y0,float u0, float v0, float v0, float x1, float y1, float u1, float v1,
float x1, float y1,float u1, float v1, float color[]) {
float color[])
{
float v[Vertex2D_Length]; float v[Vertex2D_Length];
int firstIndex = quadArray->nVertex; int firstIndex = quadArray->nVertex;
@@ -67,12 +62,35 @@ void QuadArray2D_AddQuad(QuadArray2D quadArray,
v[7] = color[3]; v[7] = color[3];
// Add the vertexes // Add the vertexes
v[0]=x0; v[1]=y0; v[2]=u0; v[3]=v0; QuadArray2D_AddVertex(quadArray,v); v[0] = x0;
v[0]=x1; v[1]=y0; v[2]=u1; v[3]=v0; QuadArray2D_AddVertex(quadArray,v); v[1] = y0;
v[0]=x1; v[1]=y1; v[2]=u1; v[3]=v1; QuadArray2D_AddVertex(quadArray,v); v[2] = u0;
v[3] = v0;
QuadArray2D_AddVertex(quadArray, v);
v[0] = x1;
v[1] = y0;
v[2] = u1;
v[3] = v0;
QuadArray2D_AddVertex(quadArray, v);
v[0] = x1;
v[1] = y1;
v[2] = u1;
v[3] = v1;
QuadArray2D_AddVertex(quadArray, v);
v[0]=x1; v[1]=y1; v[2]=u1; v[3]=v1; QuadArray2D_AddVertex(quadArray,v); v[0] = x1;
v[0]=x0; v[1]=y1; v[2]=u0; v[3]=v1; QuadArray2D_AddVertex(quadArray,v); v[1] = y1;
v[0]=x0; v[1]=y0; v[2]=u0; v[3]=v0; QuadArray2D_AddVertex(quadArray,v); v[2] = u1;
v[3] = v1;
QuadArray2D_AddVertex(quadArray, v);
v[0] = x0;
v[1] = y1;
v[2] = u0;
v[3] = v1;
QuadArray2D_AddVertex(quadArray, v);
v[0] = x0;
v[1] = y0;
v[2] = u0;
v[3] = v0;
QuadArray2D_AddVertex(quadArray, v);
} }

View File

@@ -6,7 +6,6 @@
// Vertex2D -> (x,y) (u,v) (r,g,b,a) // Vertex2D -> (x,y) (u,v) (r,g,b,a)
#define Vertex2D_Length 8 #define Vertex2D_Length 8
//////////////////////////////////////////////// ////////////////////////////////////////////////
// QuadArray2D // QuadArray2D
// //
@@ -17,7 +16,6 @@ struct TQuadArray2D {
int resVertex; int resVertex;
}; };
QuadArray2D QuadArray2D_Create(int resVertex); QuadArray2D QuadArray2D_Create(int resVertex);
void QuadArray2D_Destroy(QuadArray2D *quadArray); void QuadArray2D_Destroy(QuadArray2D *quadArray);
@@ -26,10 +24,8 @@ void QuadArray2D_Clean(QuadArray2D quadArray);
void QuadArray2D_AddVertex(QuadArray2D quadArray, float v[]); void QuadArray2D_AddVertex(QuadArray2D quadArray, float v[]);
void QuadArray2D_AddQuad(QuadArray2D quadArray, void QuadArray2D_AddQuad(QuadArray2D quadArray, float x0, float y0, float u0,
float x0, float y0,float u0, float v0, float v0, float x1, float y1, float u1, float v1,
float x1, float y1,float u1, float v1,
float color[]); float color[]);
#endif #endif

View File

@@ -8,7 +8,6 @@
#include "Time.h" #include "Time.h"
///////////////////////////// /////////////////////////////
// Time_GetTime // Time_GetTime
// //
@@ -61,5 +60,3 @@ void Time_Pause(int pausa){
select(0, NULL, NULL, NULL, &tv); select(0, NULL, NULL, NULL, &tv);
} }
#endif // if WIN32 #endif // if WIN32

View File

@@ -9,7 +9,6 @@
// Gets the current time in usecs. // Gets the current time in usecs.
long long Time_GetTime(); long long Time_GetTime();
///////////////////////////// /////////////////////////////
// Time_Pause // Time_Pause
// //

View File

@@ -8,7 +8,6 @@
#include "Util.h" #include "Util.h"
///////////////////////////// /////////////////////////////
// SolveQuadratic // SolveQuadratic
// //
@@ -34,7 +33,6 @@ int SolveQuadratic(float a,float b,float c,float *Rmin,float *Rmax){
return (1); return (1);
} }
//////////////////////////////////////////////// ////////////////////////////////////////////////
// vec2 // // vec2 //
////////// //////////
@@ -100,7 +98,6 @@ void vec2_orthogonalize8(vec2 v) {
} }
} }
///////////////////////////// /////////////////////////////
// Intersec_RayUnitCircle // Intersec_RayUnitCircle
// //
@@ -136,16 +133,12 @@ int Intersec_RayUnitCircle(vec2 orig,vec2 vel,vec2 center,float *t){
return (0); return (0);
} }
///////////////////////////// /////////////////////////////
// Colision_CircleCircle // Colision_CircleCircle
// //
// Colision point of a circle against another circle. // Colision point of a circle against another circle.
int Colision_CircleCircle( int Colision_CircleCircle(vec2 cir1, float rad1, vec2 vel, vec2 cir2,
vec2 cir1,float rad1,vec2 vel, float rad2, float *t, vec2 n) {
vec2 cir2,float rad2,
float *t,vec2 n)
{
vec2 vel_a, orig_a, cen_a, temp; vec2 vel_a, orig_a, cen_a, temp;
float rads, invrads; float rads, invrads;
float maxx, minx; float maxx, minx;
@@ -187,16 +180,12 @@ int Colision_CircleCircle(
return (0); return (0);
} }
///////////////////////////// /////////////////////////////
// Intersect_RayEdge // Intersect_RayEdge
// //
// Intersection between a ray and a edge. // Intersection between a ray and a edge.
int Intersect_RayEdge( int Intersect_RayEdge(vec2 pos, vec2 vel, vec2 norm, vec2 edgePos, float len,
vec2 pos,vec2 vel, float *t) {
vec2 norm,vec2 edgePos,float len,
float *t)
{
vec2 pos2, intersection, perp, edgePos2; vec2 pos2, intersection, perp, edgePos2;
float delta, d1, d2, hLen; float delta, d1, d2, hLen;
@@ -232,7 +221,6 @@ int Intersect_RayEdge(
return (0); return (0);
} }
///////////////////////////// /////////////////////////////
// absmod // absmod
// //
@@ -254,7 +242,6 @@ float fabsmod(float v,int d){
} }
} }
///////////////////////////// /////////////////////////////
// IsBigEndian // IsBigEndian
// //
@@ -266,7 +253,6 @@ int IsBigEndian(){
return bint.c[0] == 1; return bint.c[0] == 1;
} }
///////////////////////////// /////////////////////////////
// EndsWith // EndsWith
// //
@@ -280,7 +266,6 @@ int EndsWith(char *str, char *suffix){
return strncmp(str + lenStr - lenSuffix, suffix, lenSuffix) == 0; return strncmp(str + lenStr - lenSuffix, suffix, lenSuffix) == 0;
} }
///////////////////////////// /////////////////////////////
// Rand // Rand
// //
@@ -356,7 +341,6 @@ unsigned Rand_Get() {
return (val); return (val);
} }
///////////////////////////// /////////////////////////////
// Print // Print
// //
@@ -374,4 +358,3 @@ int Print(char *fmt, ...) {
fflush(stdout); fflush(stdout);
return (n); return (n);
} }

View File

@@ -12,21 +12,34 @@
// Solves a Quadratic equation using a, b and c coeficients. // 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);
//////////////////////////////////////////////// ////////////////////////////////////////////////
// vec2 // // vec2 //
////////// //////////
// A 2D vector. // A 2D vector.
typedef float vec2[2]; typedef float vec2[2];
#define vec2_set(v,x,y) (v)[0]=(x);(v)[1]=(y); #define vec2_set(v, x, y) \
#define vec2_copy(v1,v2) (v1)[0]=(v2)[0];(v1)[1]=(v2)[1]; (v)[0] = (x); \
#define vec2_plus(v,v1,v2) (v)[0]=(v1)[0]+(v2)[0];(v)[1]=(v1)[1]+(v2)[1]; (v)[1] = (y);
#define vec2_minus(v,v1,v2) (v)[0]=(v1)[0]-(v2)[0];(v)[1]=(v1)[1]-(v2)[1]; #define vec2_copy(v1, v2) \
#define vec2_scale(v,v1,s) (v)[0]=(v1)[0]*(s);(v)[1]=(v1)[1]*(s); (v1)[0] = (v2)[0]; \
(v1)[1] = (v2)[1];
#define vec2_plus(v, v1, v2) \
(v)[0] = (v1)[0] + (v2)[0]; \
(v)[1] = (v1)[1] + (v2)[1];
#define vec2_minus(v, v1, v2) \
(v)[0] = (v1)[0] - (v2)[0]; \
(v)[1] = (v1)[1] - (v2)[1];
#define vec2_scale(v, v1, s) \
(v)[0] = (v1)[0] * (s); \
(v)[1] = (v1)[1] * (s);
#define vec2_dot(v1, v2) ((v1)[0] * (v2)[0] + (v1)[1] * (v2)[1]) #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_len(v) sqrtf((v)[0] * (v)[0] + (v)[1] * (v)[1])
#define vec2_perp(v,n) (v)[0]=-(n)[1];(v)[1]=(n)[0]; #define vec2_perp(v, n) \
#define vec2_scaleadd(v,v1,v2,s) (v)[0]=(v2)[0]*(s)+(v1)[0];(v)[1]=(v2)[1]*(s)+(v1)[1]; (v)[0] = -(n)[1]; \
(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];
float vec2_norm(vec2 v); float vec2_norm(vec2 v);
#define vec2_interpol(v, v1, v2, f) \ #define vec2_interpol(v, v1, v2, f) \
(v)[0] = (v1)[0] - f * ((v1)[0] - (v2)[0]); \ (v)[0] = (v1)[0] - f * ((v1)[0] - (v2)[0]); \
@@ -34,53 +47,42 @@ float vec2_norm(vec2 v);
void vec2_orthogonalize4(vec2 v); void vec2_orthogonalize4(vec2 v);
void vec2_orthogonalize8(vec2 v); void vec2_orthogonalize8(vec2 v);
///////////////////////////// /////////////////////////////
// Intersec_RayUnitCircle // Intersec_RayUnitCircle
// //
// Intersection between a ray and a Unit Circle. // Intersection between a ray and a Unit Circle.
int Intersec_RayUnitCircle(vec2 orig, vec2 vel, vec2 center, float *t); int Intersec_RayUnitCircle(vec2 orig, vec2 vel, vec2 center, float *t);
///////////////////////////// /////////////////////////////
// Intersect_CircleCircle // Intersect_CircleCircle
// //
// Colision point of a circle against another circle. // Colision point of a circle against another circle.
int Colision_CircleCircle( int Colision_CircleCircle(vec2 cir1, float ra, vec2 vel, vec2 cb, float rb,
vec2 cir1,float ra,vec2 vel,
vec2 cb,float rb,
float *t, vec2 n); float *t, vec2 n);
///////////////////////////// /////////////////////////////
// Intersect_RayEdge // Intersect_RayEdge
// //
// Intersection between a ray and a edge. // Intersection between a ray and a edge.
int Intersect_RayEdge( int Intersect_RayEdge(vec2 pos, vec2 vel, vec2 norm, vec2 edgePos, float len,
vec2 pos,vec2 vel,
vec2 norm,vec2 edgePos,float len,
float *t); float *t);
///////////////////////////// /////////////////////////////
// absmod // absmod
// //
int absmod(int v, int d); int absmod(int v, int d);
float fabsmod(float v, int d); float fabsmod(float v, int d);
///////////////////////////// /////////////////////////////
// IsBigEndian // IsBigEndian
// //
int IsBigEndian(); int IsBigEndian();
///////////////////////////// /////////////////////////////
// EndsWith // EndsWith
// //
int EndsWith(char *str, char *suffix); int EndsWith(char *str, char *suffix);
///////////////////////////// /////////////////////////////
// Rand // Rand
// //
@@ -88,12 +90,10 @@ void Rand_Seed(unsigned seed);
unsigned Rand_Get(); unsigned Rand_Get();
#define Rand_GetFloat(x) (((float)(Rand_Get() % 1048576)) / 1048576.0f) #define Rand_GetFloat(x) (((float)(Rand_Get() % 1048576)) / 1048576.0f)
///////////////////////////// /////////////////////////////
// Print // Print
// //
// Prints the formated text // Prints the formated text
int Print(char *fmt, ...); int Print(char *fmt, ...);
#endif #endif