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,139 +17,116 @@ Entity ent_Player;
Entity ent_Platform; Entity ent_Platform;
Entity ent_Block; Entity ent_Block;
int EntityApplyGravity(Entity e) {
float grav = 10.0f;
float vTerminal = 50.0f;
int EntityApplyGravity(Entity e){
float grav=10.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 || return (1);
0
))
{
return(1);
} }
// Apply gravity // Apply gravity
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) {
float acel = 8.0f;
float maxVel = 30.0f;
float jumpVel = 50.0f;
float shootVel = 50.0f;
if (Input_GetKey(InputKey_Jump) == InputKey_Pressed ||
Input_GetKey(InputKey_Up) == InputKey_Pressed) {
void player_proc(Entity e,int ft){
float acel=8.0f;
float maxVel=30.0f;
float jumpVel=50.0f;
float shootVel=50.0f;
if(Input_GetKey(InputKey_Jump)==InputKey_Pressed ||
Input_GetKey(InputKey_Up)==InputKey_Pressed)
{
vec2 jump; vec2 jump;
// Apply jump // Apply jump
if(e->vel[1]>(-jumpVel)){ if (e->vel[1] > (-jumpVel)) {
e->vel[1]=-jumpVel; e->vel[1] = -jumpVel;
} }
Entity_CalcBBox(e); Entity_CalcBBox(e);
// FIXME: play sound // FIXME: play sound
} }
if(Input_GetKey(InputKey_Left)){ if (Input_GetKey(InputKey_Left)) {
vec2 left; vec2 left;
// Apply left movement // Apply left movement
vec2_set(left,-acel,0.0f); vec2_set(left, -acel, 0.0f);
Entity_AddVelLimit(e,left,maxVel); Entity_AddVelLimit(e, left, maxVel);
e->A=0; e->A = 0;
} }
if(Input_GetKey(InputKey_Right)){ if (Input_GetKey(InputKey_Right)) {
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.
// //
img_player=Draw_LoadImage("data/player.png"); img_player = Draw_LoadImage("data/player.png");
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.
// //
ent_Player=Entity_New(); ent_Player = Entity_New();
ent_Player->type=Ent_Player; ent_Player->type = Ent_Player;
//ent_Player->flags=EntityFlag_Light; // ent_Player->flags=EntityFlag_Light;
//Entity_SetLight(ent_Player,.2,.2,.2,200); // Entity_SetLight(ent_Player,.2,.2,.2,200);
ent_Player->flags=EntityFlag_Collision|EntityFlag_Overlap; ent_Player->flags = EntityFlag_Collision | EntityFlag_Overlap;
ent_Player->zorder=0; ent_Player->zorder = 0;
AnimPlay_SetImg(&ent_Player->anim,img_player); AnimPlay_SetImg(&ent_Player->anim, img_player);
ent_Player->proc=player_proc; ent_Player->proc = player_proc;
ent_Player->mass=1.0f; ent_Player->mass = 1.0f;
ent_Player->radius=12; ent_Player->radius = 12;
ent_Player->width=24; ent_Player->width = 24;
ent_Player->height=24; ent_Player->height = 24;
ent_Player->fric_static=0.0f; ent_Player->fric_static = 0.0f;
ent_Player->fric_dynamic=0.2f; ent_Player->fric_dynamic = 0.2f;
ent_Platform=Entity_New(); ent_Platform = Entity_New();
ent_Platform->type=Ent_Platform; ent_Platform->type = Ent_Platform;
ent_Platform->flags=EntityFlag_PlatformCollision; ent_Platform->flags = EntityFlag_PlatformCollision;
ent_Platform->zorder=-1; ent_Platform->zorder = -1;
AnimPlay_SetImg(&ent_Platform->anim,img_platform); AnimPlay_SetImg(&ent_Platform->anim, img_platform);
ent_Platform->mass=0.0f; ent_Platform->mass = 0.0f;
ent_Platform->radius=12; ent_Platform->radius = 12;
ent_Platform->width=64; ent_Platform->width = 64;
ent_Platform->height=16; ent_Platform->height = 16;
ent_Platform->fric_static=0.0f; ent_Platform->fric_static = 0.0f;
ent_Platform->fric_dynamic=0.2f; ent_Platform->fric_dynamic = 0.2f;
ent_Block=Entity_New();
ent_Block->type=Ent_Block;
ent_Block->flags=EntityFlag_BlockCollision;
ent_Block->zorder=-1;
AnimPlay_SetImg(&ent_Block->anim,img_block);
ent_Block->mass=0.0f;
ent_Block->radius=32;
ent_Block->width=64;
ent_Block->height=64;
ent_Block->fric_static=0.0f;
ent_Block->fric_dynamic=0.2f;
ent_Block = Entity_New();
ent_Block->type = Ent_Block;
ent_Block->flags = EntityFlag_BlockCollision;
ent_Block->zorder = -1;
AnimPlay_SetImg(&ent_Block->anim, img_block);
ent_Block->mass = 0.0f;
ent_Block->radius = 32;
ent_Block->width = 64;
ent_Block->height = 64;
ent_Block->fric_static = 0.0f;
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,119 +10,108 @@
#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;
while(i<(max-1)){ while (i < (max - 1)) {
c=fgetc(f); c = fgetc(f);
if(c==EOF){ if (c == EOF) {
line[i]=0; line[i] = 0;
return(-1); return (-1);
} }
if(c=='\r'){ if (c == '\r') {
continue; continue;
} }
if(c=='\n'){ if (c == '\n') {
line[i]=0; line[i] = 0;
return(i); return (i);
} }
line[i]=c; line[i] = c;
i++; i++;
} }
line[i]=0; line[i] = 0;
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;
e=Entity_Copy(ent); e = Entity_Copy(ent);
vec2_set(pos,(res/2)+i*res,(res/2)+j*res); vec2_set(pos, (res / 2) + i * res, (res / 2) + j * res);
vec2_plus(e->pos,e->pos,pos); vec2_plus(e->pos, e->pos, pos);
Entity_CalcBBox(e); Entity_CalcBBox(e);
GameLib_AddEntity(e); GameLib_AddEntity(e);
return(e); return (e);
} }
#define MaxLineLen 1024 #define MaxLineLen 1024
int GameMap_LoadLevel(char *filename,int res){ int GameMap_LoadLevel(char *filename, int res) {
FILE *file; FILE *file;
char line[MaxLineLen]; char line[MaxLineLen];
int len,i,j; int len, i, j;
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) {
return(0); return (0);
} }
// Read the file to determine sizes // Read the file to determine sizes
width=0; width = 0;
height=0; height = 0;
do{ do {
len=ReadLine(file,line,MaxLineLen); len = ReadLine(file, line, MaxLineLen);
if(len>-1){ if (len > -1) {
if(len>height){ if (len > height) {
height=len; height = len;
} }
width++; width++;
} }
}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);
#define MAP(x,y) map[(x)+((y)*width)] #define MAP(x, y) map[(x) + ((y)*width)]
j=0; j = 0;
do{ do {
len=ReadLine(file,line,MaxLineLen); len = ReadLine(file, line, MaxLineLen);
for(i=0;i<len;i++){ for (i = 0; i < len; i++) {
MAP(j,(height-1)-i)=line[i]; MAP(j, (height - 1) - i) = line[i];
} }
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);
} }
if(MAP(i,j)=='#'){ if (MAP(i, j) == '#') {
// Block // Block
ent=GameMapAux_CreateEnt(ent_Block,i,j,res); ent = GameMapAux_CreateEnt(ent_Block, i, j, res);
} }
if(MAP(i,j)=='|'){ if (MAP(i, j) == '|') {
// 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
return(1); return (1);
} }

View File

@@ -2,7 +2,6 @@
#ifndef _GAMEMAP_H_ #ifndef _GAMEMAP_H_
#define _GAMEMAP_H_ #define _GAMEMAP_H_
int GameMap_LoadLevel(char *filename,int res); int GameMap_LoadLevel(char *filename, int res);
#endif #endif

View File

@@ -12,66 +12,60 @@ extern int gamelib_debug;
#include "GameEnts.h" #include "GameEnts.h"
#include "GameMap.h" #include "GameMap.h"
DrawFnt font; DrawFnt font;
DrawImg imgBackground; DrawImg imgBackground;
void MainGame_Text(int x, int y, char *text){ void MainGame_Text(int x, int y, char *text) {
Draw_SetColor(0.0f, 0.0f, 0.0f, 0.5f); Draw_SetColor(0.0f, 0.0f, 0.0f, 0.5f);
Draw_DrawText(font, text, x+1, y+1); Draw_DrawText(font, text, x + 1, y + 1);
Draw_SetColor(1.0f, 1.0f, 1.0f, 1.0f); Draw_SetColor(1.0f, 1.0f, 1.0f, 1.0f);
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;
Entity *e; Entity *e;
srand(time(NULL)); srand(time(NULL));
if (argc>1) { if (argc > 1) {
if (!strcmp(argv[1],"debug")) { if (!strcmp(argv[1], "debug")) {
gamelib_debug=1; gamelib_debug = 1;
printf("Debug Mode Activated!\n"); printf("Debug Mode Activated!\n");
} }
} }
GameLib_Init(640,480,"Game",20,60); GameLib_Init(640, 480, "Game", 20, 60);
///////////////////////////// /////////////////////////////
// Load and initialize media. // Load and initialize media.
// //
font=Draw_DefaultFont(255,255,255,255); font = Draw_DefaultFont(255, 255, 255, 255);
imgBackground=Draw_LoadImage("data/background.png"); imgBackground = Draw_LoadImage("data/background.png");
Draw_SetOffset(imgBackground,0,0); Draw_SetOffset(imgBackground, 0, 0);
GameEnts_Init(); GameEnts_Init();
///////////////////////// /////////////////////////
// Initialize world. // Initialize world.
// //
GameLib_DelEnts(); GameLib_DelEnts();
GameMap_LoadLevel("data/level_01.txt",64); GameMap_LoadLevel("data/level_01.txt", 64);
///////////////////////// /////////////////////////
// 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},
GameLib_Loop(ProcGame,PostProcGame,PreDrawGame,DrawGame); (int[2]){0, 0}, (float[2]){0.5f, 0.0f});
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,242 +19,226 @@ typedef struct {
int time; int time;
} Animation; } Animation;
///////////////////////////// /////////////////////////////
// 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) {
DrawImg img; DrawImg img;
Animation *anim; Animation *anim;
int w,h; int w, h;
img=Draw_LoadImage(fichero); img = Draw_LoadImage(fichero);
if(!img){ if (!img) {
return(NULL); return (NULL);
} }
Draw_GetSize(img,&w,&h); Draw_GetSize(img, &w, &h);
Draw_SetOffset(img,-(width/2),-(h/2)); Draw_SetOffset(img, -(width / 2), -(h / 2));
// Create the animation container // Create the animation container
anim=malloc(sizeof(Animation)); anim = malloc(sizeof(Animation));
anim->img=img; anim->img = img;
anim->w=width; anim->w = width;
if(width<=0){ if (width <= 0) {
anim->w=w/frames; anim->w = w / frames;
} }
anim->fps=fps; anim->fps = fps;
anim->frames=frames; anim->frames = frames;
anim->ftime=1000/fps; anim->ftime = 1000 / fps;
anim->time=anim->ftime*frames; anim->time = anim->ftime * frames;
return((Anim)anim); return ((Anim)anim);
} }
///////////////////////////// /////////////////////////////
// Anim_GetTime // Anim_GetTime
// //
// //
int Anim_GetTime(Anim a){ int Anim_GetTime(Anim a) {
Animation *anim=a; Animation *anim = a;
return(anim->time); return (anim->time);
} }
///////////////////////////// /////////////////////////////
// Anim_GetSize // Anim_GetSize
// //
// Gets the animation size. // Gets the animation size.
void Anim_GetSize(Anim a,int *w,int *h){ void Anim_GetSize(Anim a, int *w, int *h) {
Animation *anim=a; Animation *anim = a;
int waux; int waux;
*w=anim->w; *w = anim->w;
Draw_GetSize(anim->img,&waux,h); Draw_GetSize(anim->img, &waux, h);
} }
///////////////////////////// /////////////////////////////
// Anim_SetOffset // Anim_SetOffset
// Anim_GetOffset // Anim_GetOffset
// //
// //
void Anim_SetOffset(Anim a,int x,int y){ void Anim_SetOffset(Anim a, int x, int y) {
Animation *anim=a; Animation *anim = a;
Draw_SetOffset(anim->img,x,y); Draw_SetOffset(anim->img, x, y);
} }
void Anim_GetOffset(Anim a,int *x,int *y){ void Anim_GetOffset(Anim a, int *x, int *y) {
Animation *anim=a; Animation *anim = a;
Draw_GetOffset(anim->img,x,y); Draw_GetOffset(anim->img, x, y);
} }
///////////////////////////// /////////////////////////////
// Anim_SetFlip // Anim_SetFlip
// Anim_GetFlip // Anim_GetFlip
// //
// //
void Anim_SetFlip(Anim a,int flip){ void Anim_SetFlip(Anim a, int flip) {
Animation *anim=a; Animation *anim = a;
Draw_SetFlip(anim->img,flip); Draw_SetFlip(anim->img, flip);
} }
int Anim_GetFlip(Anim a){ int Anim_GetFlip(Anim a) {
Animation *anim=a; Animation *anim = a;
return Draw_GetFlip(anim->img); return Draw_GetFlip(anim->img);
} }
///////////////////////////// /////////////////////////////
// Anim_Draw // Anim_Draw
// //
// //
void Anim_Draw(Anim a,int time_ms,int x,int y){ void Anim_Draw(Anim a, int time_ms, int x, int y) {
Animation *anim=a; Animation *anim = a;
int frame; int frame;
frame=(time_ms/anim->ftime)%anim->frames; frame = (time_ms / anim->ftime) % anim->frames;
Draw_DrawImgPartHoriz(anim->img,x,y,anim->w,frame); Draw_DrawImgPartHoriz(anim->img, x, y, anim->w, frame);
} }
///////////////////////////// /////////////////////////////
// AnimPlay_Copy // AnimPlay_Copy
// //
// //
void AnimPlay_Copy(AnimPlay *ad,AnimPlay *ao){ void AnimPlay_Copy(AnimPlay *ad, AnimPlay *ao) {
ad->img=ao->img; ad->img = ao->img;
ad->imgPart=ao->imgPart; ad->imgPart = ao->imgPart;
ad->w=ao->w; ad->w = ao->w;
ad->h=ao->h; ad->h = ao->h;
ad->i=ao->i; ad->i = ao->i;
ad->j=ao->j; ad->j = ao->j;
ad->anim=ao->anim; ad->anim = ao->anim;
ad->time_ms=ao->time_ms; ad->time_ms = ao->time_ms;
} }
///////////////////////////// /////////////////////////////
// AnimPlay_SetImg // AnimPlay_SetImg
// AnimPlay_SetAnim // AnimPlay_SetAnim
// AnimPlay_SetImgPart // AnimPlay_SetImgPart
// //
// //
void AnimPlay_SetImg(AnimPlay *ap,DrawImg img){ void AnimPlay_SetImg(AnimPlay *ap, DrawImg img) {
ap->anim=NULL; ap->anim = NULL;
ap->time_ms=0; ap->time_ms = 0;
ap->img=img; ap->img = img;
ap->imgPart=NULL; ap->imgPart = NULL;
} }
void AnimPlay_SetAnim(AnimPlay *ap,Anim ani){ void AnimPlay_SetAnim(AnimPlay *ap, Anim ani) {
ap->pause=0; ap->pause = 0;
if(ap->anim==ani){ if (ap->anim == ani) {
return; return;
} }
ap->anim=ani; ap->anim = ani;
ap->time_ms=0; ap->time_ms = 0;
ap->img=NULL; ap->img = NULL;
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,
ap->anim=NULL; int j) {
ap->time_ms=0; ap->anim = NULL;
ap->time_ms = 0;
ap->img=NULL; ap->img = NULL;
ap->imgPart=img; ap->imgPart = img;
ap->w=w; ap->w = w;
ap->h=h; ap->h = h;
ap->i=i; ap->i = i;
ap->j=j; ap->j = j;
} }
///////////////////////////// /////////////////////////////
// AnimPlay_Draw // AnimPlay_Draw
// //
// //
void AnimPlay_Draw(AnimPlay *ani,int x,int y){ void AnimPlay_Draw(AnimPlay *ani, int x, int y) {
if(ani->anim){ if (ani->anim) {
Anim_Draw(ani->anim,ani->time_ms,x,y); Anim_Draw(ani->anim, ani->time_ms, x, y);
return; return;
} }
if(ani->img){ if (ani->img) {
Draw_DrawImg(ani->img,x,y); Draw_DrawImg(ani->img, x, y);
return; return;
} }
if(ani->imgPart){ if (ani->imgPart) {
Draw_DrawImgPart(ani->imgPart,x,y,ani->w,ani->h,ani->i,ani->j); Draw_DrawImgPart(ani->imgPart, x, y, ani->w, ani->h, ani->i, ani->j);
return; return;
} }
} }
///////////////////////////// /////////////////////////////
// AnimPlay_GetOffset // AnimPlay_GetOffset
// AnimPlay_GetSize // AnimPlay_GetSize
// //
// //
void AnimPlay_GetOffset(AnimPlay *ani,int *x,int *y){ void AnimPlay_GetOffset(AnimPlay *ani, int *x, int *y) {
if(ani->anim){ if (ani->anim) {
Anim_GetOffset(ani->anim,x,y); Anim_GetOffset(ani->anim, x, y);
return; return;
} }
if(ani->img){ if (ani->img) {
Draw_GetOffset(ani->img,x,y); Draw_GetOffset(ani->img, x, y);
return; return;
} }
if(ani->imgPart){ if (ani->imgPart) {
Draw_GetOffset(ani->imgPart,x,y); Draw_GetOffset(ani->imgPart, x, y);
return; return;
} }
} }
void AnimPlay_GetSize(AnimPlay *ani,int *w,int *h){ 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;
} }
if(ani->imgPart){ if (ani->imgPart) {
Draw_GetSize(ani->imgPart,w,h); Draw_GetSize(ani->imgPart, w, h);
return; return;
} }
} }
///////////////////////////// /////////////////////////////
// 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
// //
// //
void AnimPlay_IncTime(AnimPlay *ani,int t){ void AnimPlay_IncTime(AnimPlay *ani, int t) {
if(ani->anim){ if (ani->anim){
if(!ani->pause){ if (!ani->pause) {
ani->time_ms+=t; ani->time_ms += t;
} }
} }
} }

View File

@@ -5,20 +5,17 @@
#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
@@ -26,38 +23,33 @@ Anim Anim_LoadAnim(char *fichero,int width,int frames,float fps);
// //
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
// //
// //
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
// //
// //
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 //
@@ -71,17 +63,15 @@ typedef struct {
DrawImg img; DrawImg img;
DrawImg imgPart; DrawImg imgPart;
int w,h,i,j; int w, h, i, j;
} AnimPlay; } AnimPlay;
///////////////////////////// /////////////////////////////
// AnimPlay_Copy // AnimPlay_Copy
// //
// //
void AnimPlay_Copy(AnimPlay *ad,AnimPlay *ao); void AnimPlay_Copy(AnimPlay *ad, AnimPlay *ao);
///////////////////////////// /////////////////////////////
// AnimPlay_SetImg // AnimPlay_SetImg
@@ -89,38 +79,34 @@ void AnimPlay_Copy(AnimPlay *ad,AnimPlay *ao);
// AnimPlay_SetImgPart // AnimPlay_SetImgPart
// //
// //
void AnimPlay_SetImg(AnimPlay *ap,DrawImg img); 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
// //
// //
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
// //
// //
void AnimPlay_SetPause(AnimPlay *ani,int p); void AnimPlay_SetPause(AnimPlay *ani, int p);
///////////////////////////// /////////////////////////////
// AnimPlay_IncTime // AnimPlay_IncTime
// //
// //
void AnimPlay_IncTime(AnimPlay *ani,int t); void AnimPlay_IncTime(AnimPlay *ani, int t);
#endif #endif

View File

@@ -1,8 +1,8 @@
// Copyright (C) 2011 Valeriano Alfonso Rodriguez (Kableado) // Copyright (C) 2011 Valeriano Alfonso Rodriguez (Kableado)
#ifdef WIN32 #ifdef WIN32
#define _WIN32_WINNT 0x0501 #define _WIN32_WINNT 0x0501
#include <windows.h> #include <windows.h>
#endif #endif
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
@@ -12,8 +12,7 @@
#include "Audio.h" #include "Audio.h"
static void Audio_MixerCallback(void *ud,Uint8 *stream,int l); static void Audio_MixerCallback(void *ud, Uint8 *stream, int l);
//////////////////////////////////////////////// ////////////////////////////////////////////////
// AudioWave // // AudioWave //
@@ -30,8 +29,7 @@ struct TAudioWave {
AudioWave next; AudioWave next;
}; };
AudioWave _waves=NULL; AudioWave _waves = NULL;
//////////////////////////////////////////////// ////////////////////////////////////////////////
// AudioChan // // AudioChan //
@@ -45,29 +43,29 @@ struct TAudioChan {
unsigned char leftvol; unsigned char leftvol;
int loop; int loop;
AudioChan next; AudioChan next;
}; };
AudioChan _channels=NULL; AudioChan _channels = NULL;
AudioChan _free_channels=NULL; AudioChan _free_channels = NULL;
///////////////////////////// /////////////////////////////
// Audio_Init // Audio_Init
// //
// Initializes the game audio. // Initializes the game audio.
int Audio_Init(){ int Audio_Init() {
SDL_AudioSpec as; SDL_AudioSpec as;
SDL_AudioSpec as2; SDL_AudioSpec as2;
// Initialize audio subsistem // Initialize audio subsistem
#ifdef WIN32 #ifdef WIN32
// Force DSound Driver on win32 // Force DSound Driver on win32
putenv("SDL_AUDIODRIVER=dsound"); putenv("SDL_AUDIODRIVER=dsound");
#endif #endif
if(SDL_InitSubSystem(SDL_INIT_AUDIO) < 0){ if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) {
Print("Audio_Init: Failure initializing SDL Audio.\n"); Print("Audio_Init: Failure initializing SDL Audio.\n");
Print("\tSDL Error: %s\n",SDL_GetError()); Print("\tSDL Error: %s\n", SDL_GetError());
return(0); return (0);
} }
// Open the audio device using the desired parameters // Open the audio device using the desired parameters
@@ -76,155 +74,148 @@ int Audio_Init(){
as.channels = 2; as.channels = 2;
as.samples = 2048; as.samples = 2048;
as.callback = Audio_MixerCallback; as.callback = Audio_MixerCallback;
if(SDL_OpenAudio(&as, &as2) < 0){ if (SDL_OpenAudio(&as, &as2) < 0) {
Print("Audio_Init: Failure opening audio.\n"); Print("Audio_Init: Failure opening audio.\n");
Print("\tSDL Error: %s\n",SDL_GetError()); Print("\tSDL Error: %s\n", SDL_GetError());
return(0); return (0);
} }
// 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);
} }
// Unpause and ready to go // Unpause and ready to go
SDL_PauseAudio(0); SDL_PauseAudio(0);
return(1); return (1);
} }
///////////////////////////// /////////////////////////////
// Audio_MixerCallback // Audio_MixerCallback
// //
// Mixes the audio channels. // Mixes the audio channels.
static void Audio_MixerCallback(void *ud,Uint8 *stream,int l){ static void Audio_MixerCallback(void *ud, Uint8 *stream, int l) {
signed short *ptr_out,*ptr_wave; signed short *ptr_out, *ptr_wave;
AudioChan prevchan; AudioChan prevchan;
AudioChan chan; AudioChan chan;
AudioWave wave; AudioWave wave;
int len=l/4; // Asume 16bpb and 2 output chan int len = l / 4; // Asume 16bpb and 2 output chan
int chan_remain; int chan_remain;
int len_mix; int len_mix;
int i; int i;
// Clean // Clean
memset(stream,0,l); memset(stream, 0, l);
// Mix all the channels // Mix all the channels
prevchan=NULL; prevchan = NULL;
chan=_channels; chan = _channels;
while(chan){ while (chan) {
if(!chan->wave){ if (!chan->wave) {
// Remove finished channels // Remove finished channels
AudioChan aux_chan=chan->next; AudioChan aux_chan = chan->next;
chan->next=_free_channels; chan->next = _free_channels;
_free_channels=chan; _free_channels = chan;
chan=aux_chan; chan = aux_chan;
if(prevchan){ if (prevchan) {
prevchan->next=chan; prevchan->next = chan;
}else{ } else {
_channels=chan; _channels = chan;
} }
continue; continue;
} }
// Prepare the pointers // Prepare the pointers
ptr_out=(signed short *)stream; ptr_out = (signed short *)stream;
ptr_wave=((signed short *)chan->wave->buffer)+chan->pos; ptr_wave = ((signed short *)chan->wave->buffer) + chan->pos;
wave=chan->wave; wave = chan->wave;
// Determine mixing lenght // Determine mixing lenght
chan_remain=wave->len-chan->pos; chan_remain = wave->len - chan->pos;
if(chan_remain>len){ if (chan_remain > len) {
len_mix=len; len_mix = len;
}else{ } else {
if(chan->loop){ if (chan->loop) {
len_mix=len; len_mix = len;
}else{ } else {
len_mix=chan_remain; len_mix = chan_remain;
} }
chan->wave=NULL; chan->wave = NULL;
} }
// Mix the buffer // Mix the buffer
for(i=0;i<len_mix;i++){ for (i = 0; i < len_mix; i++) {
int temp; int temp;
// Left Channel // Left Channel
temp=ptr_out[0]; temp = ptr_out[0];
temp+=(ptr_wave[0]*chan->leftvol)>>8; temp += (ptr_wave[0] * chan->leftvol) >> 8;
if(temp>(1<<14)){ if (temp > (1 << 14)) {
ptr_out[0]=1<<14; ptr_out[0] = 1 << 14;
}else if(temp<-(1<<14)){ } else if (temp < -(1 << 14)) {
ptr_out[0]=-(1<<14); ptr_out[0] = -(1 << 14);
}else{ } else {
ptr_out[0]=temp; ptr_out[0] = temp;
} }
// Right Channel // Right Channel
temp=ptr_out[1]; temp = ptr_out[1];
temp+=(ptr_wave[0]*chan->rightvol)>>8; temp += (ptr_wave[0] * chan->rightvol) >> 8;
if(temp>(1<<14)){ if (temp > (1 << 14)) {
ptr_out[1]=1<<14; ptr_out[1] = 1 << 14;
}else if(temp<-(1<<14)){ } else if (temp < -(1 << 14)) {
ptr_out[1]=-(1<<14); ptr_out[1] = -(1 << 14);
}else{ } else {
ptr_out[1]=temp; ptr_out[1] = temp;
} }
// Next sample // Next sample
ptr_out+=2; ptr_out += 2;
if(ptr_wave>=(((signed short *)wave->buffer)+(wave->len-1))){ if (ptr_wave >=
ptr_wave=((signed short *)wave->buffer); (((signed short *)wave->buffer) + (wave->len - 1))) {
}else{ ptr_wave = ((signed short *)wave->buffer);
} else {
ptr_wave++; ptr_wave++;
} }
} }
chan->pos+=len_mix; chan->pos += len_mix;
if(chan->wave==NULL && chan->loop==1){ if (chan->wave == NULL && chan->loop == 1) {
chan->wave=wave; chan->wave = wave;
while(chan->pos>wave->len){ while (chan->pos > wave->len) {
chan->pos-=wave->len; chan->pos -= wave->len;
} }
} }
// Next channel // Next channel
prevchan=chan; prevchan = chan;
chan=chan->next; chan = chan->next;
} }
} }
///////////////////////////// /////////////////////////////
// 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
// //
// Loads a sound, giving a reference. // Loads a sound, giving a reference.
AudioSnd Audio_LoadSound(char *filename){ AudioSnd Audio_LoadSound(char *filename) {
FILE *f; FILE *f;
char id[5] = { 0, 0, 0, 0, 0 }, *sndBuffer = NULL; char id[5] = {0, 0, 0, 0, 0}, *sndBuffer = NULL;
short formatTag, channels, bitsPerSample; short formatTag, channels, bitsPerSample;
int formatLen, sampleRate, dataSize; int formatLen, sampleRate, dataSize;
f = fopen(filename, "rb"); f = fopen(filename, "rb");
if (!f) { if (!f) {
Print("Audio_LoadSound: Failure opening file.\n"); Print("Audio_LoadSound: Failure opening file.\n");
return(NULL); return (NULL);
} }
// Read id "RIFF" // Read id "RIFF"
@@ -232,7 +223,7 @@ AudioSnd Audio_LoadSound(char *filename){
if (strcmp(id, "RIFF")) { if (strcmp(id, "RIFF")) {
Print("Audio_LoadSound: File is not RIFF.\n"); Print("Audio_LoadSound: File is not RIFF.\n");
fclose(f); fclose(f);
return(NULL); return (NULL);
} }
// File size (-"RIFF") // File size (-"RIFF")
@@ -243,7 +234,7 @@ AudioSnd Audio_LoadSound(char *filename){
if (strcmp(id, "WAVE")) { if (strcmp(id, "WAVE")) {
Print("Audio_LoadSound: File is not WAVE.\n"); Print("Audio_LoadSound: File is not WAVE.\n");
fclose(f); fclose(f);
return(NULL); return (NULL);
} }
// Read the format // Read the format
@@ -252,13 +243,13 @@ AudioSnd Audio_LoadSound(char *filename){
if (formatLen < 14) { if (formatLen < 14) {
Print("Audio_LoadSound: File too short.\n"); Print("Audio_LoadSound: File too short.\n");
fclose(f); fclose(f);
return (NULL ); return (NULL);
} }
fread(&formatTag, 1, sizeof(short), f); // 1=PCM fread(&formatTag, 1, sizeof(short), f); // 1=PCM
if (formatTag != 1) { if (formatTag != 1) {
Print("Audio_LoadSound: Not PCM format.\n"); Print("Audio_LoadSound: Not PCM format.\n");
fclose(f); fclose(f);
return (NULL ); return (NULL);
} }
fread(&channels, 1, sizeof(short), f); fread(&channels, 1, sizeof(short), f);
fread(&sampleRate, 1, sizeof(int), f); fread(&sampleRate, 1, sizeof(int), f);
@@ -268,34 +259,36 @@ AudioSnd Audio_LoadSound(char *filename){
fseek(f, formatLen - 14, SEEK_CUR); // Align read fseek(f, formatLen - 14, SEEK_CUR); // Align read
// Assert sound format // Assert sound format
if (sampleRate!=44100 || channels!=1 || bitsPerSample!=2) { if (sampleRate != 44100 || channels != 1 || bitsPerSample != 2) {
Print("Audio_LoadSound: Format not supported: " Print("Audio_LoadSound: Format not supported: "
"sampleRate:%d; channels:%d; BPB:%d\n", "sampleRate:%d; channels:%d; BPB:%d\n",
sampleRate, channels, bitsPerSample); sampleRate, channels, bitsPerSample);
fclose(f); fclose(f);
return(NULL); return (NULL);
} }
// 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);
}else{ } else {
break; break;
} }
}while(1); } while (1);
if (strcmp(id, "data")) { if (strcmp(id, "data")) {
Print("Audio_LoadSound: DATA block not found\n"); Print("Audio_LoadSound: DATA block not found\n");
fclose(f); fclose(f);
return (NULL ); return (NULL);
} }
// Read the "data" block // Read the "data" block
fread(&dataSize, 1, sizeof(int), f); fread(&dataSize, 1, sizeof(int), f);
sndBuffer = malloc(sizeof(char)*dataSize); sndBuffer = malloc(sizeof(char) * dataSize);
fread(sndBuffer, dataSize, sizeof(char), f); fread(sndBuffer, dataSize, sizeof(char), f);
fclose(f); fclose(f);
@@ -304,69 +297,67 @@ AudioSnd Audio_LoadSound(char *filename){
AudioWave wave = malloc(sizeof(TAudioWave)); AudioWave wave = malloc(sizeof(TAudioWave));
wave->sampleRate = sampleRate; wave->sampleRate = sampleRate;
wave->channels = channels; wave->channels = channels;
wave->buffer = (Uint8 *) sndBuffer; wave->buffer = (Uint8 *)sndBuffer;
wave->BPB = bitsPerSample; wave->BPB = bitsPerSample;
wave->bpb = wave->BPB * 8; wave->bpb = wave->BPB * 8;
wave->len = dataSize / (wave->BPB * wave->channels); wave->len = dataSize / (wave->BPB * wave->channels);
// Take a reference // Take a reference
wave->next=_waves; wave->next = _waves;
_waves=wave; _waves = wave;
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) {
return(NULL); return (NULL);
} }
// Cast AudioSnd to AudioWave // Cast AudioSnd to AudioWave
wave=snd; wave = snd;
// Get a free channel // Get a free channel
if(_free_channels){ if (_free_channels) {
chan=_free_channels; chan = _free_channels;
_free_channels=chan->next; _free_channels = chan->next;
chan->next=NULL; chan->next = NULL;
}else{ } else {
chan=malloc(sizeof(TAudioChan)); chan = malloc(sizeof(TAudioChan));
chan->next=NULL; chan->next = NULL;
} }
// Initialize the channel // Initialize the channel
chan->wave=wave; chan->wave = wave;
chan->pos=0; chan->pos = 0;
chan->rightvol=(rightvol*255); chan->rightvol = (rightvol * 255);
chan->leftvol=(leftvol*255); chan->leftvol = (leftvol * 255);
chan->loop=loop; chan->loop = loop;
// Include in sounds list // Include in sounds list
chan->next=_channels; chan->next = _channels;
_channels=chan; _channels = chan;
return chan; return chan;
} }
///////////////////////////// /////////////////////////////
// Audio_StopChan // Audio_StopChan
// //
// Stops an audio chanel // Stops an audio chanel
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) {
chan->loop=0; return;
chan->wave=NULL; }
chan->loop = 0;
chan->wave = NULL;
} }

View File

@@ -3,55 +3,46 @@
#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
// //
// Stops an audio chanel // Stops an audio chanel
void Audio_StopChan(AudioChn chan); void Audio_StopChan(AudioChn chan);
#endif #endif

File diff suppressed because it is too large Load Diff

View File

@@ -3,33 +3,24 @@
#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 *data);
void (*draw)(void *data,float f),
void *data);
///////////////////////////// /////////////////////////////
// Draw_BreakLoop // Draw_BreakLoop
@@ -37,33 +28,28 @@ void Draw_Loop(
// 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
@@ -71,73 +57,64 @@ DrawImg Draw_CreateImage(int w,int h);
// 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
// //
// Sets and Gets the image offset. // Sets and Gets the image offset.
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
// //
// //
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
// //
// //
void Draw_SetColor(float r,float g,float b,float a); void Draw_SetColor(float r, float g, float b, float a);
//////////////////////////////////////////////// ////////////////////////////////////////////////
// DrawFnt // // DrawFnt //
@@ -145,31 +122,24 @@ void Draw_SetColor(float r,float g,float b,float a);
// 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 a);
unsigned char g,
unsigned char b,
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
@@ -177,5 +147,4 @@ void Draw_DrawText(DrawFnt f,char *text,int x,int y);
// //
void Draw_SaveScreenshoot(char *filename); void Draw_SaveScreenshoot(char *filename);
#endif #endif

File diff suppressed because it is too large Load Diff

View File

@@ -7,7 +7,6 @@
#include "Draw.h" #include "Draw.h"
#include "Anim.h" #include "Anim.h"
//////////////////////////////////////////////// ////////////////////////////////////////////////
// Entity // Entity
// //
@@ -60,9 +59,9 @@ struct TEntity {
void (*oncopy)(Entity ent); void (*oncopy)(Entity ent);
void (*oninit)(Entity ent); void (*oninit)(Entity ent);
void (*ondelete)(Entity ent); void (*ondelete)(Entity ent);
void (*proc)(Entity ent,int ft); void (*proc)(Entity ent, int ft);
void (*postproc)(Entity ent,int ft); void (*postproc)(Entity ent, int ft);
int (*collision)(Entity ent, Entity ent2, float t,vec2 n); int (*collision)(Entity ent, Entity ent2, float t, vec2 n);
void (*overlap)(Entity ent, Entity ent2); void (*overlap)(Entity ent, Entity ent2);
int A; int A;
@@ -71,81 +70,70 @@ struct TEntity {
int D; int D;
Entity child; Entity child;
float maxX,minX; float maxX, minX;
float maxY,minY; float maxY, minY;
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
// //
#define CollisionResponse_Circle 1 #define CollisionResponse_Circle 1
#define CollisionResponse_Line 2 #define CollisionResponse_Line 2
typedef struct TCollisionInfo TCollisionInfo,*CollisionInfo; typedef struct TCollisionInfo TCollisionInfo, *CollisionInfo;
struct TCollisionInfo { struct TCollisionInfo {
int responseType; int responseType;
Entity ent1; Entity ent1;
@@ -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,44 +158,38 @@ 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
// //
// //
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

@@ -17,16 +17,16 @@
#include "GameLib.h" #include "GameLib.h"
// Globals // Globals
Entity *_entity=NULL; Entity *_entity = NULL;
int *_entity_flag=NULL; int *_entity_flag = NULL;
int _n_entities=0; int _n_entities = 0;
int _n_entities_res=0; int _n_entities_res = 0;
int _entities_lock=0; int _entities_lock = 0;
int _entities_compactate=0; int _entities_compactate = 0;
void (*_gameproc)()=NULL; void (*_gameproc)() = NULL;
void (*_gamepostproc)()=NULL; void (*_gamepostproc)() = NULL;
void (*_gamepredraw)(float f)=NULL; void (*_gamepredraw)(float f) = NULL;
void (*_gamedraw)(float f)=NULL; void (*_gamedraw)(float f) = NULL;
int _pft; int _pft;
int _game_size[2]; int _game_size[2];
int _game_pos0[2]; int _game_pos0[2];
@@ -49,421 +49,400 @@ struct TParallaxBackground {
}; };
#define MaxParallaxBackgrounds 10 #define MaxParallaxBackgrounds 10
TParallaxBackground _parallaxBackground[MaxParallaxBackgrounds]; TParallaxBackground _parallaxBackground[MaxParallaxBackgrounds];
int _nParallaxBackgrounds=0; int _nParallaxBackgrounds = 0;
int gamelib_debug=0;
int gamelib_debug = 0;
///////////////////////////// /////////////////////////////
// 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) {
if(!Draw_Init(w,h,title,pfps,fps)){ if (!Draw_Init(w, h, title, pfps, fps)) {
return(0); return (0);
} }
if(!Input_Init()){ if (!Input_Init()) {
return(0); return (0);
} }
Audio_Init(); Audio_Init();
_game_size[0]=w; _game_size[0] = w;
_game_size[1]=h; _game_size[1] = h;
_game_pos0[0]=0; _game_pos0[0] = 0;
_game_pos0[1]=0; _game_pos0[1] = 0;
_game_pos1[0]=0; _game_pos1[0] = 0;
_game_pos1[1]=0; _game_pos1[1] = 0;
_pft=1000/pfps; _pft = 1000 / pfps;
return(1); return (1);
} }
///////////////////////////// /////////////////////////////
// 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) {
if(_n_entities>=_n_entities_res){ if (_n_entities >= _n_entities_res) {
Entity *entity_aux; Entity *entity_aux;
int *entity_flag_aux; int *entity_flag_aux;
int i; int i;
// Grow the array // Grow the array
if(_n_entities_res==0) if (_n_entities_res == 0)
_n_entities_res=32; _n_entities_res = 32;
else else
_n_entities_res*=2; _n_entities_res *= 2;
entity_aux=malloc(sizeof(Entity)*_n_entities_res); entity_aux = malloc(sizeof(Entity) * _n_entities_res);
entity_flag_aux=malloc(sizeof(int)*_n_entities_res); entity_flag_aux = malloc(sizeof(int) * _n_entities_res);
for(i=0;i<_n_entities;i++){ for (i = 0; i < _n_entities; i++) {
entity_aux[i]=_entity[i]; entity_aux[i] = _entity[i];
entity_flag_aux[i]=_entity_flag[i]; entity_flag_aux[i] = _entity_flag[i];
} }
if(_entity){ if (_entity) {
free(_entity); free(_entity);
free(_entity_flag); free(_entity_flag);
} }
_entity=entity_aux; _entity = entity_aux;
_entity_flag=entity_flag_aux; _entity_flag = entity_flag_aux;
} }
// Add the entity // Add the entity
_entity[_n_entities]=e; _entity[_n_entities] = e;
_entity_flag[_n_entities]=1; _entity_flag[_n_entities] = 1;
_n_entities++; _n_entities++;
// Mark for light update // Mark for light update
Entity_MarkUpdateLight(e,_entity,_n_entities); Entity_MarkUpdateLight(e, _entity, _n_entities);
Entity_CalcBBox(e); Entity_CalcBBox(e);
Entity_Init(e); Entity_Init(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) {
int i; int i;
for(i=0;i<_n_entities;i++){ for (i = 0; i < _n_entities; i++) {
if(e==_entity[i]){ if (e == _entity[i]) {
// Mark or unref // Mark or unref
if(_entities_lock){ if (_entities_lock) {
_entity_flag[i]=-2; _entity_flag[i] = -2;
}else{ } else {
_entity[i]=NULL; _entity[i] = NULL;
_entity_flag[i]=0; _entity_flag[i] = 0;
} }
_entities_compactate=1; _entities_compactate = 1;
// Mark for light update // Mark for light update
Entity_MarkUpdateLight(e,_entity,_n_entities); Entity_MarkUpdateLight(e, _entity, _n_entities);
return(i); return (i);
} }
} }
return(-1); return (-1);
} }
///////////////////////////// /////////////////////////////
// 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) {
int i; int i;
if((i=GameLib_UnrefEntity(e))==-1){ if ((i = GameLib_UnrefEntity(e)) == -1) {
return(0); return (0);
} }
if(_entities_lock){ if (_entities_lock) {
// Delete latter // Delete latter
_entity[i]=e; _entity[i] = e;
_entity_flag[i]=-1; _entity_flag[i] = -1;
}else{ } else {
// Delete now // Delete now
Entity_Destroy(e); Entity_Destroy(e);
} }
return(1); return (1);
} }
///////////////////////////// /////////////////////////////
// GameLib_Compactate // GameLib_Compactate
// //
// //
void GameLib_Compactate(){ void GameLib_Compactate() {
int i,j; int i, j;
j=0; j = 0;
if(!_entities_compactate) if (!_entities_compactate)
return; return;
for(i=0;i<_n_entities;i++){ for (i = 0; i < _n_entities; i++) {
if(!_entity[i] || _entity_flag[i]==-2) if (!_entity[i] || _entity_flag[i] == -2)
continue; continue;
if(_entity_flag[i]==-1){ if (_entity_flag[i] == -1) {
Entity_Destroy(_entity[i]); Entity_Destroy(_entity[i]);
continue; continue;
} }
if(i>j){ if (i > j) {
_entity[j]=_entity[i]; _entity[j] = _entity[i];
_entity_flag[j]=_entity_flag[i]; _entity_flag[j] = _entity_flag[i];
} }
j++; j++;
} }
_n_entities=j; _n_entities = j;
_entities_compactate=0; _entities_compactate = 0;
} }
///////////////////////////// /////////////////////////////
// GameLib_ProcLoop // GameLib_ProcLoop
// //
// Process the loop. // Process the loop.
void GameLib_ProcLoop(void *data){ void GameLib_ProcLoop(void *data) {
int i,j; int i, j;
int repeat,count; int repeat, count;
long long time; long long time;
// Step the gamePosition // Step the gamePosition
_game_pos0[0]=_game_pos1[0]; _game_pos0[0] = _game_pos1[0];
_game_pos0[1]=_game_pos1[1]; _game_pos0[1] = _game_pos1[1];
// Process // Process
time=Time_GetTime(); time = Time_GetTime();
_entities_lock=1; _entities_lock = 1;
if(_gameproc){ if (_gameproc) {
_gameproc(); _gameproc();
} }
for(i=0;i<_n_entities;i++){ for (i = 0; i < _n_entities; i++) {
if(!_entity[i]) if (!_entity[i])
continue; continue;
Entity_Process(_entity[i],_pft); Entity_Process(_entity[i], _pft);
} }
GameLib_Compactate(); GameLib_Compactate();
_entities_lock=0; _entities_lock = 0;
t_proc+=Time_GetTime()-time; t_proc += Time_GetTime() - time;
// Colisions between entities // Colisions between entities
time=Time_GetTime(); time = Time_GetTime();
_entities_lock=1; _entities_lock = 1;
count=0; count = 0;
do{ do {
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);
} }
} }
if(Entity_CollisionInfoResponse(collInfo)){ if (Entity_CollisionInfoResponse(collInfo)) {
repeat=1; repeat = 1;
} }
CollisionInfo_Destroy(&collInfo); CollisionInfo_Destroy(&collInfo);
count++; count++;
}while(repeat && count<50); } while (repeat && count < 50);
// 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)) {
vec2_set(_entity[i]->vel,0,0); vec2_set(_entity[i]->vel, 0, 0);
Entity_CalcBBox(_entity[i]); Entity_CalcBBox(_entity[i]);
vec2_set(_entity[j]->vel,0,0); vec2_set(_entity[j]->vel, 0, 0);
Entity_CalcBBox(_entity[j]); Entity_CalcBBox(_entity[j]);
} }
} }
} }
} }
GameLib_Compactate(); GameLib_Compactate();
_entities_lock=0; _entities_lock = 0;
t_col+=Time_GetTime()-time; t_col += Time_GetTime() - time;
// Process Overlaps // Process Overlaps
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)
continue; continue;
Entity_Overlaps(_entity[i],_entity[j]); Entity_Overlaps(_entity[i], _entity[j]);
} }
} }
GameLib_Compactate(); GameLib_Compactate();
_entities_lock=0; _entities_lock = 0;
t_over+=Time_GetTime()-time; t_over += Time_GetTime() - time;
// Sort // Sort
int n,n2,swap; int n, n2, swap;
n=_n_entities; n = _n_entities;
do{ do {
n2=0; n2 = 0;
for(i=1;i<n;i++){ for (i = 1; i < n; i++) {
Entity ent1=_entity[i-1]; Entity ent1 = _entity[i - 1];
Entity ent2=_entity[i]; Entity ent2 = _entity[i];
swap=0; swap = 0;
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
float y1=ent1->pos[1]+ent1->sortYOffset; float y1 = ent1->pos[1] + ent1->sortYOffset;
float y2=ent2->pos[1]+ent2->sortYOffset; float y2 = ent2->pos[1] + ent2->sortYOffset;
if(y1 > y2){ if (y1 > y2) {
swap=1; swap = 1;
} }
} }
if(swap){ if (swap) {
Entity ent; Entity ent;
ent=_entity[i]; ent = _entity[i];
_entity[i]=_entity[i-1]; _entity[i] = _entity[i - 1];
_entity[i-1]=ent; _entity[i - 1] = ent;
n2=i; n2 = i;
} }
} }
n=n2; n = n2;
}while(n>0); } while (n > 0);
// PostProcess // PostProcess
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++) {
Entity_PostProcess(_entity[i],_pft); Entity_PostProcess(_entity[i], _pft);
if(Entity_IsMoving(_entity[i])){ if (Entity_IsMoving(_entity[i])) {
Entity_MarkUpdateLight(_entity[i],_entity,_n_entities); Entity_MarkUpdateLight(_entity[i], _entity, _n_entities);
} }
} }
if(_gamepostproc){ if (_gamepostproc) {
_gamepostproc(); _gamepostproc();
} }
GameLib_Compactate(); GameLib_Compactate();
_entities_lock=0; _entities_lock = 0;
t_postproc+=Time_GetTime()-time; t_postproc += Time_GetTime() - time;
fproc_count++; fproc_count++;
} }
///////////////////////////// /////////////////////////////
// GameLib_DrawLoop // GameLib_DrawLoop
// //
// //
void GameLib_DrawLoop(void *data, float f){ void GameLib_DrawLoop(void *data, float f) {
long long time; long long time;
int i; int i;
int game_pos[2]; int game_pos[2];
GameLib_GetPosInstant(game_pos,f); GameLib_GetPosInstant(game_pos, f);
time=Time_GetTime(); time = Time_GetTime();
// PreDraw // PreDraw
if(_gamepredraw){ if (_gamepredraw) {
_gamepredraw(f); _gamepredraw(f);
}else{ } else {
if(_nParallaxBackgrounds==0){ if (_nParallaxBackgrounds == 0) {
// Clean screen // Clean screen
Draw_Clean(0,0,0); Draw_Clean(0, 0, 0);
} }
} }
// 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
GameLib_Compactate(); GameLib_Compactate();
for(i=0;i<_n_entities;i++){ for (i = 0; i < _n_entities; i++) {
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;
} }
// Update ilumination of this entity // Update ilumination of this entity
if(Entity_IsUpdateLight(e)){ if (Entity_IsUpdateLight(e)) {
Entity_Iluminate(e,_entity,_n_entities); Entity_Iluminate(e, _entity, _n_entities);
} }
Entity_Draw(e,-game_pos[0],-game_pos[1],f); Entity_Draw(e, -game_pos[0], -game_pos[1], f);
} }
Draw_SetColor(1,1,1,1); Draw_SetColor(1, 1, 1, 1);
_entities_lock=1; _entities_lock = 1;
if(_gamedraw){ if (_gamedraw) {
_gamedraw(f); _gamedraw(f);
} }
GameLib_Compactate(); GameLib_Compactate();
_entities_lock=0; _entities_lock = 0;
t_draw+=Time_GetTime()-time; t_draw += Time_GetTime() - time;
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);
Print("t_over.....:%6lld\n",t_over/fproc_count); Print("t_over.....:%6lld\n", t_over / fproc_count);
Print("t_postproc.:%6lld\n",t_postproc/fproc_count); Print("t_postproc.:%6lld\n", t_postproc / fproc_count);
Print("t_draw.....:%6lld\n",t_draw/fdraw_count); Print("t_draw.....:%6lld\n", t_draw / fdraw_count);
t_proc=0; t_proc = 0;
t_col=0; t_col = 0;
t_over=0; t_over = 0;
t_postproc=0; t_postproc = 0;
t_draw=0; t_draw = 0;
fproc_count=0; fproc_count = 0;
fdraw_count=0; fdraw_count = 0;
} }
} }
///////////////////////////// /////////////////////////////
// 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)(), _gameproc = gameproc;
void (*gamepredraw)(float f), _gamepostproc = gamepostproc;
void (*gamedraw)(float f)) _gamepredraw = gamepredraw;
{ _gamedraw = gamedraw;
_gameproc=gameproc; t_proc = 0;
_gamepostproc=gamepostproc; t_col = 0;
_gamepredraw=gamepredraw; t_over = 0;
_gamedraw=gamedraw; t_postproc = 0;
t_proc=0; t_draw = 0;
t_col=0; fproc_count = 0;
t_over=0; fdraw_count = 0;
t_postproc=0; Draw_Loop(GameLib_ProcLoop, GameLib_DrawLoop, NULL);
t_draw=0;
fproc_count=0;
fdraw_count=0;
Draw_Loop(GameLib_ProcLoop,GameLib_DrawLoop,NULL);
} }
///////////////////////////// /////////////////////////////
// GameLib_GetPos // GameLib_GetPos
// GameLib_SetPos // GameLib_SetPos
@@ -472,248 +451,230 @@ void GameLib_Loop(
// GameLib_GetPosInstant // GameLib_GetPosInstant
// //
// //
void GameLib_GetPos(int pos[2]){ void GameLib_GetPos(int pos[2]) {
pos[0]=_game_pos1[0]; pos[0] = _game_pos1[0];
pos[1]=_game_pos1[1]; pos[1] = _game_pos1[1];
} }
void GameLib_SetPos(int pos[2]){ void GameLib_SetPos(int pos[2]) {
_game_pos0[0]=pos[0]; _game_pos0[0] = pos[0];
_game_pos0[1]=pos[1]; _game_pos0[1] = pos[1];
_game_pos1[0]=pos[0]; _game_pos1[0] = pos[0];
_game_pos1[1]=pos[1]; _game_pos1[1] = pos[1];
} }
void GameLib_UpdatePos(int pos[2]){ void GameLib_UpdatePos(int pos[2]) {
_game_pos1[0]=pos[0]; _game_pos1[0] = pos[0];
_game_pos1[1]=pos[1]; _game_pos1[1] = pos[1];
} }
void GameLib_GetSize(int size[2]){ void GameLib_GetSize(int size[2]) {
size[0]=_game_size[0]; size[0] = _game_size[0];
size[1]=_game_size[1]; size[1] = _game_size[1];
} }
void GameLib_GetPosInstant(int pos[2],float f){ void GameLib_GetPosInstant(int pos[2], float f) {
pos[0]=_game_pos0[0]+f*(_game_pos1[0]-_game_pos0[0]); pos[0] = _game_pos0[0] + f * (_game_pos1[0] - _game_pos0[0]);
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
// GameLib_MoveToPosV // GameLib_MoveToPosV
// //
// //
void GameLib_MoveToPos(vec2 pos,float f){ void GameLib_MoveToPos(vec2 pos, float f) {
GameLib_MoveToPosH(pos,f); GameLib_MoveToPosH(pos, 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
// //
// Deletes every entity. // Deletes every entity.
void GameLib_DelEnts(){ void GameLib_DelEnts() {
int i; int i;
for(i=0;i<_n_entities;i++){ for (i = 0; i < _n_entities; i++) {
if(!_entity[i]) if (!_entity[i])
continue; continue;
Entity_Destroy(_entity[i]); Entity_Destroy(_entity[i]);
} }
_n_entities=0; _n_entities = 0;
} }
///////////////////////////// /////////////////////////////
// GameLib_ForEachEn // GameLib_ForEachEn
// //
// Iterates every entity. // Iterates every entity.
void GameLib_ForEachEnt(int (*func)(Entity ent)){ void GameLib_ForEachEnt(int (*func)(Entity ent)) {
int i; int i;
for(i=0;i<_n_entities;i++){ for (i = 0; i < _n_entities; i++) {
if(!_entity[i]) if (!_entity[i])
continue; continue;
if(!func(_entity[i])){ if (!func(_entity[i])) {
break; break;
} }
} }
} }
///////////////////////////// /////////////////////////////
// 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) {
int i; int i;
Entity ent=NULL; Entity ent = NULL;
for(i=0;i<_n_entities;i++){ for (i = 0; i < _n_entities; i++) {
if(!_entity[i]) if (!_entity[i])
continue; continue;
if(func(_entity[i],d)){ if (func(_entity[i], d)) {
ent=_entity[i]; ent = _entity[i];
break; break;
} }
} }
return ent; return ent;
} }
///////////////////////////// /////////////////////////////
// GameLib_EntityCustomCheckCollision // GameLib_EntityCustomCheckCollision
// //
// //
int GameLib_EntityCustomCheckCollision(Entity ent,vec2 vel){ int GameLib_EntityCustomCheckCollision(Entity ent, vec2 vel) {
int collision=0; int collision = 0;
CollisionInfo collInfo=NULL; CollisionInfo collInfo = NULL;
vec2 originalVel; vec2 originalVel;
int j; int j;
vec2_copy(originalVel,ent->vel); vec2_copy(originalVel, ent->vel);
vec2_copy(ent->vel,vel); vec2_copy(ent->vel, vel);
Entity_CalcBBox(ent); Entity_CalcBBox(ent);
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);
if(collInfo!=NULL){ if (collInfo != NULL) {
collision=1; collision = 1;
break; break;
} }
} }
vec2_copy(ent->vel,originalVel); vec2_copy(ent->vel, originalVel);
Entity_CalcBBox(ent); Entity_CalcBBox(ent);
CollisionInfo_Destroy(&collInfo); CollisionInfo_Destroy(&collInfo);
return collision; return collision;
} }
///////////////////////////// /////////////////////////////
// GameLib_PlaySound // GameLib_PlaySound
// //
// //
void GameLib_PlaySound(AudioSnd snd,int x,int y){ void GameLib_PlaySound(AudioSnd snd, int x, int y) {
float vleft,vright,dx,dy; float vleft, vright, dx, dy;
int r,cx,cy,off; int r, cx, cy, off;
// Get the screen context // Get the screen context
cx=_game_pos1[0]+_game_size[0]/2; cx = _game_pos1[0] + _game_size[0] / 2;
cy=_game_pos1[1]+_game_size[1]/2; cy = _game_pos1[1] + _game_size[1] / 2;
if(_game_size[0]>_game_size[1]){ if (_game_size[0] > _game_size[1]) {
r=_game_size[0]/2; r = _game_size[0] / 2;
}else{ } else {
r=_game_size[1]/2; r = _game_size[1] / 2;
} }
r=r*1.2f; r = r * 1.2f;
off=r/10.0f; off = r / 10.0f;
// Calculate volumes // Calculate volumes
dx=x-(cx+off); dx = x - (cx + off);
dy=y-(cy); dy = y - (cy);
vright=1.0f-(sqrtf(dx*dx+dy*dy)/(float)r); vright = 1.0f - (sqrtf(dx * dx + dy * dy) / (float)r);
dx=x-(cx-off); dx = x - (cx - off);
dy=y-(cy); dy = y - (cy);
vleft=1.0f-(sqrtf(dx*dx+dy*dy)/(float)r); vleft = 1.0f - (sqrtf(dx * dx + dy * dy) / (float)r);
// Clamp to 0 // Clamp to 0
if(vleft<0.0f) if (vleft < 0.0f)
vleft=0.0f; vleft = 0.0f;
if(vright<0.0f) if (vright < 0.0f)
vright=0.0f; vright = 0.0f;
if(vleft<=0.0f && vright<=0.0f){ if (vleft <= 0.0f && vright <= 0.0f) {
return; return;
} }
// PLAY! // PLAY!
Audio_PlaySound(snd,vleft,vright,0); Audio_PlaySound(snd, vleft, vright, 0);
} }
///////////////////////////// /////////////////////////////
// GameLib_PlayLoopingSound // GameLib_PlayLoopingSound
// //
// //
AudioChn GameLib_PlayLoopingSound(AudioSnd snd){ AudioChn GameLib_PlayLoopingSound(AudioSnd snd) {
// PLAY! // PLAY!
return Audio_PlaySound(snd,1.0f,1.0f,1); return Audio_PlaySound(snd, 1.0f, 1.0f, 1);
} }
///////////////////////////// /////////////////////////////
// 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) {
if(Entity_IsLight(e)){ if (Entity_IsLight(e)) {
Entity_MarkUpdateLight(e,_entity,_n_entities); Entity_MarkUpdateLight(e, _entity, _n_entities);
Entity_SetLight(e,r,g,b,rad); Entity_SetLight(e, r, g, b, rad);
Entity_MarkUpdateLight(e,_entity,_n_entities); Entity_MarkUpdateLight(e, _entity, _n_entities);
}else{ } else {
Entity_SetLight(e,r,g,b,rad); Entity_SetLight(e, r, g, b, 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];
game_pos[0]=_game_pos0[0]+f*(_game_pos1[0]-_game_pos0[0]); game_pos[0] = _game_pos0[0] + f * (_game_pos1[0] - _game_pos0[0]);
game_pos[1]=_game_pos0[1]+f*(_game_pos1[1]-_game_pos0[1]); game_pos[1] = _game_pos0[1] + f * (_game_pos1[1] - _game_pos0[1]);
gamePos[0]=(screenPos[0]*_game_size[0])+game_pos[0]; gamePos[0] = (screenPos[0] * _game_size[0]) + game_pos[0];
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.");
return; return;
} }
_parallaxBackground[idx].img=img; _parallaxBackground[idx].img = img;
_parallaxBackground[idx].imgSize[0]=imgSize[0]; _parallaxBackground[idx].imgSize[0] = imgSize[0];
_parallaxBackground[idx].imgSize[1]=imgSize[1]; _parallaxBackground[idx].imgSize[1] = imgSize[1];
_parallaxBackground[idx].imgOffset[0]=imgOffset[0]; _parallaxBackground[idx].imgOffset[0] = imgOffset[0];
_parallaxBackground[idx].imgOffset[1]=imgOffset[1]; _parallaxBackground[idx].imgOffset[1] = imgOffset[1];
_parallaxBackground[idx].parallaxFactor[0]=parallaxFactor[0]; _parallaxBackground[idx].parallaxFactor[0] = parallaxFactor[0];
_parallaxBackground[idx].parallaxFactor[1]=parallaxFactor[1]; _parallaxBackground[idx].parallaxFactor[1] = parallaxFactor[1];
_nParallaxBackgrounds++; _nParallaxBackgrounds++;
} }
///////////////////////////// /////////////////////////////
// GameLib_CleanParallaxBackgrounds // GameLib_CleanParallaxBackgrounds
// //
// //
void GameLib_CleanParallaxBackgrounds(){ void GameLib_CleanParallaxBackgrounds() { _nParallaxBackgrounds = 0; }
_nParallaxBackgrounds=0;
}

View File

@@ -11,13 +11,11 @@
#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
@@ -25,31 +23,24 @@ int GameLib_Init(int w,int h,char *title,int pfps,int fps);
// 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
@@ -63,8 +54,7 @@ void GameLib_GetPos(int pos[2]);
void GameLib_SetPos(int pos[2]); void GameLib_SetPos(int pos[2]);
void GameLib_UpdatePos(int pos[2]); 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
@@ -72,10 +62,9 @@ void GameLib_GetPosInstant(int pos[2],float f);
// GameLib_MoveToPosV // GameLib_MoveToPosV
// //
// //
void GameLib_MoveToPos(vec2 pos,float f); 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
@@ -83,34 +72,29 @@ void GameLib_MoveToPosV(vec2 pos,float f);
// 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
@@ -118,28 +102,24 @@ void GameLib_PlaySound(AudioSnd snd,int x,int y);
// 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,194 +9,180 @@
#include "Util.h" #include "Util.h"
#include "Input.h" #include "Input.h"
// Globals // Globals
InputKeyStatus _keys[InputKey_Max]; InputKeyStatus _keys[InputKey_Max];
int _pointerDown=0; int _pointerDown = 0;
float _pointerX=0; float _pointerX = 0;
float _pointerY=0; float _pointerY = 0;
int _clicked=0;
float _clickedPositionX=0;
float _clickedPositionY=0;
int _clicked = 0;
float _clickedPositionX = 0;
float _clickedPositionY = 0;
///////////////////////////// /////////////////////////////
// Input_Init // Input_Init
// //
// Initializes the game input. // Initializes the game input.
int Input_Init(){ int Input_Init() {
int i; int i;
// Mark released all the keys // Mark released all the keys
for(i=0;i<InputKey_Max;i++){ for (i = 0; i < InputKey_Max; i++) {
_keys[i]=InputKey_Released; _keys[i] = InputKey_Released;
} }
return(1); return (1);
} }
///////////////////////////// /////////////////////////////
// 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() {
Uint8* keys; Uint8 *keys;
// Get keyboard state // Get keyboard state
keys=(Uint8 *)SDL_GetKeyState(NULL); keys = (Uint8 *)SDL_GetKeyState(NULL);
// Process Keys // Process Keys
Input_SetKey(InputKey_Action1,keys[SDLK_z]); Input_SetKey(InputKey_Action1, keys[SDLK_z]);
Input_SetKey(InputKey_Action2,keys[SDLK_x]); Input_SetKey(InputKey_Action2, keys[SDLK_x]);
Input_SetKey(InputKey_Up,keys[SDLK_UP]); Input_SetKey(InputKey_Up, keys[SDLK_UP]);
Input_SetKey(InputKey_Down,keys[SDLK_DOWN]); Input_SetKey(InputKey_Down, keys[SDLK_DOWN]);
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
// //
// Notify a frame update to the input subsystem. // Notify a frame update to the input subsystem.
void Input_PostFrame(){ void Input_PostFrame() {
Input_SetKey(InputKey_Exit,0); Input_SetKey(InputKey_Exit, 0);
_clicked=0; _clicked = 0;
} }
///////////////////////////// /////////////////////////////
// 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) {
if(!status){ if (!status) {
_keys[key]=InputKey_Released; _keys[key] = InputKey_Released;
}else{ } else {
if(_keys[key]>=InputKey_Pressed){ if (_keys[key] >= InputKey_Pressed) {
_keys[key]=InputKey_Holded; _keys[key] = InputKey_Holded;
}else{ } else {
_keys[key]=InputKey_Pressed; _keys[key] = InputKey_Pressed;
} }
} }
} }
///////////////////////////// /////////////////////////////
// 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
// //
void Input_SetPointerPosition(float x, float y){ void Input_SetPointerPosition(float x, float y) {
_pointerX=x; _pointerX = x;
_pointerY=y; _pointerY = y;
} }
///////////////////////////// /////////////////////////////
// Input_SetPointerDown // Input_SetPointerDown
// //
void Input_SetPointerDown(int pointerDown){ void Input_SetPointerDown(int pointerDown) {
if(pointerDown==0 && _pointerDown==1){ if (pointerDown == 0 && _pointerDown == 1) {
_clicked=1; _clicked = 1;
_clickedPositionX=_pointerX; _clickedPositionX = _pointerX;
_clickedPositionY=_pointerY; _clickedPositionY = _pointerY;
} }
_pointerDown=pointerDown; _pointerDown = pointerDown;
} }
///////////////////////////// /////////////////////////////
// Input_GetPointerPosition // Input_GetPointerPosition
// //
int Input_GetPointerPosition(vec2 pointer){ int Input_GetPointerPosition(vec2 pointer) {
pointer[0]=_pointerX; pointer[0] = _pointerX;
pointer[1]=_pointerY; pointer[1] = _pointerY;
return _pointerDown; return _pointerDown;
} }
///////////////////////////// /////////////////////////////
// Input_GetClickedPosition // Input_GetClickedPosition
// //
int Input_GetClickedPosition(vec2 clickPosition){ int Input_GetClickedPosition(vec2 clickPosition) {
clickPosition[0]=_clickedPositionX; clickPosition[0] = _clickedPositionX;
clickPosition[1]=_clickedPositionY; clickPosition[1] = _clickedPositionY;
return _clicked; return _clicked;
} }
///////////////////////////// /////////////////////////////
// Input_AnyKey // Input_AnyKey
// //
// //
int Input_AnyKey(){ int Input_AnyKey() {
int i; int i;
for(i=0;i<InputKey_Max;i++){ for (i = 0; i < InputKey_Max; i++) {
if(_keys[i]==InputKey_Pressed){ if (_keys[i] == InputKey_Pressed) {
return(1); return (1);
} }
} }
return(0); return (0);
} }
///////////////////////////// /////////////////////////////
// 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) {
float vlen; float vlen;
Uint8 buttons; Uint8 buttons;
int mx,my; int mx, my;
float dlen; float dlen;
extern int _width,_height; extern int _width, _height;
// Get mouse state // Get mouse state
buttons=SDL_GetMouseState(&mx,&my); buttons = SDL_GetMouseState(&mx, &my);
if(buttons){ if (buttons) {
// Use the mouse // Use the mouse
vec2_set(dir,mx-(_width/2),my-(_height/2.0f)); vec2_set(dir, mx - (_width / 2), my - (_height / 2.0f));
dlen=1.0f/sqrtf(vec2_dot(dir,dir)); dlen = 1.0f / sqrtf(vec2_dot(dir, dir));
vec2_scale(dir,dir,dlen); vec2_scale(dir, dir, dlen);
return(1); return (1);
}else{ } else {
// Use the keyboar // Use the keyboar
vec2_set(dir,0.0f,0.0f); vec2_set(dir, 0.0f, 0.0f);
if(Input_GetKey(InputKey_Up) ){ if (Input_GetKey(InputKey_Up)) {
dir[1]-=1.0f; dir[1] -= 1.0f;
} }
if(Input_GetKey(InputKey_Down) ){ if (Input_GetKey(InputKey_Down)) {
dir[1]+=1.0f; dir[1] += 1.0f;
} }
if(Input_GetKey(InputKey_Left) ){ if (Input_GetKey(InputKey_Left)) {
dir[0]-=1.0f; dir[0] -= 1.0f;
} }
if(Input_GetKey(InputKey_Right) ){ if (Input_GetKey(InputKey_Right)) {
dir[0]+=1.0f; dir[0] += 1.0f;
} }
vlen=vec2_dot(dir,dir); vlen = vec2_dot(dir, dir);
if(vlen>0.0f){ if (vlen > 0.0f) {
vlen=sqrtf(vlen); vlen = sqrtf(vlen);
vec2_scale(dir,dir,1.0f/vlen); vec2_scale(dir, dir, 1.0f / vlen);
return(1); return (1);
}else{ } else {
return(0); return (0);
} }
} }
} }

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,13 +43,11 @@ 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,73 +6,91 @@
#include "QuadArray2D.h" #include "QuadArray2D.h"
QuadArray2D QuadArray2D_Create(int resVertex) {
QuadArray2D quadArray = NULL;
QuadArray2D QuadArray2D_Create(int resVertex){ quadArray = malloc(sizeof(TQuadArray2D));
QuadArray2D quadArray=NULL;
quadArray=malloc(sizeof(TQuadArray2D)); quadArray->vertexData = malloc(sizeof(float) * Vertex2D_Length * resVertex);
quadArray->nVertex = 0;
quadArray->vertexData=malloc(sizeof(float)*Vertex2D_Length*resVertex); quadArray->resVertex = resVertex;
quadArray->nVertex=0;
quadArray->resVertex=resVertex;
return quadArray; return quadArray;
} }
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);
quadArray->vertexData=newVertexData; quadArray->vertexData = newVertexData;
} }
// 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;
// Set the color // Set the color
v[4]=color[0]; v[4] = color[0];
v[5]=color[1]; v[5] = color[1];
v[6]=color[2]; v[6] = color[2];
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,19 +16,16 @@ 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);
void QuadArray2D_Clean(QuadArray2D quadArray); 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
// //
@@ -20,46 +19,44 @@
#if WIN32 #if WIN32
#include <windows.h> #include <windows.h>
// WIN32 // WIN32
long long Time_GetTime(){ long long Time_GetTime() {
LARGE_INTEGER freq; LARGE_INTEGER freq;
LARGE_INTEGER tim; LARGE_INTEGER tim;
long long int microt; long long int microt;
QueryPerformanceFrequency(&freq); QueryPerformanceFrequency(&freq);
QueryPerformanceCounter(&tim); QueryPerformanceCounter(&tim);
microt=(tim.QuadPart*1000000)/freq.QuadPart; microt = (tim.QuadPart * 1000000) / freq.QuadPart;
return(microt); return (microt);
} }
void Time_Pause(int pausa){ void Time_Pause(int pausa) {
long long tend,t,diff; long long tend, t, diff;
t=Time_GetTime(); t = Time_GetTime();
tend=t+pausa; tend = t + pausa;
do{ do {
diff=tend-t; diff = tend - t;
if(diff>1000){ if (diff > 1000) {
Sleep(diff/1000); Sleep(diff / 1000);
}else{ } else {
Sleep(0); Sleep(0);
} }
t=Time_GetTime(); t = Time_GetTime();
}while(tend>=t); } while (tend >= t);
} }
#else #else
// UNIX // UNIX
long long Time_GetTime(){ long long Time_GetTime() {
struct timeval t; struct timeval t;
long long usecs; long long usecs;
gettimeofday(&t,NULL); gettimeofday(&t, NULL);
usecs=(t.tv_sec*1000000ll)+(t.tv_usec); usecs = (t.tv_sec * 1000000ll) + (t.tv_usec);
return(usecs); return (usecs);
} }
void Time_Pause(int pausa){ void Time_Pause(int pausa) {
struct timeval tv; struct timeval tv;
tv.tv_sec=(long long)pausa/1000000; tv.tv_sec = (long long)pausa / 1000000;
tv.tv_usec=(long long)pausa%1000000; tv.tv_usec = (long long)pausa % 1000000;
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,42 +8,40 @@
#include "Util.h" #include "Util.h"
///////////////////////////// /////////////////////////////
// SolveQuadratic // SolveQuadratic
// //
// 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) {
float root; float root;
float divisor; float divisor;
float b2; float b2;
b2=b*b; b2 = b * b;
root=b2-4.0*a*c; root = b2 - 4.0 * a * c;
if(root<0){ if (root < 0) {
// Complex // Complex
return(0); return (0);
} }
divisor=(2.0*a); divisor = (2.0 * a);
if(fabs(divisor)==0.0f){ if (fabs(divisor) == 0.0f) {
// +inf -inf // +inf -inf
return(0); return (0);
} }
root=sqrtf(root); root = sqrtf(root);
Rmin[0]=(float)((-b-root)/divisor); Rmin[0] = (float)((-b - root) / divisor);
Rmax[0]=(float)((-b+root)/divisor); Rmax[0] = (float)((-b + root) / divisor);
return(1); return (1);
} }
//////////////////////////////////////////////// ////////////////////////////////////////////////
// vec2 // // vec2 //
////////// //////////
// A 2D vector. // A 2D vector.
float vec2_norm(vec2 v){ float vec2_norm(vec2 v) {
float len; float len;
len=vec2_len(v); len = vec2_len(v);
vec2_scale(v,v,1.0f/len); vec2_scale(v, v, 1.0f / len);
return(len); return (len);
} }
void vec2_orthogonalize4(vec2 v) { void vec2_orthogonalize4(vec2 v) {
@@ -100,187 +98,174 @@ 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) {
float a,b,c; float a, b, c;
float Rmin,Rmax; float Rmin, Rmax;
vec2 distv; vec2 distv;
float qlvel; float qlvel;
float qdistv; float qdistv;
// Check if the collision is even posible // Check if the collision is even posible
qlvel=vec2_dot(vel,vel); qlvel = vec2_dot(vel, vel);
if(fabs(qlvel)<=0.0f) if (fabs(qlvel) <= 0.0f)
return(0); return (0);
vec2_minus(distv,orig,center); vec2_minus(distv, orig, center);
qdistv=vec2_dot(distv,distv); qdistv = vec2_dot(distv, distv);
// Solve as a unit circle // Solve as a unit circle
a=qlvel; a = qlvel;
b=2.0f*vec2_dot(distv,vel); b = 2.0f * vec2_dot(distv, vel);
c=qdistv-1.0f; c = qdistv - 1.0f;
if(SolveQuadratic(a,b,c,&Rmin,&Rmax)){ if (SolveQuadratic(a, b, c, &Rmin, &Rmax)) {
if(Rmin>=-0.0f && Rmin<Rmax && Rmin<=1.0f){ if (Rmin >= -0.0f && Rmin < Rmax && Rmin <= 1.0f) {
*t=Rmin; *t = Rmin;
return(1); return (1);
} }
if(Rmax>=-0.0f && Rmin>Rmax && Rmax<=1.0f){ if (Rmax >= -0.0f && Rmin > Rmax && Rmax <= 1.0f) {
*t=Rmax; *t = Rmax;
return(1); return (1);
} }
} }
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, vec2 vel_a, orig_a, cen_a, temp;
float *t,vec2 n) float rads, invrads;
{ float maxx, minx;
vec2 vel_a,orig_a,cen_a,temp; float maxy, miny;
float rads,invrads;
float maxx,minx;
float maxy,miny;
// Check if the collision is even posible // Check if the collision is even posible
rads=rad1+rad2; rads = rad1 + rad2;
minx=cir1[0]-rads; minx = cir1[0] - rads;
maxx=cir1[0]+rads; maxx = cir1[0] + rads;
if(vel[0]>0){ if (vel[0] > 0) {
maxx+=vel[0]; maxx += vel[0];
}else{ } else {
minx+=vel[0]; minx += vel[0];
} }
if(cir2[0]<minx || cir2[0]>maxx) if (cir2[0] < minx || cir2[0] > maxx)
return(0); return (0);
miny=cir1[1]-rads; miny = cir1[1] - rads;
maxy=cir1[1]+rads; maxy = cir1[1] + rads;
if(vel[1]>0){ if (vel[1] > 0) {
maxy+=vel[1]; maxy += vel[1];
}else{ } else {
miny+=vel[1]; miny += vel[1];
} }
if(cir2[1]<miny || cir2[1]>maxy) if (cir2[1] < miny || cir2[1] > maxy)
return(0); return (0);
// Convert to a unit circle vs ray // Convert to a unit circle vs ray
invrads=1.0f/rads; invrads = 1.0f / rads;
vec2_scale(vel_a,vel,invrads); vec2_scale(vel_a, vel, invrads);
vec2_scale(orig_a,cir1,invrads); vec2_scale(orig_a, cir1, invrads);
vec2_scale(cen_a,cir2,invrads); vec2_scale(cen_a, cir2, invrads);
if(Intersec_RayUnitCircle(orig_a,vel_a,cen_a,t)){ if (Intersec_RayUnitCircle(orig_a, vel_a, cen_a, t)) {
// Calculate n // Calculate n
vec2_scaleadd(temp,cir1,vel,*t); vec2_scaleadd(temp, cir1, vel, *t);
vec2_minus(n,temp,cir2); vec2_minus(n, temp, cir2);
vec2_scale(n,n,invrads); vec2_scale(n, n, invrads);
return(1); return (1);
} }
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, vec2 pos2, intersection, perp, edgePos2;
float *t) float delta, d1, d2, hLen;
{
vec2 pos2,intersection,perp,edgePos2;
float delta,d1,d2,hLen;
vec2_plus(pos2,pos,vel); vec2_plus(pos2, pos, vel);
hLen=len/2; hLen = len / 2;
// Check intersection against the line // Check intersection against the line
delta=vec2_dot(norm,edgePos); delta = vec2_dot(norm, edgePos);
d1=vec2_dot(pos ,norm)-delta; d1 = vec2_dot(pos, norm) - delta;
d2=vec2_dot(pos2,norm)-delta; d2 = vec2_dot(pos2, norm) - delta;
if(d1>=-0.0001f && d2<=0.0001f){ if (d1 >= -0.0001f && d2 <= 0.0001f) {
// Intersection with line, Calculate intersection point // Intersection with line, Calculate intersection point
*t=d1/(d1-d2); *t = d1 / (d1 - d2);
vec2_scaleadd(intersection,pos,vel,*t); vec2_scaleadd(intersection, pos, vel, *t);
// Perpendicular // Perpendicular
vec2_perp(perp,norm); vec2_perp(perp, norm);
// Check sides // Check sides
vec2_scaleadd(edgePos2,edgePos,perp,-hLen); vec2_scaleadd(edgePos2, edgePos, perp, -hLen);
delta=-vec2_dot(perp,edgePos2); delta = -vec2_dot(perp, edgePos2);
d1=(-vec2_dot(perp,intersection))-delta; d1 = (-vec2_dot(perp, intersection)) - delta;
vec2_scaleadd(edgePos2,edgePos,perp,hLen); vec2_scaleadd(edgePos2, edgePos, perp, hLen);
delta=vec2_dot(perp,edgePos2); delta = vec2_dot(perp, edgePos2);
d2=vec2_dot(perp,intersection)-delta; d2 = vec2_dot(perp, intersection) - delta;
if(d1<=0.0f && d2<=0.0f){ if (d1 <= 0.0f && d2 <= 0.0f) {
// Intersection inside Edge. // Intersection inside Edge.
return(1); return (1);
} }
} }
return(0); return (0);
} }
///////////////////////////// /////////////////////////////
// absmod // absmod
// //
int absmod(int v,int d){ int absmod(int v, int d) {
if(v<0){ if (v < 0) {
v+=d*(((v/d)*(-1))+1); v += d * (((v / d) * (-1)) + 1);
return(v); return (v);
}else{ } else {
return(v%d); return (v % d);
} }
} }
float fabsmod(float v,int d){ float fabsmod(float v, int d) {
if(v<0){ if (v < 0) {
v+=d*((((int)(v/d))*(-1))+1); v += d * ((((int)(v / d)) * (-1)) + 1);
return(v); return (v);
}else{ } else {
v-=d*(((int)(v/d))+1); v -= d * (((int)(v / d)) + 1);
return(v); return (v);
} }
} }
///////////////////////////// /////////////////////////////
// IsBigEndian // IsBigEndian
// //
int IsBigEndian(){ int IsBigEndian() {
union{ union {
unsigned int i; unsigned int i;
char c[4]; char c[4];
} bint={0x01020304}; } bint = {0x01020304};
return bint.c[0]==1; return bint.c[0] == 1;
} }
///////////////////////////// /////////////////////////////
// EndsWith // EndsWith
// //
int EndsWith(char *str, char *suffix){ int EndsWith(char *str, char *suffix) {
if (!str || !suffix) if (!str || !suffix)
return 0; return 0;
int lenStr = strlen(str); int lenStr = strlen(str);
int lenSuffix = strlen(suffix); int lenSuffix = strlen(suffix);
if (lenSuffix > lenStr) if (lenSuffix > lenStr)
return 0; return 0;
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
// //
@@ -372,6 +356,5 @@ int Print(char *fmt, ...) {
// Flush // Flush
fflush(stdout); fflush(stdout);
return(n); return (n);
} }

View File

@@ -10,84 +10,85 @@
// SolveQuadratic // SolveQuadratic
// //
// 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]; \
#define vec2_dot(v1,v2) ((v1)[0]*(v2)[0]+(v1)[1]*(v2)[1]) (v1)[1] = (v2)[1];
#define vec2_len(v) sqrtf((v)[0]*(v)[0]+(v)[1]*(v)[1]) #define vec2_plus(v, v1, v2) \
#define vec2_perp(v,n) (v)[0]=-(n)[1];(v)[1]=(n)[0]; (v)[0] = (v1)[0] + (v2)[0]; \
#define vec2_scaleadd(v,v1,v2,s) (v)[0]=(v2)[0]*(s)+(v1)[0];(v)[1]=(v2)[1]*(s)+(v1)[1]; (v)[1] = (v1)[1] + (v2)[1];
#define vec2_minus(v, v1, v2) \
(v)[0] = (v1)[0] - (v2)[0]; \
(v)[1] = (v1)[1] - (v2)[1];
#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_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_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]); \
(v)[1]=(v1)[1]-f*((v1)[1]-(v2)[1]); (v)[1] = (v1)[1] - f * ((v1)[1] - (v2)[1]);
void vec2_orthogonalize4(vec2 v); void vec2_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, float *t, vec2 n);
vec2 cb,float rb,
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, float *t);
vec2 norm,vec2 edgePos,float len,
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
// //
void Rand_Seed(unsigned seed); 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
@@ -95,5 +96,4 @@ unsigned Rand_Get();
// Prints the formated text // Prints the formated text
int Print(char *fmt, ...); int Print(char *fmt, ...);
#endif #endif