(20111215) 12:00

This commit is contained in:
2011-12-15 12:00:00 +01:00
parent 8a84d0a0f5
commit 275f28d211
38 changed files with 875 additions and 202 deletions

3
Anim.c Normal file
View File

@@ -0,0 +1,3 @@
#include "Anim.h"

5
Anim.h Normal file
View File

@@ -0,0 +1,5 @@
#ifndef _ANIM_H_
#define _ANIM_H_
#endif

120
Draw.c
View File

@@ -5,6 +5,7 @@
#include <SDL/SDL.h>
#include "Time.h"
#include "Util.h"
#include "Draw.h"
#include "Input.h"
#include "Audio.h"
@@ -149,29 +150,79 @@ void Draw_Clean(
SDL_MapRGB(_screen->format, r, g, b));
}
////////////////////////////////////////////////
// DrawImage //
///////////////
// Image container.
typedef struct Tag_DrawImage {
SDL_Surface *surf;
int x,y;
} DrawImage;
/////////////////////////////
// Draw_LoadImage
//
// Loads a image, giving a reference.
DrawImg Draw_LoadImage(char *filename){
SDL_Surface *img;
DrawImage *image;
SDL_Surface *surf;
// Load the BMP as a surface
img=SDL_LoadBMP(filename);
if(img == NULL){
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);
}
// FIX: Setting up the alpha channel.
if(img->format->BytesPerPixel==4){
img->format->Amask=0xFF000000;
img->format->Ashift=24;
if(surf->format->BytesPerPixel==4){
surf->format->Amask=0xFF000000;
surf->format->Ashift=24;
}
return((DrawImg *)img);
// Create the image container
image=malloc(sizeof(DrawImage));
image->surf=surf;
image->x=0;
image->y=0;
return((DrawImg)image);
}
/////////////////////////////
// Draw_GetSize
//
// Gets the image size.
void Draw_GetSize(DrawImg img,int *w,int *h){
DrawImage *image=img;
// Gets the image size
*w=image->surf->w;
*h=image->surf->h;
}
/////////////////////////////
// Draw_SetOffset
// Draw_GetOffset
//
// Sets and Gets the image offset.
void Draw_SetOffset(DrawImg img,int x,int y){
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;
// Gets the image offset
*x=image->x;
*y=image->y;
}
@@ -184,14 +235,11 @@ void Draw_ImgSetKeyCol(DrawImg img,
unsigned char g,
unsigned char b)
{
SDL_Surface *surf;
if(!img)
return;
DrawImage *image=img;
// Set the color key for the surface
surf=(SDL_Surface *)img;
SDL_SetColorKey(surf, SDL_SRCCOLORKEY,
SDL_MapRGB(surf->format, r, g, b));
SDL_SetColorKey(image->surf, SDL_SRCCOLORKEY,
SDL_MapRGB(image->surf->format, r, g, b));
}
@@ -200,58 +248,32 @@ void Draw_ImgSetKeyCol(DrawImg img,
//
// Setting the image alpha.
void Draw_ImgSetAlpha(DrawImg img, unsigned char a){
SDL_Surface *surf;
if(!img)
return;
DrawImage *image=img;
// Set the alpha for the surface
surf=(SDL_Surface *)img;
SDL_SetAlpha(surf, SDL_SRCALPHA, a);
SDL_SetAlpha(image->surf, SDL_SRCALPHA, a);
}
/////////////////////////////
// Draw_DrawImg
// Draw_DrawImgCenter
//
// Draws an image. And a centered variant
// Draws an image.
void Draw_DrawImg(DrawImg img,int x,int y){
SDL_Surface *surf;
DrawImage *image=img;
SDL_Rect orig;
SDL_Rect dest;
if(!img)
return;
// Prepare the rects
surf=(SDL_Surface *)img;
orig.x=0;
orig.y=0;
dest.x=x;
dest.y=y;
orig.w=dest.w=surf->w;
orig.h=dest.h=surf->h;
dest.x=x+image->x;
dest.y=y+image->y;
orig.w=dest.w=image->surf->w;
orig.h=dest.h=image->surf->h;
// Blit the surface on the screen
SDL_BlitSurface(surf,&orig,_screen,&dest);
}
void Draw_DrawImgCenter(DrawImg img,int x,int y){
SDL_Surface *surf;
SDL_Rect orig;
SDL_Rect dest;
if(!img)
return;
// Prepare the rects
surf=(SDL_Surface *)img;
orig.x=0;
orig.y=0;
dest.x=x-(surf->w/2);
dest.y=y-(surf->h/2);
orig.w=dest.w=surf->w;
orig.h=dest.h=surf->h;
// Blit the surface on the screen
SDL_BlitSurface(surf,&orig,_screen,&dest);
SDL_BlitSurface(image->surf,&orig,_screen,&dest);
}
@@ -324,8 +346,6 @@ void Draw_DrawText(DrawFnt f,char *text,int x,int y){
SDL_Rect orig;
SDL_Rect dest;
char *ptr;
if(!f)
return;
// Prepare the rects
orig.w=dest.w=font->w;

20
Draw.h
View File

@@ -40,6 +40,22 @@ typedef void *DrawImg;
DrawImg Draw_LoadImage(char *filename);
/////////////////////////////
// Draw_GetSize
//
// Gets the image size.
void Draw_GetSize(DrawImg img,int *w,int *h);
/////////////////////////////
// Draw_SetOffset
// Draw_GetOffset
//
// Sets and Gets the image offset.
void Draw_SetOffset(DrawImg img,int x,int y);
void Draw_GetOffset(DrawImg img,int *x,int *y);
/////////////////////////////
// Draw_ImgSetKeyCol
//
@@ -59,11 +75,9 @@ void Draw_ImgSetAlpha(DrawImg img, unsigned char a);
/////////////////////////////
// Draw_DrawImg
// Draw_DrawImgCenter
//
// Draws an image. And a centered variant
// Draws an image.
void Draw_DrawImg(DrawImg img,int x,int y);
void Draw_DrawImgCenter(DrawImg img,int x,int y);
////////////////////////////////////////////////

230
Entity.c
View File

@@ -1,37 +1,225 @@
#ifndef _ENTITY_H_
#define _ENTITY_H_
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "Util.h"
#include "Draw.h"
////////////////////////////////////////////////
// Entity //
////////////
// Reference to an entity.
typedef void *Entity;
#include "Entity.h"
/////////////////////////////
// Entity_Create
// Entity_New
//
// Creates an entity.
Entity Entity_Create(
int x,int y,
void (*process)(Entity ent,void *data),
void *data);
//
Entity *Entity_New(){
Entity *e;
e=malloc(sizeof(Entity));
e->type=0;
vec2_set(e->pos,0.0f,0.0f);
vec2_set(e->vel,0.0f,0.0f);
e->radius=1.0f;
e->mass=1.0f;
e->elast=0.0f;
e->fric_static=1.0f;
e->fric_dynamic=0.0f;
e->img=NULL;
e->proc=NULL;
e->postproc=NULL;
return(e);
}
/////////////////////////////
// Entity_Create
// Entity_Destroy
//
// Creates an entity.
//
void Entity_Destroy(Entity *e){
free(e);
}
/////////////////////////////
// Entity_CreateClass
// Entity_Copy
//
// Initializes the game.
void Entity_CreateClass(char *name,
void *(*createdata)(),
void (*process)(Entity ent,void *data));
//
Entity *Entity_Copy(Entity *e){
Entity *n;
n=Entity_New();
n->type=e->type;
vec2_set(n->pos,e->pos[0],e->pos[1]);
vec2_set(n->vel,e->vel[0],e->vel[1]);
n->radius=e->radius;
n->mass=e->mass;
n->elast=e->elast;
n->fric_static=e->fric_static;
n->fric_dynamic=e->fric_dynamic;
n->img=e->img;
n->proc=e->proc;
n->postproc=e->postproc;
return(n);
}
/////////////////////////////
// Entity_Draw
//
//
void Entity_Draw(Entity *e){
if(e->img)
Draw_DrawImg(e->img,e->pos[0],e->pos[1]);
}
#endif
/////////////////////////////
// Entity_Process
//
//
void Entity_Process(Entity *b){
// Launch method
if(b->proc){
b->proc(b);
}
}
/////////////////////////////
// Entity_PostProcess
//
//
void Entity_PostProcess(Entity *e){
float qlen,len;
// Launch method
if(e->postproc){
e->postproc(e);
}
// Determine if there is movement
qlen=vec2_dot(e->vel,e->vel);
if(qlen>0.0f){
// Update position
vec2_plus(e->pos,e->pos,e->vel);
// Aply friction
len=sqrtf(qlen);
if(len<e->fric_static){
// Stopped by static friction
vec2_set(e->vel,0,0);
}else{
// Aply dynamic friction
vec2_scale(e->vel,e->vel,
1.0f-(e->fric_dynamic+(e->fric_static/len)));
}
}
}
/////////////////////////////
// Entity_Collide
//
//
int Entity_Collide(Entity *b1,Entity *b2){
float t;
vec2 n;
vec2 cir1[2];
vec2 cir1i,cir2i;
Entity *b_aux;
// FIX: Swap colision order based on moving object
if(vec2_dot(b1->vel,b1->vel)<vec2_dot(b2->vel,b2->vel)){
b_aux=b1;
b1=b2;
b2=b_aux;
}
// Prepare testing vectors
vec2_copy(cir1[0],b1->pos);
vec2_plus(cir1[1],b1->pos,b1->vel);
vec2_minus(cir1[1],cir1[1],b2->vel);
if(Colision_CircleCircle(cir1,b1->radius,b2->pos,b2->radius,&t,n)){
float moment;
vec2 temp,temp2;
float elast;
// Collision response
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*
(vec2_dot(b1->vel,n)+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);
}else{
// Collision between 2 fixed balls
// (imposible, but better safe)
vec2_set(b1->vel,0,0);
vec2_set(b2->vel,0,0);
}
return(1);
}
return(0);
}
/////////////////////////////
// Entity_AddVelLimit
//
//
void Entity_AddVelLimit(Entity *e,vec2 vel,float limit){
float vlen_orig,vlen;
vec2 dir,vel_temp;
// Normalize vel getting vel
vlen_orig=sqrtf(vec2_dot(vel,vel));
vec2_scale(dir,vel,1.0f/vlen_orig);
// Limit velocity
vlen=vec2_dot(e->vel,dir);
if(vlen<limit){
vlen=limit-vlen;
if(vlen>vlen_orig){
vlen=vlen_orig;
}
vec2_scale(vel_temp,dir,vlen);
vec2_plus(e->vel,e->vel,vel_temp);
}
}

View File

@@ -1,6 +1,87 @@
#ifndef _ENTITY_H_
#define _ENTITY_H_
#include "Util.h"
#include "Draw.h"
////////////////////////////////////////////////
// Entity //
////////////
//
typedef struct Tag_Entity{
int type;
vec2 pos;
vec2 vel;
vec2 bod_offset;
float radius;
float mass;
float elast;
float fric_static;
float fric_dynamic;
DrawImg img;
void (*proc)(struct Tag_Entity *ent);
void (*postproc)(struct Tag_Entity *ent);
} Entity;
/////////////////////////////
// Entity_New
//
//
Entity *Entity_New();
/////////////////////////////
// Entity_Destroy
//
//
void Entity_Destroy(Entity *e);
/////////////////////////////
// Entity_Copy
//
//
Entity *Entity_Copy(Entity *e);
/////////////////////////////
// Entity_Draw
//
//
void Entity_Draw(Entity *e);
/////////////////////////////
// Entity_Process
//
//
void Entity_Process(Entity *e);
/////////////////////////////
// Entity_PostProcess
//
//
void Entity_PostProcess(Entity *e);
/////////////////////////////
// Entity_Collide
//
//
int Entity_Collide(Entity *b1,Entity *b2);
/////////////////////////////
// Entity_AddVelLimit
//
//
void Entity_AddVelLimit(Entity *e,vec2 vel,float limit);
#endif

167
GameLib.c
View File

@@ -1,11 +1,23 @@
#include <SDL/SDL.h>
#include "Time.h"
#include "Util.h"
#include "Draw.h"
#include "Input.h"
#include "Audio.h"
#include "Anim.h"
#include "Entity.h"
#include "GameLib.h"
// Globals
int _running;
Entity **_entity=NULL;
int _n_entities=0;
int _n_entities_res=0;
void (*_gameproc)()=NULL;
void (*_gamepostproc)()=NULL;
/////////////////////////////
// GameLib_Init
@@ -23,4 +35,159 @@ int GameLib_Init(int w,int h,char *title,int fps){
}
/////////////////////////////
// GameLib_AddEntity
//
// Adds an entity to the game.
void GameLib_AddEntity(Entity *e){
if(_n_entities>=_n_entities_res){
Entity **entity_aux;
int i;
// Grow the array
if(_n_entities_res==0)
_n_entities_res=32;
else
_n_entities_res*=2;
entity_aux=malloc(sizeof(Entity *)*_n_entities_res);
for(i=0;i<_n_entities;i++)
entity_aux[i]=_entity[i];
if(_entity)
free(_entity);
_entity=entity_aux;
}
// Add the entity
_entity[_n_entities]=e;
_n_entities++;
}
/////////////////////////////
// GameLib_UnrefEntity
//
// removes the reference to the entity.
int GameLib_UnrefEntity(Entity *e){
int i;
for(i=0;i<_n_entities;i++){
if(e==_entity[i]){
_entity[i]=NULL;
return(1);
}
}
return(0);
}
/////////////////////////////
// GameLib_DelEntity
//
// Adds an entity to the game.
int GameLib_DelEntity(Entity *e){
if(!GameLib_UnrefEntity(e)){
return(0);
}
Entity_Destroy(e);
return(1);
}
/////////////////////////////
// GameLib_ProcLoop
//
// Process the loop.
int GameLib_ProcLoop(){
int i,j;
int repeat,count;
// Launch the method
if(_gameproc){
_gameproc();
}
// Process entities
for(i=0;i<_n_entities;i++){
if(!_entity[i])
continue;
Entity_Process(_entity[i]);
}
// Process colisions between entities
count=0;
do{
repeat=0;
for(i=0;i<_n_entities-1;i++){
for(j=i+1;j<_n_entities;j++){
if(!_entity[i] || !_entity[j])
continue;
if(Entity_Collide(_entity[i],_entity[j])){
repeat=1;
}
}
}
count++;
}while(repeat && count<10);
// Stop remaining collisions
for(i=0;i<_n_entities-1;i++){
for(j=i+1;j<_n_entities;j++){
if(!_entity[i] || !_entity[j])
continue;
if(Entity_Collide(_entity[i],_entity[j])){
vec2_set(_entity[i]->vel,0,0);
vec2_set(_entity[j]->vel,0,0);
}
}
}
// PostProcess and draw entities
for(i=0;i<_n_entities;i++){
if(!_entity[i])
continue;
Entity_PostProcess(_entity[i]);
Entity_Draw(_entity[i]);
}
// Compactate
j=0;
for(i=0;i<_n_entities;i++){
if(!_entity[i])
continue;
if(i>j)
_entity[j]=_entity[i];
j++;
}
_n_entities=j;
// Launch the method
if(_gamepostproc){
_gamepostproc();
}
return(_running);
}
/////////////////////////////
// GameLib_Loop
//
// Loops the game.
void GameLib_Loop(
void (*gameproc)(),
void (*gamepostproc)())
{
_running=1;
_gameproc=gameproc;
_gamepostproc=gamepostproc;
Draw_Loop(GameLib_ProcLoop);
}
/////////////////////////////
// GameLib_BreakLoop
//
// Breaks the game loop.
void GameLib_BreakLoop(){
_running=0;
}

View File

@@ -1,9 +1,12 @@
#ifndef _GAMELIB_H_
#define _GAMELIB_H_
#include "Time.h"
#include "Util.h"
#include "Draw.h"
#include "Input.h"
#include "Audio.h"
#include "Anim.h"
#include "Entity.h"
@@ -14,11 +17,34 @@
int GameLib_Init(int w,int h,char *title,int fps);
/////////////////////////////
// GameLib_AddEntity
//
// Adds an entity to the game.
void GameLib_AddEntity(Entity *e);
/////////////////////////////
// GameLib_UnrefEntity
//
// removes the reference to the entity.
int GameLib_UnrefEntity(Entity *e);
/////////////////////////////
// GameLib_DelEntity
//
// Adds an entity to the game.
int GameLib_DelEntity(Entity *e);
/////////////////////////////
// GameLib_Loop
//
// Loops the game.
void GameLib_Loop();
void GameLib_Loop(
void (*gameproc)(),
void (*gamepostproc)());
/////////////////////////////

33
Input.c
View File

@@ -1,5 +1,7 @@
#include <math.h>
#include <SDL/SDL.h>
#include "Util.h"
#include "Input.h"
@@ -66,3 +68,34 @@ void Input_SetKey(InputKey key,int status){
InputKeyStatus Input_GetKey(InputKey key){
return(_keys[key]);
}
/////////////////////////////
// Input_GetDir
//
// Reports the direction of the dpad.
int Input_GetDir(vec2 dir){
float vlen;
vec2_set(dir,0.0f,0.0f);
if(Input_GetKey(InputKey_Up) ){
dir[1]-=1.0f;
}
if(Input_GetKey(InputKey_Down) ){
dir[1]+=1.0f;
}
if(Input_GetKey(InputKey_Left) ){
dir[0]-=1.0f;
}
if(Input_GetKey(InputKey_Right) ){
dir[0]+=1.0f;
}
vlen=vec2_dot(dir,dir);
if(vlen>0.0f){
vlen=sqrtf(vlen);
vec2_scale(dir,dir,1.0f/vlen);
return(1);
}else{
return(0);
}
}

View File

@@ -1,6 +1,8 @@
#ifndef _INPUT_H_
#define _INPUT_H_
#include "Util.h"
/////////////////////////////
// Input_Init
@@ -57,5 +59,11 @@ typedef enum {
InputKeyStatus Input_GetKey(InputKey key);
/////////////////////////////
// Input_GetDir
//
// Reports the direction of the dpad.
int Input_GetDir(vec2 dir);
#endif

View File

@@ -14,7 +14,7 @@ endif # windir
ifeq ($(TARGET_ARCH),mingw)
include Makefile.mingw
include Makefile.win32
else
ifeq ($(TARGET_ARCH),linux)
include Makefile.linux

View File

@@ -1,15 +1,20 @@
HEADS= \
Time.h \
Util.h \
Draw.h \
Input.h \
Audio.h \
Anim.h \
Entity.h \
GameLib.h
OBJS= \
$(BUILDDIR)/Time.o \
$(BUILDDIR)/Util.o \
$(BUILDDIR)/Draw.o \
$(BUILDDIR)/Input.o \
$(BUILDDIR)/Audio.o \
$(BUILDDIR)/Anim.o \
$(BUILDDIR)/Entity.o \
$(BUILDDIR)/GameLib.o \
$(BUILDDIR)/main.o
@@ -25,6 +30,9 @@ $(BUILDDIR):
$(BUILDDIR)/Time.o: Time.c $(HEADS)
$(CC) -c Time.c -o $(BUILDDIR)/Time.o $(CFLAGS)
$(BUILDDIR)/Util.o: Util.c $(HEADS)
$(CC) -c Util.c -o $(BUILDDIR)/Util.o $(CFLAGS)
$(BUILDDIR)/Draw.o: Draw.c $(HEADS)
$(CC) -c Draw.c -o $(BUILDDIR)/Draw.o $(CFLAGS)
@@ -37,6 +45,9 @@ $(BUILDDIR)/Audio.o: Audio.c $(HEADS)
$(BUILDDIR)/Entity.o: Entity.c $(HEADS)
$(CC) -c Entity.c -o $(BUILDDIR)/Entity.o $(CFLAGS)
$(BUILDDIR)/Anim.o: Anim.c $(HEADS)
$(CC) -c Anim.c -o $(BUILDDIR)/Anim.o $(CFLAGS)
$(BUILDDIR)/GameLib.o: GameLib.c $(HEADS)
$(CC) -c GameLib.c -o $(BUILDDIR)/GameLib.o $(CFLAGS)

View File

@@ -1,4 +1,4 @@
LIBS= -lSDL -lpthread -L/usr/X11R6/lib -L/usr/lib
LIBS= -lSDL -lpthread -L/usr/X11R6/lib -L/usr/lib -lm
CFLAGS= -Wall -g -I/usr/include/ -I/usr/include/SDL/ -I/usr/X11R6/include/
CC=gcc
RM=rm -rf

11
Makefile.win32 Normal file
View File

@@ -0,0 +1,11 @@
LIBS=-I/mingw/include/SDL -D_GNU_SOURCE=1 -Dmain=SDL_main
CFLAGS= -L/mingw/lib -lmingw32 -lSDLmain -lSDL -mwindows -g
CC=gcc
RM=rm -rf
RESULT=game.exe
BUILDDIR=build-mingw
include Makefile.common

93
Util.c Normal file
View File

@@ -0,0 +1,93 @@
#include <math.h>
#include "Util.h"
/////////////////////////////
// SolveQuadratic
//
// Solves a Quadratic equation using a, b and c coeficients.
int SolveQuadratic(float a,float b,float c,float *Rmin,float *Rmax){
float root;
float divisor;
float b2;
b2=b*b;
root=b2-4.0*a*c;
if(root<0){
// Complex
return(0);
}
divisor=(2.0*a);
if(fabs(divisor)==0.0f){
// +inf -inf
return(0);
}
root=sqrtf(root);
Rmin[0]=(float)((-b-root)/divisor);
Rmax[0]=(float)((-b+root)/divisor);
return(1);
}
/////////////////////////////
// Intersec_RayUnitCircle
//
// Intersection between a ray and a Unit Circle.
int Intersec_RayUnitCircle(vec2 orig,vec2 vel,vec2 center,float *t){
float a,b,c;
float Rmin,Rmax;
vec2 temp;
// Solve as a unit circle
a=vec2_dot(vel,vel);
if(fabs(a)<0.000001f){
return(0);
}
vec2_minus(temp,orig,center);
b=2.0f*vec2_dot(temp,vel);
c=vec2_dot(temp,temp)-1.0f;
if(SolveQuadratic(a,b,c,&Rmin,&Rmax)){
if(Rmin>=-0.0001f && Rmin<Rmax && Rmin<=1.0f){
*t=Rmin;
return(1);
}
if(Rmax>=-0.0001f && Rmin>Rmax && Rmax<=1.0f){
*t=Rmax;
return(1);
}
}
return(0);
}
/////////////////////////////
// Colision_CircleCircle
//
// Colision point of a circle against another circle.
int Colision_CircleCircle(
vec2 cir1[2],float rad1,
vec2 cir2,float rad2,
float *t,vec2 n)
{
vec2 vel,orig,cen,temp;
float rads,invrads;
// Convert to a unit circle vs ray
rads=rad1+rad2;
invrads=1.0f/rads;
vec2_minus(vel,cir1[1],cir1[0]);
vec2_scale(vel,vel,invrads);
vec2_scale(orig,cir1[0],invrads);
vec2_scale(cen,cir2,invrads);
if(Intersec_RayUnitCircle(orig,vel,cen,t)){
// Calculate n
vec2_minus(vel,cir1[1],cir1[0]);
vec2_scale(temp,vel,*t);
vec2_plus(temp,cir1[0],temp);
vec2_minus(temp,cir2,temp);
vec2_scale(n,temp,1.0f/rads);
return(1);
}
return(0);
}

44
Util.h Normal file
View File

@@ -0,0 +1,44 @@
#ifndef _UTIL_H_
#define _UTIL_H_
/////////////////////////////
// SolveQuadratic
//
// Solves a Quadratic equation using a, b and c coeficients.
int SolveQuadratic(float a,float b,float c,float *Rmin,float *Rmax);
////////////////////////////////////////////////
// vec2 //
//////////
// A 2D vector.
typedef float vec2[2];
#define vec2_set(v,x,y) (v)[0]=(x);(v)[1]=(y);
#define vec2_copy(v1,v2) (v1)[0]=(v2)[0];(v1)[1]=(v2)[1];
#define vec2_plus(v,v1,v2) (v)[0]=(v1)[0]+(v2)[0];(v)[1]=(v1)[1]+(v2)[1];
#define vec2_minus(v,v1,v2) (v)[0]=(v1)[0]-(v2)[0];(v)[1]=(v1)[1]-(v2)[1];
#define vec2_scale(v,v1,s) (v)[0]=(v1)[0]*(s);(v)[1]=(v1)[1]*(s);
#define vec2_dot(v1,v2) ((v1)[0]*(v2)[0]+(v1)[1]*(v2)[1])
/////////////////////////////
// Intersec_RayUnitCircle
//
// Intersection between a ray and a Unit Circle.
int Intersec_RayUnitCircle(vec2 orig,vec2 vel,vec2 center,float *t);
/////////////////////////////
// Intersect_CircleCircle
//
// Colision point of a circle against another circle.
int Colision_CircleCircle(
vec2 ca[2],float ra,
vec2 cb,float rb,
float *t,vec2 n);
#endif

Binary file not shown.

Before

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.1 KiB

BIN
data/bisho2_alpha.bmp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.1 KiB

BIN
data/bloque.bmp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.1 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 16 KiB

219
main.c
View File

@@ -1,114 +1,45 @@
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "GameLib.h"
#define MAP_ANCHO 11
#define MAP_ALTO 9
DrawImg tiles[20];
int mapa[MAP_ANCHO][MAP_ALTO];
// Dibujar el mapa
void DrawMapa(){
int x;
int y;
int value;
// dibujar el entorno
for(x=0;x<MAP_ANCHO-1;x++){
for(y=0;y<MAP_ALTO-1;y++){
value = mapa[x ][y ] * 8;
value += mapa[x+1][y ] * 4;
value += mapa[x ][y+1] * 2;
value += mapa[x+1][y+1] * 1;
Draw_DrawImg(tiles[value], x*64, y*64);
}
}
}
// Crear un mapa aleatorio
int CrearMapa(){
int x;
int y;
double rnd;
for(x=0;x<MAP_ANCHO;x++){
for(y=0;y<MAP_ALTO;y++){
rnd=rand();
rnd=rnd/RAND_MAX;
if(rnd>0.7){
mapa[x][y]=0;
}else{
mapa[x][y]=1;
}
}
}
return 0;
}
DrawFnt font;
DrawFnt fonts;
DrawImg bisho;
float xacel,yacel;
int xpos=0,ypos=0;
DrawFnt font_shad;
DrawImg img_bisho;
DrawImg img_bisho2;
DrawImg img_bloque;
AudioSnd coin;
int t_start,t_end;
int frames=0;
int ProcGame(){
int i;
//Draw_Clean(0,0,0);
DrawMapa();
if(Input_GetKey(InputKey_Up) && yacel>-10){
yacel-=2;
}
if(Input_GetKey(InputKey_Down) && yacel<10){
yacel+=2;
}
if(Input_GetKey(InputKey_Left) && xacel>-10){
xacel-=2;
}
if(Input_GetKey(InputKey_Right) && xacel<10){
xacel+=2;
}
if(xacel!=0){
xpos+=xacel;
if(xacel>0){
xacel-=1;
}else{
xacel+=1;
}
}
if(yacel!=0){
ypos+=yacel;
if(yacel>0){
yacel-=1;
}else{
yacel+=1;
}
}
Draw_DrawImgCenter(bisho,xpos,ypos);
Draw_DrawText(fonts,"BISHO!",xpos-15,ypos-23);
Draw_DrawText(font ,"BISHO!",xpos-16,ypos-24);
Entity *bisho;
Entity *bisho2;
Entity *bloque;
void ProcGame(){
Draw_Clean(0,0,0);
}
void PostProcGame(){
if(Input_GetKey(InputKey_Action1)==InputKey_Pressed){
Audio_PlaySound(coin,1,1);
}
/*for(i=0;i<1000;i++){
Draw_DrawImgCenter(bisho,rand()%640,rand()%480);
}*/
Draw_DrawText(fonts,"Hola Mundo!",11,11);
Draw_DrawText(font_shad,"Hola Mundo!",11,11);
Draw_DrawText(font ,"Hola Mundo!",10,10);
frames++;
}
return(1);
void player_proc(Entity *e){
vec2 vel;
if(Input_GetDir(vel)){
vec2_scale(vel,vel,2.0f);
Entity_AddVelLimit(e,vel,10.0f);
}
}
int main(int argc,char *argv[]){
@@ -117,46 +48,84 @@ int main(int argc,char *argv[]){
GameLib_Init(640,480,"Game",60);
tiles[0] = Draw_LoadImage("data/tile0000.bmp");
tiles[1] = Draw_LoadImage("data/tile0001.bmp");
tiles[2] = Draw_LoadImage("data/tile0010.bmp");
tiles[3] = Draw_LoadImage("data/tile0011.bmp");
tiles[4] = Draw_LoadImage("data/tile0100.bmp");
tiles[5] = Draw_LoadImage("data/tile0101.bmp");
tiles[6] = Draw_LoadImage("data/tile0110.bmp");
tiles[7] = Draw_LoadImage("data/tile0111.bmp");
tiles[8] = Draw_LoadImage("data/tile1000.bmp");
tiles[9] = Draw_LoadImage("data/tile1001.bmp");
tiles[10] = Draw_LoadImage("data/tile1010.bmp");
tiles[11] = Draw_LoadImage("data/tile1011.bmp");
tiles[12] = Draw_LoadImage("data/tile1100.bmp");
tiles[13] = Draw_LoadImage("data/tile1101.bmp");
tiles[14] = Draw_LoadImage("data/tile1110.bmp");
tiles[15] = Draw_LoadImage("data/tile1111.bmp");
font=Draw_DefaultFont(255,255,255,255);
fonts=Draw_DefaultFont(0,0,0,127);
font_shad=Draw_DefaultFont(0,0,0,127);
//bisho=Draw_LoadImage("data/bisho.bmp");
//Draw_ImgSetKeyCol(bisho,0,255,0);
//Draw_ImgSetAlpha(bisho,127);
img_bisho=Draw_LoadImage("data/bisho_alpha.bmp");
Draw_ImgSetAlpha(img_bisho,255);
Draw_SetOffset(img_bisho,-16,-16);
bisho=Draw_LoadImage("data/bisho_alpha.bmp");
Draw_ImgSetAlpha(bisho,255);
xpos=320;
ypos=240;
img_bisho2=Draw_LoadImage("data/bisho2_alpha.bmp");
Draw_ImgSetAlpha(img_bisho2,255);
Draw_SetOffset(img_bisho2,-16,-16);
img_bloque=Draw_LoadImage("data/bloque.bmp");
Draw_ImgSetAlpha(img_bloque,255);
Draw_SetOffset(img_bloque,-16,-16);
coin=Audio_LoadSound("data/coin.wav");
CrearMapa();
bisho=Entity_New();
bisho->radius=16.0f;
bisho->img=img_bisho;
bisho->proc=player_proc;
bisho2=Entity_New();
bisho2->radius=16.0f;
bisho2->fric_static=0.1f;
bisho2->elast=0.0f;
bisho2->img=img_bisho2;
bloque=Entity_New();
bloque->mass=-1.0f;
bloque->radius=15.5f;
bloque->img=img_bloque;
int i;
Entity *e;
for(i=0;i<20;i++){
e=Entity_Copy(bloque);
vec2_set(e->pos,16+i*32,16);
GameLib_AddEntity(e);
}
for(i=0;i<20;i++){
e=Entity_Copy(bloque);
vec2_set(e->pos,16+i*32,464);
GameLib_AddEntity(e);
}
for(i=1;i<14;i++){
e=Entity_Copy(bloque);
vec2_set(e->pos,16,16+i*32);
GameLib_AddEntity(e);
}
for(i=1;i<14;i++){
e=Entity_Copy(bloque);
vec2_set(e->pos,624,16+i*32);
GameLib_AddEntity(e);
}
for(i=0;i<4;i++){
e=Entity_Copy(bloque);
vec2_set(e->pos,100,100+i*32);
GameLib_AddEntity(e);
}
for(i=0;i<4;i++){
e=Entity_Copy(bloque);
vec2_set(e->pos,164,100+i*32);
GameLib_AddEntity(e);
}
e=Entity_Copy(bisho2);
vec2_set(e->pos,132,100);
GameLib_AddEntity(e);
e=Entity_Copy(bisho);
vec2_set(e->pos,132,50);
GameLib_AddEntity(e);
GameLib_Loop(ProcGame,PostProcGame);
t_start=SDL_GetTicks();
Draw_Loop(ProcGame);
t_end=SDL_GetTicks();
printf("%d %d %.2f\n",t_end-t_start,frames,frames/((t_end-t_start)/1000.0f));
return(0);
}