(20140701)

This commit is contained in:
2014-07-01 00:00:00 +02:00
parent e283956e98
commit 7e6bd08390
27 changed files with 9164 additions and 707 deletions

View File

@@ -48,43 +48,43 @@ AudioSnd snd_burn;
AudioSnd snd_fillhole;
AudioSnd snd_drag;
Entity *ent_player;
Entity *ent_barrel;
Entity *ent_column;
Entity *ent_column_faded;
Entity *ent_rock;
Entity *ent_lamp;
Entity *ent_floor;
Entity *ent_floor_right;
Entity *ent_floor_left;
Entity *ent_floor_center;
Entity *ent_hole_spiked;
Entity *ent_hole_filled;
Entity *ent_hole_lava;
Entity *ent_arrowshooter_up;
Entity *ent_arrowshooter_down;
Entity *ent_arrowshooter_left;
Entity *ent_arrowshooter_right;
Entity *ent_arrow_up;
Entity *ent_arrow_down;
Entity *ent_arrow_left;
Entity *ent_arrow_right;
Entity *ent_exitpoint;
Entity *ent_endpoint;
Entity *ent_savepoint;
Entity *ent_teleporter;
Entity *ent_teleporter_dest;
Entity ent_player;
Entity ent_barrel;
Entity ent_column;
Entity ent_column_faded;
Entity ent_rock;
Entity ent_lamp;
Entity ent_floor;
Entity ent_floor_right;
Entity ent_floor_left;
Entity ent_floor_center;
Entity ent_hole_spiked;
Entity ent_hole_filled;
Entity ent_hole_lava;
Entity ent_arrowshooter_up;
Entity ent_arrowshooter_down;
Entity ent_arrowshooter_left;
Entity ent_arrowshooter_right;
Entity ent_arrow_up;
Entity ent_arrow_down;
Entity ent_arrow_left;
Entity ent_arrow_right;
Entity ent_exitpoint;
Entity ent_endpoint;
Entity ent_savepoint;
Entity ent_teleporter;
Entity ent_teleporter_dest;
Entity *ent_fire;
Entity *ent_player_broken;
Entity ent_fire;
Entity ent_player_broken;
extern int game_level;
extern int game_level_point;
extern int game_level_reset;
void player_proc(Entity *e,int ft){
void player_proc(Entity e,int ft){
vec2 vel;
int pos[2],size[2],delta[2];
@@ -144,7 +144,7 @@ void player_proc(Entity *e,int ft){
GameLib_SetPos(pos);
}
int player_collision(Entity *e1,Entity *e2,float t,vec2 n){
int player_collision(Entity e1,Entity e2,float t,vec2 n){
if(e2->type==Ent_Barrel){
float vlen;
vec2 vdir;
@@ -164,7 +164,7 @@ int player_collision(Entity *e1,Entity *e2,float t,vec2 n){
return(1);
}
void barrel_proc(Entity *e,int ft){
void barrel_proc(Entity e,int ft){
float qvel;
int tnow;
@@ -179,11 +179,11 @@ void barrel_proc(Entity *e,int ft){
}
void hole_spiked_overlap(Entity *e1,Entity *e2){
Entity *e;
void hole_spiked_overlap(Entity e1,Entity e2){
Entity e;
if(e2->type==Ent_Barrel){
Entity *e;
Entity e;
// Disable future overlaps
e1->overlap=NULL;
@@ -212,8 +212,8 @@ void hole_spiked_overlap(Entity *e1,Entity *e2){
}
void hole_lava_overlap(Entity *e1,Entity *e2){
Entity *e;
void hole_lava_overlap(Entity e1,Entity e2){
Entity e;
if(e2->type==Ent_Barrel){
// "Burn the barrel"
@@ -235,8 +235,8 @@ void hole_lava_overlap(Entity *e1,Entity *e2){
}
int arrow_collision(Entity *e1,Entity *e2,float t,vec2 n){
Entity *e;
int arrow_collision(Entity e1,Entity e2,float t,vec2 n){
Entity e;
if(e1->postproc)
return(0);
@@ -261,13 +261,13 @@ int arrow_collision(Entity *e1,Entity *e2,float t,vec2 n){
return(0);
}
void arrowshooter_oncopy(Entity *e){
void arrowshooter_oncopy(Entity e){
e->A=rand()%30;
}
void arrowshooter_proc(Entity *e,int ft){
void arrowshooter_proc(Entity e,int ft){
if(e->A==0){
Entity *e2;
Entity e2;
e2=Entity_Copy(e->child);
vec2_copy(e2->pos,e->pos);
@@ -280,13 +280,13 @@ void arrowshooter_proc(Entity *e,int ft){
}
}
Entity *_savepoint=NULL;
void savepoint_ondelete(Entity *e){
Entity _savepoint=NULL;
void savepoint_ondelete(Entity e){
if(_savepoint==e){
_savepoint=NULL;
}
}
void savepoint_overlap(Entity *e1,Entity *e2){
void savepoint_overlap(Entity e1,Entity e2){
if(e2->type==Ent_Player){
// Save the point
if(game_level_point!=e1->A){
@@ -305,7 +305,7 @@ void savepoint_overlap(Entity *e1,Entity *e2){
}
}
void exitpoint_overlap(Entity *e1,Entity *e2){
void exitpoint_overlap(Entity e1,Entity e2){
if(e2->type==Ent_Player){
// Exit the level
game_level++;
@@ -319,7 +319,7 @@ void exitpoint_overlap(Entity *e1,Entity *e2){
}
}
void endpoint_overlap(Entity *e1,Entity *e2){
void endpoint_overlap(Entity e1,Entity e2){
if(e2->type==Ent_Player){
// Go to end
game_level_reset=3;
@@ -331,7 +331,7 @@ void endpoint_overlap(Entity *e1,Entity *e2){
}
}
void timeoutent_proc(Entity *e,int ft){
void timeoutent_proc(Entity e,int ft){
if(e->A==0){
GameLib_DelEntity(e);
}else{
@@ -339,7 +339,7 @@ void timeoutent_proc(Entity *e,int ft){
}
}
int teleporter_searchdest(Entity *ent,void *d){
int teleporter_searchdest(Entity ent,void *d){
int a=*(int*)d;
if(ent->type!=Ent_Teleporter_Dest){
return 0;
@@ -351,8 +351,8 @@ int teleporter_searchdest(Entity *ent,void *d){
return 0;
}
void teleporter_overlap(Entity *e1,Entity *e2){
Entity *dest=NULL;
void teleporter_overlap(Entity e1,Entity e2){
Entity dest=NULL;
// Search the destination
dest=GameLib_SearchEnt(teleporter_searchdest,&e1->A);
@@ -364,84 +364,84 @@ void teleporter_overlap(Entity *e1,Entity *e2){
void GameEnts_Init(){
Entity *ent;
Entity ent;
//////////////////////////////
// Load Resources
img_barrel=Draw_LoadImage("data/barrel.bmp");
img_barrel=Draw_LoadImage("data/barrel.png");
Draw_SetOffset(img_barrel,-16,-32);
img_barrel2=Draw_LoadImage("data/barrel2.bmp");
img_barrel2=Draw_LoadImage("data/barrel2.png");
Draw_SetOffset(img_barrel2,-16,-16);
img_floor=Draw_LoadImage("data/floor.bmp");
img_floor=Draw_LoadImage("data/floor.png");
Draw_SetOffset(img_floor,-16,-16);
img_floor_left=Draw_LoadImage("data/floor_left.bmp");
img_floor_left=Draw_LoadImage("data/floor_left.png");
Draw_SetOffset(img_floor_left,-16,-16);
img_floor_right=Draw_LoadImage("data/floor_right.bmp");
img_floor_right=Draw_LoadImage("data/floor_right.png");
Draw_SetOffset(img_floor_right,-16,-16);
img_floor_center=Draw_LoadImage("data/floor_center.bmp");
img_floor_center=Draw_LoadImage("data/floor_center.png");
Draw_SetOffset(img_floor_center,-16,-16);
img_column=Draw_LoadImage("data/column.bmp");
img_column=Draw_LoadImage("data/column.png");
Draw_SetOffset(img_column,-16,-80);
img_column_faded=Draw_LoadImage("data/column_faded.bmp");
img_column_faded=Draw_LoadImage("data/column_faded.png");
Draw_SetOffset(img_column_faded,-16,-80);
img_rock=Draw_LoadImage("data/rock.bmp");
img_rock=Draw_LoadImage("data/rock.png");
Draw_SetOffset(img_rock,-16,-32);
img_lamp=Draw_LoadImage("data/lamp.bmp");
img_lamp=Draw_LoadImage("data/lamp.png");
Draw_SetOffset(img_lamp,-16,-48);
img_hole_spiked=Draw_LoadImage("data/hole_spiked.bmp");
img_hole_spiked=Draw_LoadImage("data/hole_spiked.png");
Draw_SetOffset(img_hole_spiked,-16,-16);
anim_hole_lava=Anim_LoadAnim("data/hole_lava.bmp",2,3);
anim_hole_lava=Anim_LoadAnim("data/hole_lava.png",2,3);
Anim_SetOffset(anim_hole_lava,-16,-16);
img_player_up=Draw_LoadImage("data/player_up.bmp");
img_player_up=Draw_LoadImage("data/player_up.png");
Draw_SetOffset(img_player_up,-16,-48);
img_player_down=Draw_LoadImage("data/player_down.bmp");
img_player_down=Draw_LoadImage("data/player_down.png");
Draw_SetOffset(img_player_down,-16,-48);
img_player_left=Draw_LoadImage("data/player_left.bmp");
img_player_left=Draw_LoadImage("data/player_left.png");
Draw_SetOffset(img_player_left,-16,-48);
img_player_right=Draw_LoadImage("data/player_right.bmp");
img_player_right=Draw_LoadImage("data/player_right.png");
Draw_SetOffset(img_player_right,-16,-48);
img_savepoint=Draw_LoadImage("data/save_point.bmp");
img_savepoint=Draw_LoadImage("data/save_point.png");
Draw_SetOffset(img_savepoint,-16,-16);
anim_savepoint_active=Anim_LoadAnim("data/save_point_active.bmp",2,5);
anim_savepoint_active=Anim_LoadAnim("data/save_point_active.png",2,5);
Anim_SetOffset(anim_savepoint_active,-16,-16);
anim_exitpoint=Anim_LoadAnim("data/exit_point.bmp",2,10);
anim_exitpoint=Anim_LoadAnim("data/exit_point.png",2,10);
Anim_SetOffset(anim_exitpoint,-16,-48);
img_endpoint=Draw_LoadImage("data/end_point.bmp");
img_endpoint=Draw_LoadImage("data/end_point.png");
Draw_SetOffset(img_endpoint,-16,-32);
img_arrowshooter_up=Draw_LoadImage("data/arrowshooter_up.bmp");
img_arrowshooter_up=Draw_LoadImage("data/arrowshooter_up.png");
Draw_SetOffset(img_arrowshooter_up,-16,-16);
img_arrowshooter_down=Draw_LoadImage("data/arrowshooter_down.bmp");
img_arrowshooter_down=Draw_LoadImage("data/arrowshooter_down.png");
Draw_SetOffset(img_arrowshooter_down,-16,-16);
img_arrowshooter_left=Draw_LoadImage("data/arrowshooter_left.bmp");
img_arrowshooter_left=Draw_LoadImage("data/arrowshooter_left.png");
Draw_SetOffset(img_arrowshooter_left,-16,-16);
img_arrowshooter_right=Draw_LoadImage("data/arrowshooter_right.bmp");
img_arrowshooter_right=Draw_LoadImage("data/arrowshooter_right.png");
Draw_SetOffset(img_arrowshooter_right,-16,-16);
img_arrow_up=Draw_LoadImage("data/arrow_up.bmp");
img_arrow_up=Draw_LoadImage("data/arrow_up.png");
Draw_SetOffset(img_arrow_up,-16,-16);
img_arrow_down=Draw_LoadImage("data/arrow_down.bmp");
img_arrow_down=Draw_LoadImage("data/arrow_down.png");
Draw_SetOffset(img_arrow_down,-16,-16);
img_arrow_left=Draw_LoadImage("data/arrow_left.bmp");
img_arrow_left=Draw_LoadImage("data/arrow_left.png");
Draw_SetOffset(img_arrow_left,-16,-16);
img_arrow_right=Draw_LoadImage("data/arrow_right.bmp");
img_arrow_right=Draw_LoadImage("data/arrow_right.png");
Draw_SetOffset(img_arrow_right,-16,-16);
anim_fire=Anim_LoadAnim("data/fire.bmp",3,5);
anim_fire=Anim_LoadAnim("data/fire.png",3,5);
Anim_SetOffset(anim_fire,-16,-48);
img_player_broken=Draw_LoadImage("data/player_broken.bmp");
img_player_broken=Draw_LoadImage("data/player_broken.png");
Draw_SetOffset(img_player_broken,-16,-48);

View File

@@ -21,31 +21,31 @@ enum {
Ent_Teleporter_Dest,
Ent_Effect
} EntityType;
extern Entity *ent_player;
extern Entity *ent_barrel;
extern Entity *ent_column;
extern Entity *ent_column_faded;
extern Entity *ent_rock;
extern Entity *ent_lamp;
extern Entity *ent_floor;
extern Entity *ent_floor_right;
extern Entity *ent_floor_left;
extern Entity *ent_floor_center;
extern Entity *ent_hole_spiked;
extern Entity *ent_hole_filled;
extern Entity *ent_hole_lava;
extern Entity *ent_arrowshooter_up;
extern Entity *ent_arrowshooter_down;
extern Entity *ent_arrowshooter_left;
extern Entity *ent_arrowshooter_right;
extern Entity *ent_arrow_up;
extern Entity *ent_arrow_down;
extern Entity *ent_arrow_left;
extern Entity *ent_arrow_right;
extern Entity *ent_exitpoint;
extern Entity *ent_endpoint;
extern Entity *ent_savepoint;
extern Entity *ent_teleporter;
extern Entity *ent_teleporter_dest;
extern Entity ent_player;
extern Entity ent_barrel;
extern Entity ent_column;
extern Entity ent_column_faded;
extern Entity ent_rock;
extern Entity ent_lamp;
extern Entity ent_floor;
extern Entity ent_floor_right;
extern Entity ent_floor_left;
extern Entity ent_floor_center;
extern Entity ent_hole_spiked;
extern Entity ent_hole_filled;
extern Entity ent_hole_lava;
extern Entity ent_arrowshooter_up;
extern Entity ent_arrowshooter_down;
extern Entity ent_arrowshooter_left;
extern Entity ent_arrowshooter_right;
extern Entity ent_arrow_up;
extern Entity ent_arrow_down;
extern Entity ent_arrow_left;
extern Entity ent_arrow_right;
extern Entity ent_exitpoint;
extern Entity ent_endpoint;
extern Entity ent_savepoint;
extern Entity ent_teleporter;
extern Entity ent_teleporter_dest;
#endif

View File

@@ -4,6 +4,9 @@
#define _WIN32_WINNT 0x0501
#include <windows.h>
#endif
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <SDL/SDL.h>
#include "Audio.h"
@@ -63,7 +66,11 @@ int Audio_Init(){
as.freq = 44100;
as.format = AUDIO_S16SYS;
as.channels = 2;
#ifdef EMSCRIPTEN
as.samples = 4096;
#else
as.samples = 1024;
#endif
as.callback = Audio_MixerCallback;
if(SDL_OpenAudio(&as, &as2) < 0){
printf("Audio_Init: Failure opening audio.\n");
@@ -191,7 +198,7 @@ void Audio_Frame(){
// Loads a sound, giving a reference.
AudioSnd Audio_LoadSound(char *filename){
AudioWave *wave;
#ifndef EMSCRIPTEN
// Allocate and load the sound
wave=malloc(sizeof(AudioWave));
if( SDL_LoadWAV(filename,
@@ -222,6 +229,9 @@ AudioSnd Audio_LoadSound(char *filename){
_waves=wave;
return((AudioSnd)wave);
#else
return(NULL);
#endif
}

View File

@@ -2,6 +2,7 @@
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#ifdef WIN32
@@ -10,23 +11,39 @@
#include <GL/gl.h>
#include <GL/glext.h>
#else
#ifdef MACOSX
#include <Cocoa/Cocoa.h>
#include <OpenGL/OpenGL.h>
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#include <OpenGL/glext.h>
#ifdef EMSCRIPTEN
//#include <GLES2/gl2.h>
//#define GL_GLEXT_PROTOTYPES 1
//#include <GLES2/gl2ext.h>
#include <emscripten.h>
#include <GL/gl.h>
#else
#include <GL/gl.h>
#endif
#endif
#include "lodepng.c"
#include <SDL/SDL.h>
#include "Time.h"
#include "Util.h"
#include "Draw.h"
#include "Input.h"
#include "QuadArray2D.h"
#include "Audio.h"
#include "Input.h"
#include "Draw.h"
////////////////////////////////////////////////
// DrawImage //
///////////////
// Image container.
typedef struct TDrawImage TDrawImage, *DrawImage;
struct TDrawImage {
unsigned char *data;
int x,y;
int w,h;
GLuint tex;
};
// Globals
@@ -35,6 +52,13 @@ int _width;
int _height;
long long proc_t_frame=33333;
long long draw_t_frame=16667;
int _fps=60;
QuadArray2D _quadArray=NULL;
DrawImage _currentImg=NULL;
float _color[4];
/////////////////////////////
// Draw_Init
@@ -57,7 +81,7 @@ int Draw_Init(int width,int height,char *title,int pfps,int fps){
// Initialize SDL
if(SDL_Init(SDL_INIT_VIDEO)<0){
printf("Draw_Init: Failure initializing SDL.\n");
printf("Draw_Init: SDL Error: %s\n",SDL_GetError());
printf("\tSDL Error: %s\n",SDL_GetError());
return(0);
}
@@ -69,7 +93,6 @@ int Draw_Init(int width,int height,char *title,int pfps,int fps){
SDL_GL_SetAttribute (SDL_GL_ALPHA_SIZE, 8);
SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE, 24);
SDL_GL_SetAttribute (SDL_GL_STENCIL_SIZE, 8);
SDL_GL_SetAttribute (SDL_GL_SWAP_CONTROL, 0);
SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, 1);
@@ -77,18 +100,19 @@ int Draw_Init(int width,int height,char *title,int pfps,int fps){
_screen=SDL_SetVideoMode(width,height,32,SDL_HWSURFACE|SDL_OPENGL);
if( _screen == NULL){
printf("Draw_Init: Failure initializing video mode.\n");
printf("Draw_Init: SDL Error: %s\n",SDL_GetError());
printf("\tSDL Error: %s\n",SDL_GetError());
return(0);
}
SDL_WM_SetCaption(title, NULL);
proc_t_frame=1000000/pfps;
draw_t_frame=1000000/fps;
_fps=fps;
_width=width;
_height=height;
// Set the desired state
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glEnable(GL_CULL_FACE);
//glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glDisable(GL_CULL_FACE);
glEnable(GL_TEXTURE_2D);
glDisable(GL_LIGHTING);
glDisable(GL_DEPTH_TEST);
@@ -126,83 +150,123 @@ int Draw_Init(int width,int height,char *title,int pfps,int fps){
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
// Initialize the triangle array
_quadArray=QuadArray2D_Create(400);
Draw_SetColor(1.0f,1.0f,1.0f,1.0f);
return(1);
}
/////////////////////////////
// Draw_LoopIteration
//
// One iteracion of the loop updating the game window.
int (*_proc_func)()=NULL;
void (*_draw_func)(float f)=NULL;
long long _accTime;
int Draw_LoopIteration(){
SDL_Event event;
Uint8* keys;
int done=0;
// Update screen
SDL_GL_SwapBuffers();
// Process Events
while(SDL_PollEvent(&event) ){
if(event.type == SDL_QUIT ){
done=1;
}
if(event.type == SDL_KEYDOWN ){
if(event.key.keysym.sym == SDLK_ESCAPE ) {
done=1;
}
}
}
#ifndef EMSCRIPTEN
// Process keys for Draw
keys=(Uint8 *)SDL_GetKeyState(NULL);
if(keys[SDLK_F12]){
// Screenshot key
Draw_SaveScreenshoot("shot.bmp");
}
#endif
// Sound Frame
Audio_Frame();
// Process
if(_proc_func){
if(_accTime>100000){
_accTime=100000;
}
while(_accTime>=proc_t_frame && !done){
Input_Frame();
if(!_proc_func()){
done=1;
}
_accTime-=proc_t_frame;
}
}
// Draw
if(_draw_func){
float frameFactor=0.0f;
frameFactor=(float)_accTime/(float)proc_t_frame;
_draw_func(frameFactor);
Draw_Flush();
}
return done;
}
#ifdef EMSCRIPTEN
long long _procTime1;
long long _procTime2;
void Draw_LoopIterationAux(){
Draw_LoopIteration();
// Update time
_procTime2=Time_GetTime();
_accTime+=_procTime2-_procTime1;
_procTime1=_procTime2;
}
#endif
/////////////////////////////
// Draw_Loop
//
// Loops updating the game window.
void Draw_Loop(int (*proc)(),void (*draw)()){
int done=0;
SDL_Event event;
Uint8* keys;
long long time,time2,timed;
long long t_frame=0;
void Draw_Loop(int (*proc)(),void (*draw)(float f)){
long long newTime;
long long procTime1,procTime2,drawTime1,drawTime2,waitTime;
t_frame=proc_t_frame;
time=Time_GetTime();
while(!done){
_proc_func=proc;
_draw_func=draw;
#ifndef EMSCRIPTEN
_accTime=proc_t_frame;
procTime1=drawTime1=Time_GetTime();
while(!Draw_LoopIteration()){
// Update screen
SDL_GL_SwapBuffers();
// Wait to round draw_t_frame
drawTime2=Time_GetTime();
waitTime=draw_t_frame-(drawTime2-drawTime1);
Time_Pause(waitTime);
drawTime2=Time_GetTime();
drawTime1=drawTime2;
// Process Events
while(SDL_PollEvent(&event) ){
if(event.type == SDL_QUIT ){
done=1;
}
if(event.type == SDL_KEYDOWN ){
if(event.key.keysym.sym == SDLK_ESCAPE ) {
done=1;
}
}
}
// Process keys for Draw
keys=SDL_GetKeyState(NULL);
if(keys[SDLK_F12]){
// Screenshot key
Draw_SaveScreenshoot("shot.bmp");
}
// Sound Frame
Audio_Frame();
// Process and draw
if(proc){
while(t_frame>=proc_t_frame && !done){
Input_Frame();
if(!proc()){
done=1;
}
t_frame-=proc_t_frame;
}
}
if(draw){
draw();
}
// Measure time
time2=Time_GetTime();
timed=time2-time;
if(timed<draw_t_frame){
Time_Pause(draw_t_frame-timed);
time2=Time_GetTime();
t_frame+=time2-time;
}else{
t_frame+=timed;
}
time=time2;
// FIX: Limit process frame rate
if(t_frame>50000){
t_frame=50000;
}
// Update time
procTime2=Time_GetTime();
_accTime+=procTime2-procTime1;
procTime1=procTime2;
}
#else
_accTime=proc_t_frame;
_procTime1=Time_GetTime();
emscripten_set_main_loop(Draw_LoopIterationAux, _fps, 1);
#endif
}
@@ -215,123 +279,84 @@ void Draw_Clean(
unsigned char g,
unsigned char b)
{
glClearColor(r/256.0f,g/256.0f,b/256.0f,1.0f);
glClearColor(r/255.0f,g/255.0f,b/255.0f,1.0f);
glClear(GL_COLOR_BUFFER_BIT);
}
////////////////////////////////////////////////
// DrawImage //
///////////////
// Image container.
typedef struct Tag_DrawImage {
SDL_Surface *surf;
GLuint tex;
int x,y;
} DrawImage;
/////////////////////////////
// Draw_LoadSurface
//
// Loads a surface.
SDL_Surface *Draw_LoadSurface(char *filename){
SDL_Surface *surf;
// Load the BMP as a surface
surf=SDL_LoadBMP(filename);
if(surf == NULL){
printf("Draw_LoadImage: Failure Loading image: %s\n",filename);
printf("Draw_LoadImage: SDL Error: %s\n",SDL_GetError());
return(NULL);
}
if (surf->format->BytesPerPixel==4) {
// Swap RGB to BGR
Uint32 *ptr,*ptr_end;
ptr=(Uint32 *)surf->pixels;
ptr_end=ptr+(surf->w*surf->h);
while (ptr<ptr_end) {
unsigned char temp;
unsigned char *pixel;
pixel=(unsigned char *)ptr;
temp=pixel[2];
pixel[2]=pixel[0];
pixel[0]=temp;
ptr++;
}
}
return(surf);
}
/////////////////////////////
// Draw_UploadGLTexture
//
// Uploads a OpenGL texture.
GLuint Draw_UploadGLTexture(SDL_Surface *surf){
GLuint Draw_UploadGLTexture(int w, int h, unsigned char *pixels){
GLuint tex;
// Generate OpenGL texture
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
//glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
//glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
// Load OpenGL texture
glBindTexture(GL_TEXTURE_2D, tex);
glPixelStorei( GL_UNPACK_ROW_LENGTH, surf->w );
//glPixelStorei( GL_UNPACK_ROW_LENGTH, w );
glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
//glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8,
// surf->w, surf->h, 0,
// GL_RGBA, GL_UNSIGNED_BYTE, surf->pixels);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
surf->w, surf->h, 0,
GL_RGBA, GL_UNSIGNED_BYTE, surf->pixels);
//glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8,
// imagen->ancho, imagen->alto, 0,
// GL_RGB, GL_UNSIGNED_BYTE, imagen->data);
w, h, 0,
GL_RGBA, GL_UNSIGNED_BYTE, pixels);
return(tex);
}
/////////////////////////////
// Draw_CreateImage
//
DrawImg Draw_CreateImage(int w,int h){
DrawImage image;
// Create the image container
image=malloc(sizeof(TDrawImage));
image->data=malloc(w*h*4);
image->x=0;
image->y=0;
image->w=w;
image->h=h;
image->tex=-1;
return((DrawImg)image);
}
/////////////////////////////
// Draw_LoadImage
//
// Loads a image, giving a reference.
DrawImg Draw_LoadImage(char *filename){
DrawImage *image;
SDL_Surface *surf;
DrawImage image;
// Loads the surface
surf=Draw_LoadSurface(filename);
if(surf == NULL){
return(NULL);
// Try loading PNG images
if(EndsWith(filename,".png") || EndsWith(filename,".PNG")){
image=malloc(sizeof(TDrawImage));
unsigned error = lodepng_decode32_file(
&image->data,
(unsigned*)&image->w,
(unsigned*)&image->h,
filename);
if(error){
printf("Draw_LoadImage: PNG decoder error %u: %s\n", error, lodepng_error_text(error));
return(NULL);
}
image->x=-(int)(image->w/2);
image->y=-(int)(image->h/2);
image->tex=-1;
return (DrawImg)image;
}
// Create the image container
image=malloc(sizeof(DrawImage));
image->surf=surf;
image->tex=Draw_UploadGLTexture(surf);
//image->x=0;
//image->y=0;
image->x=-(surf->w/2);
image->y=-(surf->h/2);
return((DrawImg)image);
printf("Draw_LoadImage: Image type not supported: %s\n",filename);
return(NULL);
}
@@ -340,11 +365,11 @@ DrawImg Draw_LoadImage(char *filename){
//
// Gets the image size.
void Draw_GetSize(DrawImg img,int *w,int *h){
DrawImage *image=img;
DrawImage image=img;
// Gets the image size
*w=image->surf->w;
*h=image->surf->h;
*w=image->w;
*h=image->h;
}
@@ -354,14 +379,14 @@ void Draw_GetSize(DrawImg img,int *w,int *h){
//
// Sets and Gets the image offset.
void Draw_SetOffset(DrawImg img,int x,int y){
DrawImage *image=img;
DrawImage image=img;
// Sets the image offset
image->x=x;
image->y=y;
}
void Draw_GetOffset(DrawImg img,int *x,int *y){
DrawImage *image=img;
DrawImage image=img;
// Gets the image offset
*x=image->x;
@@ -369,35 +394,65 @@ void Draw_GetOffset(DrawImg img,int *x,int *y){
}
/////////////////////////////
// Draw_Flush
//
// Performs all the queued draw actions.
void Draw_Flush(){
if(_currentImg==NULL){
return;
}
if(_currentImg->tex==-1){
_currentImg->tex=Draw_UploadGLTexture(_currentImg->w, _currentImg->h, _currentImg->data);
}
// Draw the quad array
glBindTexture(GL_TEXTURE_2D, _currentImg->tex);
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
glColorPointer( 4, GL_FLOAT, Vertex2D_Length*sizeof(float),
(GLvoid *)(_quadArray->vertexData+4) );
glTexCoordPointer( 2, GL_FLOAT, Vertex2D_Length*sizeof(float),
(GLvoid *)(_quadArray->vertexData+2) );
glVertexPointer( 2, GL_FLOAT, Vertex2D_Length*sizeof(float),
(GLvoid *)(_quadArray->vertexData) );
glDrawArrays(GL_QUADS,0,_quadArray->nVertex);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
// Empty it
QuadArray2D_Clean(_quadArray);
}
/////////////////////////////
// Draw_DrawImg
//
// Draws an image.
void Draw_DrawImg(DrawImg img,int x,int y){
DrawImage *image=img;
int x1,x2,y1,y2;
DrawImage image=img;
float x1,x2,y1,y2;
// Prepare
x1=x+image->x;
y1=_height-(y+image->y);
x2=(x+image->x)+image->surf->w;
y2=_height-((y+image->y)+image->surf->h);
x2=(x+image->x)+image->w;
y2=_height-((y+image->y)+image->h);
// Draw a quad
glBindTexture(GL_TEXTURE_2D, image->tex);
glBegin (GL_QUADS);
glTexCoord2f (1, 0);
glVertex2i (x2, y1);
glTexCoord2f (0, 0);
glVertex2i (x1, y1);
glTexCoord2f (0, 1);
glVertex2i (x1, y2);
glTexCoord2f (1, 1);
glVertex2i (x2, y2);
glEnd ();
if(_currentImg!=image){
Draw_Flush();
_currentImg=image;
}
QuadArray2D_AddQuad(_quadArray,
x1,y1,0.0f,0.0f,
x2,y2,1.0f,1.0f,
_color);
}
@@ -406,7 +461,7 @@ void Draw_DrawImg(DrawImg img,int x,int y){
//
// Draws an image, resizing.
void Draw_DrawImgResized(DrawImg img,int x,int y,float w,float h){
DrawImage *image=img;
DrawImage image=img;
int x1,x2,y1,y2;
// Prepare
@@ -416,20 +471,14 @@ void Draw_DrawImgResized(DrawImg img,int x,int y,float w,float h){
y2=_height-((y+image->y)+h);
// Draw a quad
glBindTexture(GL_TEXTURE_2D, image->tex);
glBegin (GL_QUADS);
glTexCoord2f (1, 0);
glVertex2i (x2, y1);
glTexCoord2f (0, 0);
glVertex2i (x1, y1);
glTexCoord2f (0, 1);
glVertex2i (x1, y2);
glTexCoord2f (1, 1);
glVertex2i (x2, y2);
glEnd ();
if(_currentImg!=image){
Draw_Flush();
_currentImg=image;
}
QuadArray2D_AddQuad(_quadArray,
x1,y1,0.0f,0.0f,
x2,y2,1.0f,1.0f,
_color);
}
@@ -439,7 +488,7 @@ void Draw_DrawImgResized(DrawImg img,int x,int y,float w,float h){
//
// Draws an image part.
void Draw_DrawImgPart(DrawImg img,int x,int y,int w,int i){
DrawImage *image=img;
DrawImage image=img;
int x1,x2,y1,y2;
float us,u1,u2;
@@ -447,26 +496,20 @@ void Draw_DrawImgPart(DrawImg img,int x,int y,int w,int i){
x1=x+image->x;
y1=_height-(y+image->y);
x2=(x+image->x)+w;
y2=_height-((y+image->y)+image->surf->h);
us=1.0f/image->surf->w;
y2=_height-((y+image->y)+image->h);
us=1.0f/image->w;
u1=us*i*w;
u2=u1+us*w;
// Draw a quad
glBindTexture(GL_TEXTURE_2D, image->tex);
glBegin (GL_QUADS);
glTexCoord2f (u2, 0);
glVertex2i (x2, y1);
glTexCoord2f (u1, 0);
glVertex2i (x1, y1);
glTexCoord2f (u1, 1);
glVertex2i (x1, y2);
glTexCoord2f (u2, 1);
glVertex2i (x2, y2);
glEnd ();
if(_currentImg!=image){
Draw_Flush();
_currentImg=image;
}
QuadArray2D_AddQuad(_quadArray,
x1,y1,u1,0.0f,
x2,y2,u2,1.0f,
_color);
}
@@ -476,6 +519,10 @@ void Draw_DrawImgPart(DrawImg img,int x,int y,int w,int i){
//
void Draw_SetColor(float r,float g,float b,float a){
glColor4f(r,g,b,a);
_color[0]=r;
_color[1]=g;
_color[2]=b;
_color[3]=a;
}
@@ -491,51 +538,41 @@ typedef struct {
/////////////////////////////
// Draw_DefaultFont
// Draw_DefaultImage
//
// Creates a surface with the default font.
// Creates a image with the default font.
#include "FontData.h"
SDL_Surface *Draw_DefaultFontSurface(
DrawImage Draw_DefaultFontImage(
unsigned char r,
unsigned char g,
unsigned char b,
unsigned char a)
{
SDL_Surface *surf;
DrawImage img;
int x,y,c;
Uint32 color,color2;
// Create the surface
surf = SDL_CreateRGBSurface(SDL_SWSURFACE,
8*256, 8, 32,0,0,0,0);
surf->format->Amask=0xFF000000;
surf->format->Ashift=24;
SDL_SetAlpha(surf, SDL_SRCALPHA, 255);
// HACK: Set the colors in BGR order
color =SDL_MapRGBA(surf->format,b,g,r,a);
color2=SDL_MapRGBA(surf->format,b,g,r,0);
// Create the image and colors
img=Draw_CreateImage(8*256,8);
// Draw the font
SDL_LockSurface(surf);
for(c=0;c<256;c++){
for(y=0;y<8;y++){
for(x=0;x<8;x++){
int offset=((c*8+x)+(8*256*y))*4;
img->data[offset+0]=r;
img->data[offset+1]=g;
img->data[offset+2]=b;
if(((fontdata_8x8[c*8+y]>>(7-x)) & 0x01)==1){
//Imagen_PutPixel(dest,c*8+x,y,color);
((Uint32 *)surf->pixels)[(c*8+x)+(8*256*y)]=
color;
img->data[offset+3]=0xFF;
}else{
//Imagen_PutPixel(dest,c*8+x,y,color2);
((Uint32 *)surf->pixels)[(c*8+x)+(8*256*y)]=
color2;
img->data[offset+3]=0x00;
}
}
}
}
SDL_UnlockSurface(surf);
return(surf);
return(img);
}
@@ -553,10 +590,7 @@ DrawFnt Draw_DefaultFont(
// Create the default font
font=malloc(sizeof(DrawFont));
font->img.surf=Draw_DefaultFontSurface(r,g,b,a);
font->img.tex=Draw_UploadGLTexture(font->img.surf);
font->img.x=0;
font->img.y=0;
font->img=Draw_DefaultFontImage(r,g,b,a);
font->w=8;
font->h=8;
font->min=0;
@@ -574,12 +608,9 @@ DrawFnt Draw_LoadFont(char *fichero,int min,int max){
// Create the font form the image
font=malloc(sizeof(DrawFont));
font->img.surf=Draw_LoadSurface(fichero);
font->img.tex=Draw_UploadGLTexture(font->img.surf);
font->img.x=0;
font->img.y=0;
font->w=font->img.surf->w/(max-min);
font->h=font->img.surf->h;
font->img=Draw_LoadImage(fichero);
font->w=font->img->w/(max-min);
font->h=font->img->h;
font->min=min;
font->max=max;
@@ -599,7 +630,7 @@ void Draw_DrawText(DrawFnt f,char *text,int x,int y){
ptr=text;
while(*ptr){
if((*ptr)<font->max){
Draw_DrawImgPart((DrawImg)&font->img,x,y,font->w,(*ptr)-font->min);
Draw_DrawImgPart(font->img,x,y,font->w,(*ptr)-font->min);
}
x+=font->w;
ptr++;

View File

@@ -15,7 +15,7 @@ int Draw_Init(int width,int height,char *title,int pfps,int fps);
// Draw_Loop
//
// Loops updating the game window.
void Draw_Loop(int (*proc)(),void (*draw)());
void Draw_Loop(int (*proc)(),void (*draw)(float f));
/////////////////////////////
@@ -28,6 +28,13 @@ void Draw_Clean(
unsigned char b);
/////////////////////////////
// Draw_Flush
//
// Performs all the queued draw actions.
void Draw_Flush();
////////////////////////////////////////////////
// DrawImg //
/////////////
@@ -35,6 +42,12 @@ void Draw_Clean(
typedef void *DrawImg;
/////////////////////////////
// Draw_CreateImage
//
DrawImg Draw_CreateImage(int w,int h);
/////////////////////////////
// Draw_LoadImage
//

View File

@@ -1,4 +1,4 @@
// Copyright (C) 2011 Valeriano Alfonso Rodriguez (Kableado)
// Copyright (C) 2011-2014 Valeriano Alfonso Rodriguez (Kableado)
#include <stdio.h>
#include <stdlib.h>
@@ -17,12 +17,12 @@
// Entity_New
//
//
Entity *_free_entity=NULL;
Entity *Entity_New(){
Entity *e;
Entity _free_entity=NULL;
Entity Entity_New(){
Entity e;
if(!_free_entity){
e=malloc(sizeof(Entity));
e=malloc(sizeof(TEntity));
}else{
e=_free_entity;
_free_entity=e->next;
@@ -30,6 +30,7 @@ Entity *Entity_New(){
e->base=NULL;
e->type=0;
vec2_set(e->pos0,0.0f,0.0f);
vec2_set(e->pos,0.0f,0.0f);
e->flags=EntityFlag_Collision|EntityFlag_Overlap;
e->zorder=1;
@@ -75,7 +76,7 @@ Entity *Entity_New(){
// Entity_Destroy
//
//
void Entity_Destroy(Entity *e){
void Entity_Destroy(Entity e){
if(e->ondelete){
e->ondelete(e);
}
@@ -88,8 +89,8 @@ void Entity_Destroy(Entity *e){
// Entity_Copy
//
//
Entity *Entity_Copy(Entity *e){
Entity *n;
Entity Entity_Copy(Entity e){
Entity n;
n=Entity_New();
@@ -133,6 +134,8 @@ Entity *Entity_Copy(Entity *e){
n->D=e->D;
n->child=e->child;
Entity_CalcBBox(n);
// Call the copy event
if(n->oncopy){
n->oncopy(n);
@@ -142,22 +145,67 @@ Entity *Entity_Copy(Entity *e){
}
/////////////////////////////
// Entity_CalcBBox
//
//
#define BBox_ExtraMargin 10
#define max(a,b) ((a)>(b)?(a):(b))
void Entity_CalcBBox(Entity e){
float hHeight=(max(e->height,e->radius)/2)+BBox_ExtraMargin;
float hWidth=(max(e->width,e->radius)/2)+BBox_ExtraMargin;
if(e->vel[0]>0){
e->maxX=e->pos[0]+e->vel[0]+hWidth;
e->minX=e->pos[0]-hWidth;
}else{
e->minX=(e->pos[0]+e->vel[0])-hWidth;
e->maxX=e->pos[0]+hWidth;
}
if(e->vel[1]>0){
e->maxY=e->pos[1]+e->vel[1]+hHeight;
e->minY=e->pos[1]-hHeight;
}else{
e->minY=(e->pos[1]+e->vel[1])-hHeight;
e->maxY=e->pos[1]+hHeight;
}
}
/////////////////////////////
// Entity_BBoxIntersect
//
//
int Entity_BBoxIntersect(Entity ent1,Entity ent2){
if( ent1->maxX>=ent2->minX && ent1->minX<=ent2->maxX &&
ent1->maxY>=ent2->minY && ent1->minY<=ent2->maxY )
{
return(1);
}
return(0);
}
/////////////////////////////
// Entity_Draw
//
//
void Entity_Draw(Entity *e,int x,int y){
void Entity_Draw(Entity e,int x,int y,float f){
vec2 fPos;
Draw_SetColor(e->color[0],e->color[1],e->color[2],e->color[3]);
AnimPlay_Draw(&e->anim,e->pos[0]+x,e->pos[1]+y);
if(e->flags&EntityFlag_UpdatedPos){
vec2_interpol(fPos,e->pos0,e->pos,f);
AnimPlay_Draw(&e->anim,fPos[0]+x,fPos[1]+y);
}else{
AnimPlay_Draw(&e->anim,e->pos[0]+x,e->pos[1]+y);
}
}
/////////////////////////////
// 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){
int xmax,xmin;
int ymax,ymin;
int ih,iw;
@@ -185,7 +233,7 @@ int Entity_IsVisible(Entity *e,int x,int y,int w,int h){
// Entity_Process
//
//
void Entity_Process(Entity *b,int ft){
void Entity_Process(Entity b,int ft){
b->flags&=~EntityFlag_UpdatedPos;
// Launch method
@@ -199,9 +247,11 @@ void Entity_Process(Entity *b,int ft){
// Entity_PostProcess
//
//
void Entity_PostProcess(Entity *e,int ft){
void Entity_PostProcess(Entity e,int ft){
float qlen,len;
vec2_copy(e->pos0,e->pos);
// Determine if there is movement
qlen=vec2_dot(e->vel,e->vel);
if(qlen>0.0f){
@@ -223,6 +273,8 @@ void Entity_PostProcess(Entity *e,int ft){
// Mark the update of the position.
vec2_copy(e->oldpos,e->pos);
e->flags|=EntityFlag_UpdatedPos;
Entity_CalcBBox(e);
}
// Launch method
@@ -236,131 +288,123 @@ void Entity_PostProcess(Entity *e,int ft){
/////////////////////////////
// Entity_CollisionResponseClircle
// CollisionInfo_New
//
// Normal response to a collision between circles.
void Entity_CollisionResponseCircle(
Entity *b1,Entity *b2,float t,vec2 n)
{
float moment;
vec2 temp;
float elast;
//
CollisionInfo _free_collInfo=NULL;
CollisionInfo CollisionInfo_New(int responseType,Entity ent1,Entity ent2,float t,vec2 n,int applyFriction){
CollisionInfo collInfo;
if(b1->mass>0.0f && b2->mass>0.0f){
// Calculate elasticity
elast=(b1->mass*b1->elast+b2->mass*b2->elast)/
(b1->mass+b2->mass);
// Collision between two massed balls
moment=((1.0f+elast)*b1->mass*b2->mass*
(fabs(vec2_dot(b1->vel,n))+fabs(vec2_dot(b2->vel,n))))
/(b1->mass+b2->mass);
vec2_scale(temp,n,moment/b1->mass);
vec2_minus(b1->vel,b1->vel,temp);
vec2_scale(temp,n,moment/b2->mass);
vec2_plus(b2->vel,b2->vel,temp);
}else
if(b1->mass>0.0f && b2->mass<=0.0f){
// Collision between a massed ball and a fixed ball
moment=(1.0f+b1->elast)*
(vec2_dot(b1->vel,n));
vec2_scale(temp,n,moment);
vec2_minus(b1->vel,b1->vel,temp);
}else
if(b1->mass<=0.0f && b2->mass>0.0f){
// Collision between a massed ball and a fixed ball
// (imposible, but better safe)
moment=(1.0f+b2->elast)*
(vec2_dot(b2->vel,n));
vec2_scale(temp,n,moment);
vec2_plus(b2->vel,b2->vel,temp);
if(!_free_collInfo){
collInfo=malloc(sizeof(TCollisionInfo));
}else{
// Collision between 2 fixed balls
// (imposible, but better safe)
vec2_set(b1->vel,0,0);
vec2_set(b2->vel,0,0);
collInfo=_free_collInfo;
_free_collInfo=collInfo->next;
}
collInfo->next=NULL;
collInfo->responseType=responseType;
collInfo->ent1=ent1;
collInfo->ent2=ent2;
collInfo->t=t;
vec2_copy(collInfo->n,n);
collInfo->applyFriction=applyFriction;
return collInfo;
}
/////////////////////////////
// Entity_CollisionResponseLine
// CollisionInfo_Destroy
//
// Normal response to a collision with a line.
void Entity_CollisionResponseLine(
Entity *ent,Entity *ent2,float t,vec2 norm,int applyFriction)
//
void CollisionInfo_Destroy(CollisionInfo *collInfoRef){
if(collInfoRef==NULL || collInfoRef[0]==NULL){return;}
CollisionInfo collInfo=collInfoRef[0];
CollisionInfo nextCollInfo;
while(collInfo!=NULL){
nextCollInfo=collInfo->next;
collInfo->next=_free_collInfo;
_free_collInfo=collInfo;
collInfo=nextCollInfo;
}
collInfoRef[0]=NULL;
}
/////////////////////////////
// CollisionInfo_Add
//
//
void CollisionInfo_Add(CollisionInfo *collInfoRef,
int responseType,Entity ent1,Entity ent2,float t,vec2 n,int applyFriction)
{
vec2 pos2,vel2,velFric,intersection;
float dist,fric_static,fric_dynamic,fricLen;
if(collInfoRef==NULL){return;}
CollisionInfo prevCollInfo=NULL;
CollisionInfo collInfo=collInfoRef[0];
CollisionInfo newCollInfo=CollisionInfo_New(responseType,ent1,ent2,t,n,applyFriction);
// Calculate friction
fric_static=(ent->fric_static+ent2->fric_static)/2;
fric_dynamic=(ent->fric_dynamic+ent2->fric_dynamic)/2;
// Calculate end position
vec2_scale(vel2,ent->vel,1.0f-t);
dist=-vec2_dot(norm,vel2);
vec2_plus(pos2,ent->pos,ent->vel);
vec2_scaleadd(pos2,pos2,norm,dist);
// Calculate intersection
vec2_scaleadd(intersection,ent->pos,ent->vel,t);
if(applyFriction){
// Apply friction
vec2_minus(velFric,pos2,intersection);
fricLen=sqrtf(vec2_dot(velFric,velFric));
if(fricLen<fric_static){
// Apply static friction
vec2_copy(pos2,intersection);
}else{
// Apply dynamic friction
if(fricLen>0.0f){
vec2_scaleadd(pos2,intersection,velFric,
1.0f-(fric_dynamic+(fric_static/fricLen)));
}else{
vec2_scaleadd(pos2,intersection,velFric,
1.0f-fric_dynamic);
}
}
while(collInfo!=NULL && collInfo->t<t){
prevCollInfo=collInfo;
collInfo=collInfo->next;
}
// Apply to velocity
vec2_scaleadd(pos2,pos2,norm,0.1f);
vec2_minus(ent->vel,pos2,ent->pos);
if(prevCollInfo==NULL){
collInfoRef[0]=newCollInfo;
}else{
prevCollInfo->next=newCollInfo;
}
newCollInfo->next=collInfo;
}
/////////////////////////////
// Entity_Collide
// CollisionInfo_CheckRepetition
//
//
int Entity_Collide(Entity *b1,Entity *b2){
int CollisionInfo_CheckRepetition(CollisionInfo collInfo,Entity ent1,Entity ent2)
{
while(collInfo!=NULL){
if((collInfo->ent1==ent1 && collInfo->ent2==ent2) ||
(collInfo->ent1==ent2 && collInfo->ent2==ent1))
{
return(1);
}
collInfo=collInfo->next;
}
return(0);
}
/////////////////////////////
// Entity_CheckCollisions
//
//
int Entity_CheckCollision(Entity ent1,Entity ent2,CollisionInfo *collInfoRef){
float t;
vec2 n,p;
vec2 vel;
int flags=b1->flags|b2->flags;
int flags=ent1->flags|ent2->flags;
if(flags&EntityFlag_Platform && !(flags&EntityFlag_Block)){
// One of the entities is a platform and none is a block
Entity *ent,*ent_plat;
Entity ent,ent_plat;
float plat_width;
vec2 p;
// Decide who is the platform and who is the ent
if(b1->mass<=0.0f && b2->mass>0.0f){
ent=b2;
ent_plat=b1;
if(ent1->mass<=0.0f && ent2->mass>0.0f){
ent=ent2;
ent_plat=ent1;
}else
if(b2->mass<=0.0f && b1->mass>0.0f){
ent=b1;
ent_plat=b2;
if(ent2->mass<=0.0f && ent1->mass>0.0f){
ent=ent1;
ent_plat=ent2;
}else{
// Two static or two dinamic entities?!?
return(0);
}
// Check Top
vec2_set(n,0,-1);
vec2_scaleadd(p,ent_plat->pos,n,(ent->height+ent_plat->height)/2);
@@ -368,36 +412,10 @@ int Entity_Collide(Entity *b1,Entity *b2){
if(Intersect_RayEdge(ent->pos,ent->vel,
n,p,plat_width,&t))
{
int response=1;
int rc;
// Check the collision methods
if(ent->collision){
rc=ent->collision(ent,ent_plat,t,n);
if (rc==0)
response=0;
if (rc>1)
response=2;
}
if(ent_plat->collision){
vec2 n2;
vec2_scale(n2,n,-1.0f);
rc=ent_plat->collision(ent_plat,ent,t,n2);
if (rc==0)
response=0;
if (rc>1)
response=2;
}
// Collision response
if(response==1){
Entity_CollisionResponseLine(ent,ent_plat,t,n,1);
return(1);
}
if (response==2) {
return(1);
}
return(0);
// Keep colision info
CollisionInfo_Add(collInfoRef,
CollisionResponse_Line,ent,ent_plat,t,n,1);
return(1);
}
return(0);
@@ -405,19 +423,19 @@ int Entity_Collide(Entity *b1,Entity *b2){
if(flags&EntityFlag_Block && !(flags&EntityFlag_Platform)){
// One of the entities is a block and none is a platform
Entity *ent,*ent_block;
Entity ent,ent_block;
float auxT,block_len;
vec2 auxN,p;
int applyFriction;
// Decide who is the block and who is the ent
if(b1->mass<=0.0f && b2->mass>0.0f){
ent=b2;
ent_block=b1;
// Decide who is the platform and who is the ent
if(ent1->mass<=0.0f && ent2->mass>0.0f){
ent=ent2;
ent_block=ent1;
}else
if(b2->mass<=0.0f && b1->mass>0.0f){
ent=b1;
ent_block=b2;
if(ent2->mass<=0.0f && ent1->mass>0.0f){
ent=ent1;
ent_block=ent2;
}else{
// Two static or two dinamic entities?!?
return(0);
@@ -484,61 +502,153 @@ int Entity_Collide(Entity *b1,Entity *b2){
}
if(t<1.0f){
// Handle colision
int response=1;
int rc;
// Check the collision methods
if(ent->collision){
rc=ent->collision(ent,ent_block,t,n);
if (rc==0)
response=0;
if (rc>1)
response=2;
}
if(ent_block->collision){
vec2 n2;
vec2_scale(n2,n,-1.0f);
rc=ent_block->collision(ent_block,ent,t,n2);
if (rc==0)
response=0;
if (rc>1)
response=2;
}
// Collision response
if(response==1){
Entity_CollisionResponseLine(ent,ent_block,t,n,applyFriction);
return(1);
}
if (response==2) {
return(1);
}
return(0);
// Keep colision info
CollisionInfo_Add(collInfoRef,
CollisionResponse_Line,ent,ent_block,t,n,applyFriction);
return(1);
}
return(0);
}
// Test relative to ent1
vec2_minus(vel,ent1->vel,ent2->vel);
if(Colision_CircleCircle(ent1->pos,ent1->radius,vel,ent2->pos,ent2->radius,&t,n)){
// Keep colision info
CollisionInfo_Add(collInfoRef,
CollisionResponse_Circle,ent1,ent2,t,n,0);
return(1);
}
return(0);
}
// Test relative to b1
vec2_minus(vel,b1->vel,b2->vel);
if(Colision_CircleCircle(b1->pos,b1->radius,vel,b2->pos,b2->radius,&t,n)){
/////////////////////////////
// Entity_CollisionResponseCircle
//
// Normal response to a collision between circles.
void Entity_CollisionResponseCircle(
Entity b1,Entity b2,float t,vec2 n)
{
float moment;
vec2 temp;
float elast;
if(b1->mass>0.0f && b2->mass>0.0f){
// Calculate elasticity
elast=(b1->mass*b1->elast+b2->mass*b2->elast)/
(b1->mass+b2->mass);
// Collision between two massed balls
moment=((1.0f+elast)*b1->mass*b2->mass*
(fabs(vec2_dot(b1->vel,n))+fabs(vec2_dot(b2->vel,n))))
/(b1->mass+b2->mass);
vec2_scale(temp,n,moment/b1->mass);
vec2_minus(b1->vel,b1->vel,temp);
Entity_CalcBBox(b1);
vec2_scale(temp,n,moment/b2->mass);
vec2_plus(b2->vel,b2->vel,temp);
Entity_CalcBBox(b2);
}else
if(b1->mass>0.0f && b2->mass<=0.0f){
// Collision between a massed ball and a fixed ball
moment=(1.0f+b1->elast)*
(vec2_dot(b1->vel,n));
vec2_scale(temp,n,moment);
vec2_minus(b1->vel,b1->vel,temp);
Entity_CalcBBox(b1);
}else
if(b1->mass<=0.0f && b2->mass>0.0f){
// Collision between a massed ball and a fixed ball
// (imposible, but better safe)
moment=(1.0f+b2->elast)*
(vec2_dot(b2->vel,n));
vec2_scale(temp,n,moment);
vec2_plus(b2->vel,b2->vel,temp);
Entity_CalcBBox(b2);
}else{
// Collision between 2 fixed balls
// (imposible, but better safe)
vec2_set(b1->vel,0,0);
Entity_CalcBBox(b1);
vec2_set(b2->vel,0,0);
Entity_CalcBBox(b2);
}
}
/////////////////////////////
// Entity_CollisionResponseLine
//
// Normal response to a collision with a line.
void Entity_CollisionResponseLine(
Entity ent,Entity ent2,float t,vec2 norm,int applyFriction)
{
vec2 pos2,vel2,velFric,intersection;
float dist,fric_static,fric_dynamic,fricLen;
// Calculate friction
fric_static=(ent->fric_static+ent2->fric_static)/2;
fric_dynamic=(ent->fric_dynamic+ent2->fric_dynamic)/2;
// Calculate end position
vec2_scale(vel2,ent->vel,1.0f-t);
dist=-vec2_dot(norm,vel2);
vec2_plus(pos2,ent->pos,ent->vel);
vec2_scaleadd(pos2,pos2,norm,dist);
// Calculate intersection
vec2_scaleadd(intersection,ent->pos,ent->vel,t);
if(applyFriction){
// Apply friction
vec2_minus(velFric,pos2,intersection);
fricLen=sqrtf(vec2_dot(velFric,velFric));
if(fricLen<fric_static){
// Apply static friction
vec2_copy(pos2,intersection);
}else{
// Apply dynamic friction
if(fricLen>0.0f){
vec2_scaleadd(pos2,intersection,velFric,
1.0f-(fric_dynamic+(fric_static/fricLen)));
}else{
vec2_scaleadd(pos2,intersection,velFric,
1.0f-fric_dynamic);
}
}
}
// Apply to velocity
vec2_scaleadd(pos2,pos2,norm,0.1f);
vec2_minus(ent->vel,pos2,ent->pos);
Entity_CalcBBox(ent);
}
/////////////////////////////
// Entity_CollisionInfoResponse
//
//
int Entity_CollisionInfoResponse(CollisionInfo collInfo){
while(collInfo!=NULL){
// Handle colision
int response=1;
int rc;
vec2 n2;
vec2_scale(n2,n,-1.0f);
vec2_scale(n2,collInfo->n,-1.0f);
// Check the collision methods
if(b1->collision){
rc=b1->collision(b1,b2,t,n2);
if(collInfo->ent1->collision){
rc=collInfo->ent1->collision(collInfo->ent1,collInfo->ent2,collInfo->t,collInfo->n);
if (rc==0)
response=0;
if (rc>1)
response=2;
}
if(b2->collision){
rc=b2->collision(b2,b1,t,n);
if(collInfo->ent2->collision){
rc=collInfo->ent2->collision(collInfo->ent2,collInfo->ent1,collInfo->t,n2);
if (rc==0)
response=0;
if (rc>1)
@@ -547,17 +657,26 @@ int Entity_Collide(Entity *b1,Entity *b2){
// Collision response
if(response==1){
if(vec2_dot(b1->vel,b1->vel)>vec2_dot(b2->vel,b2->vel)){
Entity_CollisionResponseCircle(b1,b2,t,n);
}else{
Entity_CollisionResponseCircle(b2,b1,t,n);
if(collInfo->responseType==CollisionResponse_Line){
Entity_CollisionResponseLine(
collInfo->ent1,collInfo->ent2,collInfo->t,collInfo->n,collInfo->applyFriction);
}else
if(collInfo->responseType==CollisionResponse_Circle){
if(vec2_dot(collInfo->ent1->vel,collInfo->ent1->vel)>
vec2_dot(collInfo->ent2->vel,collInfo->ent2->vel))
{
Entity_CollisionResponseCircle(collInfo->ent1,collInfo->ent2,collInfo->t,n2);
}else{
Entity_CollisionResponseCircle(collInfo->ent2,collInfo->ent1,collInfo->t,collInfo->n);
}
}
return(1);
}
if (response==2) {
return(1);
}
return(0);
collInfo=collInfo->next;
}
return(0);
}
@@ -567,7 +686,7 @@ int Entity_Collide(Entity *b1,Entity *b2){
// Entity_Overlaps
//
//
void Entity_Overlaps(Entity *b1,Entity *b2){
void Entity_Overlaps(Entity b1,Entity b2){
vec2 len;
vec2_minus(len,b1->pos,b2->pos);
@@ -594,7 +713,7 @@ void Entity_Overlaps(Entity *b1,Entity *b2){
// Entity_GetPos
//
//
void Entity_GetPos(Entity *e,vec2 pos){
void Entity_GetPos(Entity e,vec2 pos){
vec2_copy(pos,e->pos);
}
@@ -602,7 +721,7 @@ void Entity_GetPos(Entity *e,vec2 pos){
// Entity_UpdatePos
//
//
void Entity_UpdatePos(Entity *e,vec2 pos){
void Entity_UpdatePos(Entity e,vec2 pos){
// Mark the update of the position.
vec2_copy(e->oldpos,e->pos);
@@ -615,7 +734,7 @@ void Entity_UpdatePos(Entity *e,vec2 pos){
// Entity_AddVelLimit
//
//
void Entity_AddVelLimit(Entity *e,vec2 vel,float limit){
void Entity_AddVelLimit(Entity e,vec2 vel,float limit){
float vlen_orig,vlen;
vec2 dir,vel_temp;
@@ -633,6 +752,7 @@ void Entity_AddVelLimit(Entity *e,vec2 vel,float limit){
vec2_scale(vel_temp,dir,vlen);
vec2_plus(e->vel,e->vel,vel_temp);
}
Entity_CalcBBox(e);
}
@@ -640,7 +760,7 @@ void Entity_AddVelLimit(Entity *e,vec2 vel,float limit){
// 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){
e->color[0]=r;
e->color[1]=g;
e->color[2]=b;
@@ -652,7 +772,7 @@ void Entity_SetColor(Entity *e,float r,float g,float b,float a){
// 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){
e->color[0]+=r;
if(e->color[0]>1.0f)
e->color[0]=1.0f;
@@ -672,7 +792,7 @@ void Entity_AddColor(Entity *e,float r,float g,float b,float a){
// Entity_SetLight
//
//
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){
e->light[0]=r;
e->light[1]=g;
e->light[2]=b;
@@ -685,7 +805,7 @@ void Entity_SetLight(Entity *e,float r,float g,float b,float rad){
// Entity_Iluminate
//
//
void Entity_Iluminate(Entity *e,Entity **elist,int n){
void Entity_Iluminate(Entity e,Entity *elist,int n){
int i;
vec2 vdist;
float qdist,f;
@@ -725,7 +845,7 @@ void Entity_Iluminate(Entity *e,Entity **elist,int n){
// Entity_MarkUpdateLight
//
//
void Entity_MarkUpdateLight(Entity *e,Entity **elist,int n){
void Entity_MarkUpdateLight(Entity e,Entity *elist,int n){
if(e->flags&EntityFlag_Light){
int i;
vec2 max,min;

View File

@@ -1,4 +1,4 @@
// Copyright (C) 2011 Valeriano Alfonso Rodriguez (Kableado)
// Copyright (C) 2011-2014 Valeriano Alfonso Rodriguez (Kableado)
#ifndef _ENTITY_H_
#define _ENTITY_H_
@@ -9,8 +9,7 @@
////////////////////////////////////////////////
// Entity //
////////////
// Entity
//
#define EntityFlag_Collision 1
#define EntityFlag_Platform 2
@@ -21,11 +20,13 @@
#define EntityFlag_Light 16
#define EntityFlag_UpdateLight 32
#define EntityFlag_UpdatedPos 64
typedef struct Tag_Entity {
struct Tag_Entity *base;
typedef struct TEntity TEntity, *Entity;
struct TEntity {
Entity base;
int type;
vec2 oldpos;
vec2 pos0;
vec2 pos;
int flags;
int zorder;
@@ -49,72 +50,134 @@ typedef struct Tag_Entity {
float color[4];
float light[4];
void (*oncopy)(struct Tag_Entity *ent);
void (*ondelete)(struct Tag_Entity *ent);
void (*proc)(struct Tag_Entity *ent,int ft);
void (*postproc)(struct Tag_Entity *ent,int ft);
int (*collision)(
struct Tag_Entity *ent,
struct Tag_Entity *ent2,
float t,vec2 n);
void (*overlap)(
struct Tag_Entity *ent,
struct Tag_Entity *ent2);
void (*oncopy)(Entity ent);
void (*ondelete)(Entity ent);
void (*proc)(Entity ent,int ft);
void (*postproc)(Entity ent,int ft);
int (*collision)(Entity ent, Entity ent2, float t,vec2 n);
void (*overlap)(Entity ent, Entity ent2);
int A;
int B;
int C;
int D;
struct Tag_Entity *child;
Entity child;
void *next;
} Entity;
float maxX,minX;
float maxY,minY;
Entity next;
};
/////////////////////////////
// Entity_New
//
//
Entity *Entity_New();
Entity Entity_New();
/////////////////////////////
// Entity_Destroy
//
//
void Entity_Destroy(Entity *e);
void Entity_Destroy(Entity e);
/////////////////////////////
// Entity_Copy
//
Entity Entity_Copy(Entity e);
/////////////////////////////
// Entity_CalcBBox
//
Entity *Entity_Copy(Entity *e);
//
void Entity_CalcBBox(Entity e);
/////////////////////////////
// Entity_BBoxIntersect
//
//
int Entity_BBoxIntersect(Entity ent1,Entity ent2);
/////////////////////////////
// Entity_Draw
//
//
void Entity_Draw(Entity *e,int x,int y);
void Entity_Draw(Entity e,int x,int y,float f);
/////////////////////////////
// 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
//
//
void Entity_Process(Entity *e,int ft);
void Entity_Process(Entity e,int ft);
/////////////////////////////
// Entity_PostProcess
//
void Entity_PostProcess(Entity e,int ft);
////////////////////////////////////////////////
// CollisionInfo
//
void Entity_PostProcess(Entity *e,int ft);
#define CollisionResponse_Circle 1
#define CollisionResponse_Line 2
typedef struct TCollisionInfo TCollisionInfo,*CollisionInfo;
struct TCollisionInfo {
int responseType;
Entity ent1;
Entity ent2;
float t;
vec2 n;
int applyFriction;
CollisionInfo next;
};
/////////////////////////////
// CollisionInfo_New
//
//
CollisionInfo CollisionInfo_New(int responseType,Entity ent1,Entity ent2,float t,vec2 n,int applyFriction);
/////////////////////////////
// CollisionInfo_Destroy
//
//
void CollisionInfo_Destroy(CollisionInfo *collInfoRef);
/////////////////////////////
// CollisionInfo_Add
//
//
void CollisionInfo_Add(CollisionInfo *collInfo,
int responseType,Entity ent1,Entity ent2,float t,vec2 n,int applyFriction);
/////////////////////////////
// CollisionInfo_CheckRepetition
//
//
int CollisionInfo_CheckRepetition(CollisionInfo collInfo,Entity ent1,Entity ent2);
/////////////////////////////
// Entity_CheckCollision
//
//
int Entity_CheckCollision(Entity ent1,Entity ent2,CollisionInfo *collInfoRef);
/////////////////////////////
@@ -122,7 +185,7 @@ void Entity_PostProcess(Entity *e,int ft);
//
// Normal response to a collision of spheres.
void Entity_CollisionResponseCircle(
Entity *b1,Entity *b2,float t,vec2 n);
Entity b1,Entity b2,float t,vec2 n);
/////////////////////////////
@@ -130,75 +193,69 @@ void Entity_CollisionResponseCircle(
//
// Normal response to a collision with a line.
void Entity_CollisionResponseLine(
Entity *ent,Entity *ent2,float t,vec2 n,int applyFriction);
Entity ent,Entity ent2,float t,vec2 n,int applyFriction);
/////////////////////////////
// Entity_Collide
// Entity_CollisionInfoResponse
//
//
int Entity_Collide(Entity *b1,Entity *b2);
int Entity_CollisionInfoResponse(CollisionInfo collInfo);
/////////////////////////////
// Entity_Overlaps
//
//
void Entity_Overlaps(Entity *b1,Entity *b2);
void Entity_Overlaps(Entity b1,Entity b2);
/////////////////////////////
// Entity_GetPos
//
//
void Entity_GetPos(Entity *e,vec2 pos);
void Entity_GetPos(Entity e,vec2 pos);
/////////////////////////////
// Entity_UpdatePos
//
//
void Entity_UpdatePos(Entity *e,vec2 pos);
void Entity_UpdatePos(Entity e,vec2 pos);
/////////////////////////////
// Entity_AddVelLimit
//
//
void Entity_AddVelLimit(Entity *e,vec2 vel,float limit);
void Entity_AddVelLimit(Entity e,vec2 vel,float limit);
/////////////////////////////
// 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
//
//
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_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_AddColor
//
//
void Entity_Iluminate(Entity *e,Entity **elist,int n);
void Entity_Iluminate(Entity e,Entity *elist,int n);
/////////////////////////////
// Entity_MarkUpdateLight
//
//
void Entity_MarkUpdateLight(Entity *e,Entity **elist,int n);
void Entity_MarkUpdateLight(Entity e,Entity *elist,int n);
#endif

View File

@@ -1,6 +1,9 @@
// Copyright (C) 2011 Valeriano Alfonso Rodriguez (Kableado)
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <SDL/SDL.h>
#include "Time.h"
@@ -15,7 +18,7 @@
// Globals
int _running;
Entity **_entity=NULL;
Entity *_entity=NULL;
int *_entity_flag=NULL;
int _n_entities=0;
int _n_entities_res=0;
@@ -23,11 +26,12 @@ int _entities_lock=0;
int _entities_compactate=0;
void (*_gameproc)()=NULL;
void (*_gamepostproc)()=NULL;
void (*_gamepredraw)()=NULL;
void (*_gamedraw)()=NULL;
void (*_gamepredraw)(float f)=NULL;
void (*_gamedraw)(float f)=NULL;
int _ft;
int _game_size[2];
int _game_pos[2];
int _game_pos0[2];
int _game_pos1[2];
long long t_proc;
long long t_col;
@@ -55,8 +59,10 @@ int GameLib_Init(int w,int h,char *title,int pfps,int fps){
_game_size[0]=w;
_game_size[1]=h;
_game_pos[0]=0;
_game_pos[1]=0;
_game_pos0[0]=0;
_game_pos0[1]=0;
_game_pos1[0]=0;
_game_pos1[1]=0;
_ft=1000/fps;
@@ -68,9 +74,9 @@ int GameLib_Init(int w,int h,char *title,int pfps,int fps){
// GameLib_AddEntity
//
// Adds an entity to the game.
void GameLib_AddEntity(Entity *e){
void GameLib_AddEntity(Entity e){
if(_n_entities>=_n_entities_res){
Entity **entity_aux;
Entity *entity_aux;
int *entity_flag_aux;
int i;
@@ -79,7 +85,7 @@ void GameLib_AddEntity(Entity *e){
_n_entities_res=32;
else
_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);
for(i=0;i<_n_entities;i++){
entity_aux[i]=_entity[i];
@@ -107,7 +113,7 @@ void GameLib_AddEntity(Entity *e){
// GameLib_UnrefEntity
//
// removes the reference to the entity.
int GameLib_UnrefEntity(Entity *e){
int GameLib_UnrefEntity(Entity e){
int i;
for(i=0;i<_n_entities;i++){
if(e==_entity[i]){
@@ -133,7 +139,7 @@ int GameLib_UnrefEntity(Entity *e){
// GameLib_DelEntity
//
// Adds an entity to the game.
int GameLib_DelEntity(Entity *e){
int GameLib_DelEntity(Entity e){
int i;
if((i=GameLib_UnrefEntity(e))==-1){
return(0);
@@ -177,6 +183,7 @@ void GameLib_Compactate(){
_entities_lock=0;
}
/////////////////////////////
// GameLib_ProcLoop
//
@@ -186,6 +193,9 @@ int GameLib_ProcLoop(){
int repeat,count;
long long time;
// Step the gamePosition
_game_pos0[0]=_game_pos1[0];
_game_pos0[0]=_game_pos1[0];
// Process
time=Time_GetTime();
@@ -201,26 +211,38 @@ int GameLib_ProcLoop(){
GameLib_Compactate();
t_proc+=Time_GetTime()-time;
// Colisions between entities
time=Time_GetTime();
GameLib_Compactate();_entities_lock=1;
count=0;
do{
repeat=0;
CollisionInfo collInfo=NULL;
for(i=0;i<_n_entities;i++){
if(!(_entity[i]->flags&EntityFlag_Collision) || _entity[i]->mass<0.0f)
continue;
if(_entity[i]->vel[0]<=0.0f && _entity[i]->vel[0]>=-0.0f &&
_entity[i]->vel[1]<=0.0f && _entity[i]->vel[1]>=-0.0f)
{
continue;
}
for(j=0;j<_n_entities;j++){
if(!(_entity[j]->flags&EntityFlag_Collision) || i==j)
if(!(_entity[j]->flags&EntityFlag_Collision) ||
!Entity_BBoxIntersect(_entity[i],_entity[j]) ||
CollisionInfo_CheckRepetition(collInfo,_entity[i],_entity[j]))
{
continue;
if(Entity_Collide(_entity[i],_entity[j])){
repeat=1;
}
Entity_CheckCollision(_entity[i],_entity[j],&collInfo);
}
}
if(Entity_CollisionInfoResponse(collInfo)){
repeat=1;
}
CollisionInfo_Destroy(&collInfo);
count++;
}while(repeat && count<10);
}while(repeat && count<50);
// Stop remaining collisions
if(count==10){
for(i=0;i<_n_entities;i++){
@@ -229,9 +251,11 @@ int GameLib_ProcLoop(){
for(j=0;j<_n_entities;j++){
if(!(_entity[j]->flags&EntityFlag_Collision) || i==j)
continue;
if(Entity_Collide(_entity[i],_entity[j])){
if(Entity_CheckCollision(_entity[i],_entity[j],NULL)){
vec2_set(_entity[i]->vel,0,0);
Entity_CalcBBox(_entity[i]);
vec2_set(_entity[j]->vel,0,0);
Entity_CalcBBox(_entity[j]);
}
}
}
@@ -274,7 +298,7 @@ int GameLib_ProcLoop(){
}
}
if(swap){
Entity *ent;
Entity ent;
ent=_entity[i];
_entity[i]=_entity[i-1];
_entity[i-1]=ent;
@@ -284,7 +308,6 @@ int GameLib_ProcLoop(){
n=n2;
}while(n>0);
// PostProcess
time=Time_GetTime();
GameLib_Compactate();_entities_lock=1;
@@ -310,20 +333,19 @@ int GameLib_ProcLoop(){
// GameLib_DrawLoop
//
//
void GameLib_DrawLoop(){
void GameLib_DrawLoop(float f){
long long time;
int i;
int game_pos[2];
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]);
time=Time_GetTime();
// Update Lights
//GameLib_UpdateIlumination();
// Predibujado
if(_gamepredraw){
_gamepredraw();
_gamepredraw(f);
}else{
// Limpiar pantalla
Draw_Clean(0,0,0);
@@ -332,11 +354,11 @@ void GameLib_DrawLoop(){
// Draw entities
GameLib_Compactate();_entities_lock=1;
for(i=0;i<_n_entities;i++){
Entity *e=_entity[i];
Entity e=_entity[i];
// Check visivility
if(!Entity_IsVisible(e,
_game_pos[0],_game_pos[1],
game_pos[0],game_pos[1],
_game_size[0],_game_size[1]))
{
continue;
@@ -348,18 +370,37 @@ void GameLib_DrawLoop(){
e->flags&=~EntityFlag_UpdateLight;
}
Entity_Draw(e,-_game_pos[0],-_game_pos[1]);
Entity_Draw(e,-game_pos[0],-game_pos[1],f);
}
Draw_SetColor(1,1,1,1);
if(_gamedraw){
_gamedraw();
_gamedraw(f);
}
GameLib_Compactate();
t_draw+=Time_GetTime()-time;
fdraw_count++;
if(Input_GetKey(InputKey_DumpProfiling)==InputKey_Pressed){
printf("Profiling:::::::::\n");
if(fproc_count>0){
printf("t_proc.....:%6lld\n",t_proc/fproc_count);
printf("t_col......:%6lld\n",t_col/fproc_count);
printf("t_over.....:%6lld\n",t_over/fproc_count);
printf("t_postproc.:%6lld\n",t_postproc/fproc_count);
}
if(fdraw_count>0){
printf("t_draw.....:%6lld\n",t_draw/fdraw_count);
}
t_proc=0;
t_col=0;
t_over=0;
t_postproc=0;
t_draw=0;
fproc_count=0;
fdraw_count=0;
}
}
@@ -370,8 +411,8 @@ void GameLib_DrawLoop(){
void GameLib_Loop(
void (*gameproc)(),
void (*gamepostproc)(),
void (*gamepredraw)(),
void (*gamedraw)())
void (*gamepredraw)(float f),
void (*gamedraw)(float f))
{
_running=1;
@@ -387,15 +428,6 @@ void GameLib_Loop(
fproc_count=0;
fdraw_count=0;
Draw_Loop(GameLib_ProcLoop,GameLib_DrawLoop);
if (gamelib_debug) {
printf("Profiling:::::::::\n");
printf("t_proc.....:%6lld\n",t_proc/fproc_count);
printf("t_col......:%6lld\n",t_col/fproc_count);
printf("t_over.....:%6lld\n",t_over/fproc_count);
printf("t_postproc.:%6lld\n",t_postproc/fproc_count);
printf("t_draw.....:%6lld\n",t_draw/fdraw_count);
}
}
@@ -411,16 +443,23 @@ void GameLib_BreakLoop(){
/////////////////////////////
// GameLib_GetPos
// GameLib_SetPos
// GameLib_UpdatePos
// GameLib_SetPos
//
//
void GameLib_GetPos(int pos[2]){
pos[0]=_game_pos[0];
pos[1]=_game_pos[1];
pos[0]=_game_pos1[0];
pos[1]=_game_pos1[1];
}
void GameLib_SetPos(int pos[2]){
_game_pos[0]=pos[0];
_game_pos[1]=pos[1];
_game_pos0[0]=pos[0];
_game_pos0[1]=pos[1];
_game_pos1[0]=pos[0];
_game_pos1[1]=pos[1];
}
void GameLib_UpdatePos(int pos[2]){
_game_pos1[0]=pos[0];
_game_pos1[1]=pos[1];
}
void GameLib_GetSize(int size[2]){
size[0]=_game_size[0];
@@ -441,10 +480,10 @@ void GameLib_MoveToPos(vec2 pos,float f){
GameLib_MoveToPosV(pos,f);
}
void GameLib_MoveToPosH(vec2 pos,float f){
_game_pos[0]=_game_pos[0]+(pos[0]-(_game_pos[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){
_game_pos[1]=_game_pos[1]+(pos[1]-(_game_pos[1]+(_game_size[1]/2.0f)))*f;
_game_pos1[1]=_game_pos1[1]+(pos[1]-(_game_pos1[1]+(_game_size[1]/2.0f)))*f;
}
@@ -470,7 +509,7 @@ void GameLib_DelEnts(){
// GameLib_ForEachEn
//
// Iterates every entity.
void GameLib_ForEachEnt(int (*func)(Entity *ent)){
void GameLib_ForEachEnt(int (*func)(Entity ent)){
int i;
for(i=0;i<_n_entities;i++){
if(!_entity[i])
@@ -486,9 +525,9 @@ void GameLib_ForEachEnt(int (*func)(Entity *ent)){
// GameLib_SearchEnt
//
// 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;
Entity *ent=NULL;
Entity ent=NULL;
for(i=0;i<_n_entities;i++){
if(!_entity[i])
continue;
@@ -500,6 +539,7 @@ Entity *GameLib_SearchEnt(int (*func)(Entity *ent,void *d),void *d){
return ent;
}
/////////////////////////////
// GameLib_PlaySound
//
@@ -509,8 +549,8 @@ void GameLib_PlaySound(AudioSnd snd,int x,int y){
int r,cx,cy,off;
// Get the screen context
cx=_game_pos[0]+_game_size[0]/2;
cy=_game_pos[1]+_game_size[1]/2;
cx=_game_pos1[0]+_game_size[0]/2;
cy=_game_pos1[1]+_game_size[1]/2;
if(_game_size[0]>_game_size[1]){
r=_game_size[0]/2;
}else{
@@ -558,7 +598,7 @@ void GameLib_Iluminate(){
// 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(e->flags&EntityFlag_Light){
Entity_MarkUpdateLight(e,_entity,_n_entities);
Entity_SetLight(e,r,g,b,rad);

View File

@@ -23,21 +23,21 @@ int GameLib_Init(int w,int h,char *title,int pfps,int fps);
// GameLib_AddEntity
//
// Adds an entity to the game.
void GameLib_AddEntity(Entity *e);
void GameLib_AddEntity(Entity e);
/////////////////////////////
// GameLib_UnrefEntity
//
// removes the reference to the entity.
int GameLib_UnrefEntity(Entity *e);
int GameLib_UnrefEntity(Entity e);
/////////////////////////////
// GameLib_DelEntity
//
// Adds an entity to the game.
int GameLib_DelEntity(Entity *e);
int GameLib_DelEntity(Entity e);
/////////////////////////////
@@ -47,8 +47,8 @@ int GameLib_DelEntity(Entity *e);
void GameLib_Loop(
void (*gameproc)(),
void (*gamepostproc)(),
void (*gamepredraw)(),
void (*gamedraw)());
void (*gamepredraw)(float f),
void (*gamedraw)(float f));
/////////////////////////////
@@ -91,14 +91,14 @@ void GameLib_DelEnts();
// GameLib_ForEachEnt
//
// Iterates every entity.
void GameLib_ForEachEnt(int (*func)(Entity *ent));
void GameLib_ForEachEnt(int (*func)(Entity ent));
/////////////////////////////
// GameLib_SearchEnt
//
// 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);
/////////////////////////////
@@ -119,7 +119,7 @@ void GameLib_Iluminate();
// 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);
/////////////////////////////

View File

@@ -57,7 +57,7 @@ void Input_Frame(){
Uint8* keys;
// Process Keys
keys=SDL_GetKeyState(NULL);
keys=(Uint8 *)SDL_GetKeyState(NULL);
Input_SetKey(InputKey_Action1,keys[SDLK_z]);
Input_SetKey(InputKey_Action2,keys[SDLK_x]);
Input_SetKey(InputKey_Up,keys[SDLK_UP]);
@@ -66,6 +66,8 @@ void Input_Frame(){
Input_SetKey(InputKey_Right,keys[SDLK_RIGHT]);
Input_SetKey(InputKey_Jump,keys[SDLK_SPACE]);
Input_SetKey(InputKey_Continue,keys[SDLK_RETURN]|keys[SDLK_KP_ENTER]);
Input_SetKey(InputKey_DumpProfiling,keys[SDLK_p]);
}

View File

@@ -33,6 +33,9 @@ typedef enum {
InputKey_Right,
InputKey_Jump,
InputKey_Continue,
InputKey_DumpProfiling,
InputKey_Max
} InputKey;

75
GameLib/QuadArray2D.c Normal file
View File

@@ -0,0 +1,75 @@
// Copyright (C) 2013 Valeriano Alfonso Rodriguez (Kableado)
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include "QuadArray2D.h"
QuadArray2D QuadArray2D_Create(int resVertex){
QuadArray2D quadArray=NULL;
quadArray=malloc(sizeof(TQuadArray2D));
quadArray->vertexData=malloc(sizeof(float)*Vertex2D_Length*resVertex);
quadArray->nVertex=0;
quadArray->resVertex=resVertex;
return quadArray;
}
void QuadArray2D_Destroy(QuadArray2D *quadArray){
if(!quadArray) return;
if(!quadArray[0]) return;
free(quadArray[0]->vertexData);
free(quadArray[0]);
quadArray[0]=NULL;
}
void QuadArray2D_Clean(QuadArray2D quadArray){
quadArray->nVertex=0;
}
void QuadArray2D_AddVertex(QuadArray2D quadArray,float v[]){
if(quadArray->resVertex<=quadArray->nVertex){
// Grow vertexData
quadArray->resVertex*=2;
float *newVertexData=malloc(sizeof(float)*Vertex2D_Length*
quadArray->resVertex);
memcpy(newVertexData,quadArray->vertexData,
sizeof(float)*Vertex2D_Length*quadArray->nVertex);
free(quadArray->vertexData);
quadArray->vertexData=newVertexData;
}
// Add the vertex
memcpy(
quadArray->vertexData+
(Vertex2D_Length*quadArray->nVertex),
v,sizeof(float)*Vertex2D_Length);
quadArray->nVertex++;
}
void QuadArray2D_AddQuad(QuadArray2D quadArray,
float x0, float y0,float u0, float v0,
float x1, float y1,float u1, float v1,
float color[])
{
float v[Vertex2D_Length];
int firstIndex=quadArray->nVertex;
// Set the color
v[4]=color[0];
v[5]=color[1];
v[6]=color[2];
v[7]=color[3];
// Add the vertexes
v[0]=x0; v[1]=y0; 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]=x0; v[1]=y1; v[2]=u0; v[3]=v1; QuadArray2D_AddVertex(quadArray,v);
}

35
GameLib/QuadArray2D.h Normal file
View File

@@ -0,0 +1,35 @@
// Copyright (C) 2013 Valeriano Alfonso Rodriguez (Kableado)
#ifndef _QUADARRAY2D_H_
#define _QUADARRAY2D_H_
// Vertex2D -> (x,y) (u,v) (r,g,b,a)
#define Vertex2D_Length 8
////////////////////////////////////////////////
// QuadArray2D
//
typedef struct TQuadArray2D TQuadArray2D, *QuadArray2D;
struct TQuadArray2D {
float *vertexData;
int nVertex;
int resVertex;
};
QuadArray2D QuadArray2D_Create(int resVertex);
void QuadArray2D_Destroy(QuadArray2D *quadArray);
void QuadArray2D_Clean(QuadArray2D quadArray);
void QuadArray2D_AddVertex(QuadArray2D quadArray,float v[]);
void QuadArray2D_AddQuad(QuadArray2D quadArray,
float x0, float y0,float u0, float v0,
float x1, float y1,float u1, float v1,
float color[]);
#endif

View File

@@ -1,6 +1,8 @@
// Copyright (C) 2011 Valeriano Alfonso Rodriguez (Kableado)
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include "Util.h"
@@ -120,10 +122,9 @@ int Colision_CircleCircle(
vec2_scale(cen_a,cir2,invrads);
if(Intersec_RayUnitCircle(orig_a,vel_a,cen_a,t)){
// Calculate n
vec2_scale(temp,vel,*t);
vec2_plus(temp,cir1,temp);
vec2_minus(temp,cir2,temp);
vec2_scale(n,temp,1.0f/rads);
vec2_scaleadd(temp,cir1,vel,*t);
vec2_minus(n,temp,cir2);
vec2_scale(n,n,invrads);
return(1);
}
return(0);
@@ -188,7 +189,6 @@ int absmod(int v,int d){
return(v%d);
}
}
float fabsmod(float v,int d){
if(v<0){
v+=d*((((int)(v/d))*(-1))+1);
@@ -200,4 +200,29 @@ float fabsmod(float v,int d){
}
/////////////////////////////
// IsBigEndian
//
int IsBigEndian(){
union{
unsigned int i;
char c[4];
} bint={0x01020304};
return bint.c[0]==1;
}
/////////////////////////////
// EndsWith
//
int EndsWith(char *str, char *suffix){
if (!str || !suffix)
return 0;
int lenStr = strlen(str);
int lenSuffix = strlen(suffix);
if (lenSuffix > lenStr)
return 0;
return strncmp(str+lenStr-lenSuffix, suffix, lenSuffix)==0;
}

View File

@@ -26,6 +26,9 @@ typedef float vec2[2];
#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);
#define vec2_interpol(v,v1,v2,f) \
(v)[0]=(v1)[0]-f*((v1)[0]-(v2)[0]);\
(v)[1]=(v1)[1]-f*((v1)[1]-(v2)[1]);
/////////////////////////////
@@ -34,6 +37,7 @@ float vec2_norm(vec2 v);
// Intersection between a ray and a Unit Circle.
int Intersec_RayUnitCircle(vec2 orig,vec2 vel,vec2 center,float *t);
/////////////////////////////
// Intersect_CircleCircle
//
@@ -43,6 +47,7 @@ int Colision_CircleCircle(
vec2 cb,float rb,
float *t,vec2 n);
/////////////////////////////
// Intersect_RayEdge
//
@@ -53,7 +58,6 @@ int Intersect_RayEdge(
float *t);
/////////////////////////////
// absmod
//
@@ -61,4 +65,16 @@ int absmod(int v,int d);
float fabsmod(float v,int d);
/////////////////////////////
// IsBigEndian
//
int IsBigEndian();
/////////////////////////////
// EndsWith
//
int EndsWith(char *str, char *suffix);
#endif

6285
GameLib/lodepng.c Normal file

File diff suppressed because it is too large Load Diff

1710
GameLib/lodepng.h Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -8,10 +8,11 @@
#include "GameMap.h"
Entity *GameMapAux_CreateEnt(Entity *ent,int i,int j){
Entity *e;
Entity GameMapAux_CreateEnt(Entity ent,int i,int j){
Entity e;
e=Entity_Copy(ent);
vec2_set(e->pos,16+i*32,16+j*32);
Entity_CalcBBox(e);
GameLib_AddEntity(e);
return(e);
}
@@ -39,10 +40,10 @@ void Aux_Linea(FILE *f,char *line){
}
int _startpoint;
int GameMapAux_CreatePlayer(Entity *ent){
int GameMapAux_CreatePlayer(Entity ent){
if(ent->type==Ent_SavePoint){
if(ent->A==_startpoint){
Entity *e;
Entity e;
e=Entity_Copy(ent_player);
vec2_copy(e->pos,ent->pos);
GameLib_AddEntity(e);
@@ -158,7 +159,7 @@ int GameMap_CreateLevel(int level,int point){
GameMapAux_CreateEnt(ent_hole_lava,i,j);
}else
if(line[i2]=='S'){
Entity *e;
Entity e;
// Save point
e=GameMapAux_CreateEnt(ent_savepoint,i,j);
e->A=line[i2+1]-'0';
@@ -189,12 +190,12 @@ int GameMap_CreateLevel(int level,int point){
}else
if(line[i2]=='T'){
// Teleporter
Entity *ent=GameMapAux_CreateEnt(ent_teleporter,i,j);
Entity ent=GameMapAux_CreateEnt(ent_teleporter,i,j);
ent->A=line[i2+1]-'0';
}else
if(line[i2]=='D'){
// Teleporter Destination
Entity *ent=GameMapAux_CreateEnt(ent_teleporter_dest,i,j);
Entity ent=GameMapAux_CreateEnt(ent_teleporter_dest,i,j);
ent->A=line[i2+1]-'0';
}else

View File

@@ -7,6 +7,7 @@ CFLAGS += -IGameLib
HEADS= \
GameLib/Time.h \
GameLib/Util.h \
GameLib/QuadArray2D.h \
GameLib/Draw.h \
GameLib/Input.h \
GameLib/Audio.h \
@@ -16,6 +17,7 @@ HEADS= \
OBJS= \
$(BUILDDIR)/GameLib/Time.o \
$(BUILDDIR)/GameLib/Util.o \
$(BUILDDIR)/GameLib/QuadArray2D.o \
$(BUILDDIR)/GameLib/Draw.o \
$(BUILDDIR)/GameLib/Input.o \
$(BUILDDIR)/GameLib/Audio.o \
@@ -24,7 +26,6 @@ OBJS= \
$(BUILDDIR)/GameLib/GameLib.o \
#####################
# Game Declarations #
#####################
@@ -36,11 +37,6 @@ OBJS+= \
$(BUILDDIR)/main.o
#################
# General Rules #
#################
@@ -54,11 +50,9 @@ clean:
rm -f $(OBJS) $(BUILDDIR)/$(RESULT)
run: $(BUILDDIR)/$(RESULT)
./$(BUILDDIR)/$(RESULT) debug
$(LAUNCHER) ./$(BUILDDIR)/$(RESULT) debug
rebuild: clean all
#################
# GameLib Rules #
@@ -67,6 +61,8 @@ $(BUILDDIR)/GameLib/Time.o: GameLib/Time.c $(HEADS)
$(CC) -c GameLib/Time.c -o $(BUILDDIR)/GameLib/Time.o $(CFLAGS)
$(BUILDDIR)/GameLib/Util.o: GameLib/Util.c $(HEADS)
$(CC) -c GameLib/Util.c -o $(BUILDDIR)/GameLib/Util.o $(CFLAGS)
$(BUILDDIR)/GameLib/QuadArray2D.o: GameLib/QuadArray2D.c $(HEADS)
$(CC) -c GameLib/QuadArray2D.c -o $(BUILDDIR)/GameLib/QuadArray2D.o $(CFLAGS)
$(BUILDDIR)/GameLib/Draw.o: GameLib/Draw.c $(HEADS)
$(CC) -c GameLib/Draw.c -o $(BUILDDIR)/GameLib/Draw.o $(CFLAGS)
$(BUILDDIR)/GameLib/Input.o: GameLib/Input.c $(HEADS)
@@ -81,7 +77,6 @@ $(BUILDDIR)/GameLib/GameLib.o: GameLib/GameLib.c $(HEADS)
$(CC) -c GameLib/GameLib.c -o $(BUILDDIR)/GameLib/GameLib.o $(CFLAGS)
##############
# Game Rules #
##############
@@ -95,8 +90,13 @@ $(BUILDDIR)/GameMap.o: GameMap.c $(HEADS)
$(BUILDDIR)/main.o: main.c $(HEADS)
$(CC) -c main.c -o $(BUILDDIR)/main.o $(CFLAGS)
################
# Result Rules #
################
$(BUILDDIR)/$(RESULT): $(OBJS)
$(CC) -o $(BUILDDIR)/$(RESULT) $(OBJS) $(LIBS) $(CFLAGS)
$(CC) -o $(BUILDDIR)/$(RESULT) $(OBJS) $(LIBS) $(CFLAGS) $(LDFLAGS)

14
Makefile.emscripten Normal file
View File

@@ -0,0 +1,14 @@
CC= python /c/Program\ Files/Emscripten/emscripten/1.7.8/emcc
LAUNCHER= start
RM=rm -rf
LIBS=
CFLAGS= -s LEGACY_GL_EMULATION=1 -s ASM_JS=1 -O1
LDFLAGS= --preload-file data
RESULT=game.html
BUILDDIR=build-emscripten
include Makefile.common

View File

@@ -1,7 +1,10 @@
CC=gcc
LAUNCHER=
RM=rm -rf
LIBS= -lSDL -lpthread -L/usr/X11R6/lib -L/usr/lib -lm -lGL -lX11
CFLAGS= -Wall -g -I/usr/include/ -I/usr/include/SDL/ -I/usr/X11R6/include/
CC=gcc
RM=rm -rf
LDFLAGS=
RESULT=game
BUILDDIR=build-linux

View File

@@ -1,7 +1,10 @@
CC= i486-mingw32-gcc
LAUNCHER=
RM=rm -rf
LIBS= -L/usr/i486-mingw/lib -D_GNU_SOURCE=1 -Dmain=SDL_main -lopengl32
CFLAGS= -I/usr/i486-mingw/include -lmingw32 -lSDLmain -lSDL -mwindows
CC= i486-mingw32-gcc
RM=rm -rf
LDFLAGS=
RESULT=game.exe
BUILDDIR=build-mingw

View File

@@ -1,7 +1,10 @@
CC=gcc
LAUNCHER=
RM=rm -rf
LIBS=-I/mingw/include/SDL -D_GNU_SOURCE=1 -Dmain=SDL_main -lopengl32
CFLAGS= -L/mingw/lib -lmingw32 -lSDLmain -lSDL -mwindows -g
CC=gcc
RM=rm -rf
LDFLAGS=
RESULT=game.exe
BUILDDIR=build-mingw

2
dev-server.bat Normal file
View File

@@ -0,0 +1,2 @@
php -S 0.0.0.0:80 -tbuild-emscripten

View File

@@ -1,7 +1,8 @@
#!/bin/sh
DATE=$(date +%Y%m%d)
DIRNAME="Lonely_Ruins"
DATE=$(date +%Y%m%d)
ZIPNAME="$DIRNAME.$DATE.zip"
make -f Makefile.linux

BIN
game.save

Binary file not shown.

18
main.c
View File

@@ -85,7 +85,6 @@ void ProcGame(){
}
void PostProcGame(){
if(game_level_reset){
if(Input_AnyKey()){
if(GameMap_CreateLevel(game_level,game_level_point)){
@@ -102,7 +101,7 @@ void PostProcGame(){
}
}
void DrawGame(){
void DrawGame(float f){
char string[1024];
Draw_SetColor(1.0f,1.0f,1.0f,1.0f);
@@ -176,15 +175,15 @@ int main(int argc,char *argv[]){
GameLib_Init(640,480,"Game",60,60);
img_logo=Draw_LoadImage("data/logo.bmp");
img_end=Draw_LoadImage("data/end.bmp");
img_logo=Draw_LoadImage("data/logo.png");
img_end=Draw_LoadImage("data/end.png");
font=Draw_DefaultFont(255,255,255,255);
GameEnts_Init();
LoadGame("game.save");
#ifndef EMSCRIPTEN
do{
play=0;
Draw_Loop(ProcTitle,DrawTitle);
@@ -208,6 +207,15 @@ int main(int argc,char *argv[]){
SaveGame("game.save");
#else
int pos[2]={0,0};
GameLib_SetPos(pos);
game_level=0;
game_level_point=1;
game_level_reset=0;
GameMap_CreateLevel(game_level,game_level_point);
GameLib_Loop(ProcGame,PostProcGame,NULL,DrawGame);
#endif
return(0);
}