(20111225) 04:20
This commit is contained in:
26
Draw.h
26
Draw.h
@@ -15,7 +15,7 @@ int Draw_Init(int width,int height,char *title,int fps);
|
||||
// Draw_Loop
|
||||
//
|
||||
// Loops updating the game window.
|
||||
void Draw_Loop(int (*proc)());
|
||||
void Draw_Loop(int (*proc)(),void (*draw)());
|
||||
|
||||
|
||||
/////////////////////////////
|
||||
@@ -58,23 +58,6 @@ void Draw_SetOffset(DrawImg img,int x,int y);
|
||||
void Draw_GetOffset(DrawImg img,int *x,int *y);
|
||||
|
||||
|
||||
/////////////////////////////
|
||||
// Draw_ImgSetKeyCol
|
||||
//
|
||||
// Setting the image color key.
|
||||
void Draw_ImgSetKeyCol(DrawImg img,
|
||||
unsigned char r,
|
||||
unsigned char g,
|
||||
unsigned char b);
|
||||
|
||||
|
||||
/////////////////////////////
|
||||
// Draw_ImgSetAlpha
|
||||
//
|
||||
// Setting the image alpha.
|
||||
void Draw_ImgSetAlpha(DrawImg img, unsigned char a);
|
||||
|
||||
|
||||
/////////////////////////////
|
||||
// Draw_DrawImg
|
||||
//
|
||||
@@ -89,13 +72,6 @@ void Draw_DrawImg(DrawImg img,int x,int y);
|
||||
void Draw_DrawImgPart(DrawImg img,int x,int y,int w,int i);
|
||||
|
||||
|
||||
/////////////////////////////
|
||||
// Draw_DrawImgTrans
|
||||
//
|
||||
// Draws an image transformed.
|
||||
void Draw_DrawImgTrans(DrawImg img,int x,int y,float angle);
|
||||
|
||||
|
||||
////////////////////////////////////////////////
|
||||
// DrawFnt //
|
||||
/////////////
|
||||
|
||||
551
DrawGL.c
Normal file
551
DrawGL.c
Normal file
@@ -0,0 +1,551 @@
|
||||
// Copyright (C) 2011 Valeriano Alfonso Rodriguez (Kableado)
|
||||
|
||||
#include <math.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#ifdef WIN32
|
||||
#define _WIN32_WINNT 0x0501
|
||||
#include <windows.h>
|
||||
#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>
|
||||
#else
|
||||
#include <GL/gl.h>
|
||||
#endif
|
||||
#endif
|
||||
#include <SDL/SDL.h>
|
||||
|
||||
#include "Time.h"
|
||||
#include "Util.h"
|
||||
#include "Draw.h"
|
||||
#include "Input.h"
|
||||
#include "Audio.h"
|
||||
|
||||
|
||||
// Globals
|
||||
SDL_Surface *_screen=NULL;
|
||||
int _width;
|
||||
int _height;
|
||||
long long _t_frame=17000;
|
||||
|
||||
/////////////////////////////
|
||||
// Draw_Init
|
||||
//
|
||||
// Initializes the game window.
|
||||
int Draw_Init(int width,int height,char *title,int fps){
|
||||
#ifdef WIN32
|
||||
// Stdout on the parent console
|
||||
AttachConsole(ATTACH_PARENT_PROCESS);
|
||||
if(GetStdHandle(STD_OUTPUT_HANDLE)!=0){
|
||||
fclose(stdin);
|
||||
fclose(stdout);
|
||||
fclose(stderr);
|
||||
freopen("CONIN$","r",stdin);
|
||||
freopen("CONOUT$","w",stdout);
|
||||
freopen("CONOUT$","w",stderr);
|
||||
}
|
||||
#endif
|
||||
|
||||
// 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());
|
||||
return(0);
|
||||
}
|
||||
|
||||
|
||||
// Prepare OpenGL inicialization
|
||||
SDL_GL_SetAttribute (SDL_GL_RED_SIZE, 8);
|
||||
SDL_GL_SetAttribute (SDL_GL_GREEN_SIZE, 8);
|
||||
SDL_GL_SetAttribute (SDL_GL_BLUE_SIZE, 8);
|
||||
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);
|
||||
|
||||
|
||||
// Initialize video mode
|
||||
_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());
|
||||
return(0);
|
||||
}
|
||||
SDL_WM_SetCaption(title, NULL);
|
||||
_t_frame=1000000/fps;
|
||||
_width=width;
|
||||
_height=height;
|
||||
|
||||
// Set the desired state
|
||||
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
|
||||
glEnable(GL_CULL_FACE);
|
||||
glEnable(GL_TEXTURE_2D);
|
||||
glDisable(GL_LIGHTING);
|
||||
glDisable(GL_DEPTH_TEST);
|
||||
glDepthMask( GL_FALSE);
|
||||
|
||||
// Triplebuffer swap
|
||||
SDL_GL_SwapBuffers();
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
SDL_GL_SwapBuffers();
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
SDL_GL_SwapBuffers();
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
// Show device info
|
||||
char *str;
|
||||
printf("\n*********************************\n");
|
||||
printf("*** Draw Info\n");
|
||||
str=(char *)glGetString(GL_VENDOR);
|
||||
printf(" Vendor: %s\n",str);
|
||||
str=(char *)glGetString(GL_RENDERER);
|
||||
printf(" Renderer: %s\n",str);
|
||||
str=(char *)glGetString(GL_VERSION);
|
||||
printf(" Version: %s\n",str);
|
||||
printf("*********************************\n");
|
||||
|
||||
// Set the proyection (Ortographic)
|
||||
glMatrixMode (GL_PROJECTION);
|
||||
glPushMatrix ();
|
||||
glLoadIdentity ();
|
||||
glOrtho (0,width, 0, height, -1000, 1000);
|
||||
glMatrixMode (GL_MODELVIEW);
|
||||
glPushMatrix ();
|
||||
glLoadIdentity ();
|
||||
|
||||
// Enable Alpha blending
|
||||
glEnable(GL_BLEND);
|
||||
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
|
||||
|
||||
return(1);
|
||||
}
|
||||
|
||||
|
||||
/////////////////////////////
|
||||
// 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;
|
||||
long long t_frame=0;
|
||||
|
||||
t_frame=0;
|
||||
while(!done){
|
||||
|
||||
// Update screen
|
||||
time=Time_GetTime();
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Process keys for Draw
|
||||
keys=SDL_GetKeyState(NULL);
|
||||
if(keys[SDLK_F12]){
|
||||
// Screenshot key
|
||||
SDL_SaveBMP(_screen,"shot.bmp");
|
||||
}
|
||||
|
||||
// Input and sound Frame
|
||||
Input_Frame();
|
||||
Audio_Frame();
|
||||
|
||||
// Process
|
||||
time2=Time_GetTime();
|
||||
t_frame+=time2-time;
|
||||
time=time2;
|
||||
if(proc){
|
||||
while(t_frame>_t_frame && !done){
|
||||
if(!proc()){
|
||||
done=1;
|
||||
}
|
||||
t_frame-=_t_frame;
|
||||
}
|
||||
}
|
||||
|
||||
// Draw
|
||||
draw();
|
||||
|
||||
Time_Pause(0);
|
||||
t_frame+=Time_GetTime()-time;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/////////////////////////////
|
||||
// Draw_Clean
|
||||
//
|
||||
// Cleans the game window.
|
||||
void Draw_Clean(
|
||||
unsigned char r,
|
||||
unsigned char g,
|
||||
unsigned char b)
|
||||
{
|
||||
glClearColor(r/256.0f,g/256.0f,b/256.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 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_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);
|
||||
|
||||
return(tex);
|
||||
}
|
||||
|
||||
|
||||
/////////////////////////////
|
||||
// Draw_LoadImage
|
||||
//
|
||||
// Loads a image, giving a reference.
|
||||
DrawImg Draw_LoadImage(char *filename){
|
||||
DrawImage *image;
|
||||
SDL_Surface *surf;
|
||||
|
||||
|
||||
// Loads the surface
|
||||
surf=Draw_LoadSurface(filename);
|
||||
if(surf == NULL){
|
||||
return(NULL);
|
||||
}
|
||||
|
||||
|
||||
// Create the image container
|
||||
image=malloc(sizeof(DrawImage));
|
||||
image->surf=surf;
|
||||
image->tex=Draw_UploadGLTexture(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;
|
||||
}
|
||||
|
||||
|
||||
/////////////////////////////
|
||||
// Draw_DrawImg
|
||||
//
|
||||
// Draws an image.
|
||||
void Draw_DrawImg(DrawImg img,int x,int y){
|
||||
DrawImage *image=img;
|
||||
int 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);
|
||||
|
||||
// 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 ();
|
||||
}
|
||||
|
||||
|
||||
/////////////////////////////
|
||||
// Draw_DrawImgPart
|
||||
//
|
||||
// Draws an image part.
|
||||
void Draw_DrawImgPart(DrawImg img,int x,int y,int w,int i){
|
||||
DrawImage *image=img;
|
||||
int x1,x2,y1,y2;
|
||||
float us,u1,u2;
|
||||
|
||||
// Prepare
|
||||
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;
|
||||
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 ();
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////
|
||||
// DrawFnt //
|
||||
/////////////
|
||||
// Reference to a Font.
|
||||
typedef struct {
|
||||
DrawImage img;
|
||||
int w,h;
|
||||
int min,max;
|
||||
} DrawFont;
|
||||
|
||||
|
||||
/////////////////////////////
|
||||
// Draw_DefaultFont
|
||||
//
|
||||
// Creates a surface with the default font.
|
||||
#include "FontData.h"
|
||||
SDL_Surface *Draw_DefaultFontSurface(
|
||||
unsigned char r,
|
||||
unsigned char g,
|
||||
unsigned char b,
|
||||
unsigned char a)
|
||||
{
|
||||
SDL_Surface *surf;
|
||||
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);
|
||||
|
||||
// Draw the font
|
||||
SDL_LockSurface(surf);
|
||||
for(c=0;c<256;c++){
|
||||
for(y=0;y<8;y++){
|
||||
for(x=0;x<8;x++){
|
||||
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;
|
||||
}else{
|
||||
//Imagen_PutPixel(dest,c*8+x,y,color2);
|
||||
((Uint32 *)surf->pixels)[(c*8+x)+(8*256*y)]=
|
||||
color2;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
SDL_UnlockSurface(surf);
|
||||
|
||||
return(surf);
|
||||
}
|
||||
|
||||
|
||||
/////////////////////////////
|
||||
// Draw_DefaultFont
|
||||
//
|
||||
// Creates the default font.
|
||||
DrawFnt Draw_DefaultFont(
|
||||
unsigned char r,
|
||||
unsigned char g,
|
||||
unsigned char b,
|
||||
unsigned char a)
|
||||
{
|
||||
DrawFont *font;
|
||||
|
||||
// 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->w=8;
|
||||
font->h=8;
|
||||
font->min=0;
|
||||
font->max=256;
|
||||
|
||||
return((DrawFnt)font);
|
||||
}
|
||||
|
||||
/////////////////////////////
|
||||
// Draw_LoadFont
|
||||
//
|
||||
// Load a font from a file.
|
||||
DrawFnt Draw_LoadFont(char *fichero,int min,int max){
|
||||
DrawFont *font;
|
||||
|
||||
// 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->min=min;
|
||||
font->max=max;
|
||||
|
||||
return((DrawFnt)font);
|
||||
}
|
||||
|
||||
|
||||
/////////////////////////////
|
||||
// Draw_DrawText
|
||||
//
|
||||
// Draws text using a font
|
||||
void Draw_DrawText(DrawFnt f,char *text,int x,int y){
|
||||
DrawFont *font=f;
|
||||
char *ptr;
|
||||
|
||||
// Iterate the string
|
||||
ptr=text;
|
||||
while(*ptr){
|
||||
if((*ptr)<font->max){
|
||||
Draw_DrawImgPart((DrawImg)&font->img,x,y,font->w,(*ptr)-font->min);
|
||||
}
|
||||
x+=font->w;
|
||||
ptr++;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,24 +62,21 @@ int Draw_Init(int width,int height,char *title,int fps){
|
||||
// Draw_Loop
|
||||
//
|
||||
// Loops updating the game window.
|
||||
void Draw_Loop(int (*proc)()){
|
||||
void Draw_Loop(int (*proc)(),void (*draw)()){
|
||||
int done=0;
|
||||
SDL_Event event;
|
||||
Uint8* keys;
|
||||
long long t_framestart;
|
||||
long long t_frame;
|
||||
long long t_swap;
|
||||
long long t_proc;
|
||||
int f_count;
|
||||
|
||||
t_framestart=Time_GetTime();
|
||||
long long time,time2;
|
||||
long long t_frame=0;
|
||||
|
||||
t_frame=0;
|
||||
while(!done){
|
||||
|
||||
// Update screen
|
||||
t_swap=Time_GetTime();
|
||||
time=Time_GetTime();
|
||||
//SDL_GL_SwapBuffers();
|
||||
SDL_Flip(_screen);
|
||||
t_swap=Time_GetTime()-t_swap;
|
||||
|
||||
|
||||
// Process Events
|
||||
while(SDL_PollEvent(&event) ){
|
||||
@@ -104,31 +101,24 @@ void Draw_Loop(int (*proc)()){
|
||||
Input_Frame();
|
||||
Audio_Frame();
|
||||
|
||||
// Process loop, with frameskip for slow swapping systems
|
||||
t_proc=Time_GetTime();
|
||||
if(!proc()){
|
||||
done=1;
|
||||
}
|
||||
f_count=(t_swap/_t_frame);
|
||||
while(f_count>0 && !done){
|
||||
if(!proc()){
|
||||
done=1;
|
||||
// Process
|
||||
time2=Time_GetTime();
|
||||
t_frame+=time2-time;
|
||||
time=time2;
|
||||
if(proc){
|
||||
while(t_frame>_t_frame && !done){
|
||||
if(!proc()){
|
||||
done=1;
|
||||
}
|
||||
t_frame-=_t_frame;
|
||||
}
|
||||
f_count--;
|
||||
t_framestart+=_t_frame;
|
||||
}
|
||||
t_proc=Time_GetTime()-t_proc;
|
||||
t_framestart+=_t_frame*(t_proc/_t_frame);
|
||||
|
||||
// Draw
|
||||
draw();
|
||||
|
||||
// Sleep to limit frames
|
||||
t_frame=Time_GetTime()-t_framestart;
|
||||
if(t_frame<_t_frame){
|
||||
Time_Pause(_t_frame-t_frame);
|
||||
}else{
|
||||
Time_Pause(0);
|
||||
}
|
||||
t_framestart=Time_GetTime();
|
||||
Time_Pause(0);
|
||||
t_frame+=Time_GetTime()-time;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -265,35 +255,6 @@ void Draw_GetOffset(DrawImg img,int *x,int *y){
|
||||
}
|
||||
|
||||
|
||||
/////////////////////////////
|
||||
// Draw_ImgSetKeyCol
|
||||
//
|
||||
// Setting the image color key.
|
||||
void Draw_ImgSetKeyCol(DrawImg img,
|
||||
unsigned char r,
|
||||
unsigned char g,
|
||||
unsigned char b)
|
||||
{
|
||||
DrawImage *image=img;
|
||||
|
||||
// Set the color key for the surface
|
||||
SDL_SetColorKey(image->surf, SDL_SRCCOLORKEY,
|
||||
SDL_MapRGB(image->surf->format, r, g, b));
|
||||
}
|
||||
|
||||
|
||||
/////////////////////////////
|
||||
// Draw_ImgSetAlpha
|
||||
//
|
||||
// Setting the image alpha.
|
||||
void Draw_ImgSetAlpha(DrawImg img, unsigned char a){
|
||||
DrawImage *image=img;
|
||||
|
||||
// Set the alpha for the surface
|
||||
SDL_SetAlpha(image->surf, SDL_SRCALPHA, a);
|
||||
}
|
||||
|
||||
|
||||
/////////////////////////////
|
||||
// Draw_DrawImg
|
||||
//
|
||||
@@ -396,7 +357,7 @@ SDL_Surface *Draw_DefaultFontSurface(
|
||||
// Draw the font
|
||||
SDL_LockSurface(surf);
|
||||
color =SDL_MapRGBA(surf->format,r,g,b,a);
|
||||
color2=SDL_MapRGBA(surf->format,r,g,0,0);
|
||||
color2=SDL_MapRGBA(surf->format,r,g,b,0);
|
||||
for(c=0;c<256;c++){
|
||||
for(y=0;y<8;y++){
|
||||
for(x=0;x<8;x++){
|
||||
@@ -417,6 +378,7 @@ SDL_Surface *Draw_DefaultFontSurface(
|
||||
return(surf);
|
||||
}
|
||||
|
||||
|
||||
/////////////////////////////
|
||||
// Draw_DefaultFont
|
||||
//
|
||||
@@ -440,6 +402,7 @@ DrawFnt Draw_DefaultFont(
|
||||
return((DrawFnt)font);
|
||||
}
|
||||
|
||||
|
||||
/////////////////////////////
|
||||
// Draw_LoadFont
|
||||
//
|
||||
@@ -448,7 +411,7 @@ DrawFnt Draw_LoadFont(char *fichero,int min,int max){
|
||||
DrawFont *font;
|
||||
int w,h;
|
||||
|
||||
// Create the default font
|
||||
// Create the font form the image
|
||||
font=malloc(sizeof(DrawFont));
|
||||
font->surf = Draw_LoadSurface(fichero);
|
||||
font->w=font->surf->w/(max-min);
|
||||
@@ -459,6 +422,7 @@ DrawFnt Draw_LoadFont(char *fichero,int min,int max){
|
||||
return((DrawFnt)font);
|
||||
}
|
||||
|
||||
|
||||
/////////////////////////////
|
||||
// Draw_DrawText
|
||||
//
|
||||
@@ -401,7 +401,6 @@ void GameEnts_Init(){
|
||||
ent_player->mass=70.0f;
|
||||
ent_player->fric_static=0.5f;
|
||||
AnimPlay_SetImg(&ent_player->anim,img_player_down);
|
||||
//AnimPlay_SetAnim(&ent_player->anim,anim_fire);
|
||||
ent_player->proc=player_proc;
|
||||
|
||||
|
||||
|
||||
87
GameLib.c
87
GameLib.c
@@ -1,5 +1,6 @@
|
||||
// Copyright (C) 2011 Valeriano Alfonso Rodriguez (Kableado)
|
||||
|
||||
#include <math.h>
|
||||
#include <SDL/SDL.h>
|
||||
|
||||
#include "Time.h"
|
||||
@@ -22,10 +23,18 @@ int _entities_lock=0;
|
||||
int _entities_compactate=0;
|
||||
void (*_gameproc)()=NULL;
|
||||
void (*_gamepostproc)()=NULL;
|
||||
void (*_gamedraw)()=NULL;
|
||||
int _ft;
|
||||
int _game_size[2];
|
||||
int _game_pos[2];
|
||||
|
||||
long long t_proc;
|
||||
long long t_col;
|
||||
long long t_over;
|
||||
long long t_postproc;
|
||||
long long t_draw;
|
||||
int f_count;
|
||||
|
||||
|
||||
/////////////////////////////
|
||||
// GameLib_Init
|
||||
@@ -161,11 +170,6 @@ void GameLib_Compactate(){
|
||||
// GameLib_ProcLoop
|
||||
//
|
||||
// Process the loop.
|
||||
long long t_proc;
|
||||
long long t_col;
|
||||
long long t_over;
|
||||
long long t_postproc;
|
||||
int f_count;
|
||||
int GameLib_ProcLoop(){
|
||||
int i,j;
|
||||
int repeat,count;
|
||||
@@ -270,25 +274,12 @@ int GameLib_ProcLoop(){
|
||||
}while(n>0);
|
||||
|
||||
|
||||
// PostProcess and draw entities
|
||||
// PostProcess
|
||||
time=Time_GetTime();
|
||||
GameLib_Compactate();_entities_lock=1;
|
||||
for(i=0;i<_n_entities;i++){
|
||||
Entity *e;
|
||||
Entity_PostProcess(_entity[i],_ft);
|
||||
|
||||
// FIXME: This is a hack
|
||||
e=_entity[i];
|
||||
if(e->pos[0]<(_game_pos[0]-128))
|
||||
continue;
|
||||
if(e->pos[0]>(_game_pos[0]+_game_size[0]+128))
|
||||
continue;
|
||||
if(e->pos[1]<(_game_pos[1]-128))
|
||||
continue;
|
||||
if(e->pos[1]>(_game_pos[1]+_game_size[1]+128))
|
||||
continue;
|
||||
|
||||
Entity_Draw(e,-_game_pos[0],-_game_pos[1]);
|
||||
}
|
||||
if(_gamepostproc){
|
||||
_gamepostproc();
|
||||
@@ -302,30 +293,74 @@ int GameLib_ProcLoop(){
|
||||
}
|
||||
|
||||
|
||||
/////////////////////////////
|
||||
// GameLib_DrawLoop
|
||||
//
|
||||
//
|
||||
void GameLib_DrawLoop(){
|
||||
long long time;
|
||||
int i;
|
||||
|
||||
time=Time_GetTime();
|
||||
|
||||
// Limpiar pantalla
|
||||
Draw_Clean(0,0,0);
|
||||
|
||||
// Draw entities
|
||||
GameLib_Compactate();_entities_lock=1;
|
||||
for(i=0;i<_n_entities;i++){
|
||||
Entity *e;
|
||||
|
||||
// FIXME: This is a hack
|
||||
e=_entity[i];
|
||||
if(e->pos[0]<(_game_pos[0]-128))
|
||||
continue;
|
||||
if(e->pos[0]>(_game_pos[0]+_game_size[0]+128))
|
||||
continue;
|
||||
if(e->pos[1]<(_game_pos[1]-128))
|
||||
continue;
|
||||
if(e->pos[1]>(_game_pos[1]+_game_size[1]+128))
|
||||
continue;
|
||||
|
||||
Entity_Draw(e,-_game_pos[0],-_game_pos[1]);
|
||||
}
|
||||
if(_gamedraw){
|
||||
_gamedraw();
|
||||
}
|
||||
GameLib_Compactate();
|
||||
|
||||
t_draw+=Time_GetTime()-time;
|
||||
}
|
||||
|
||||
|
||||
/////////////////////////////
|
||||
// GameLib_Loop
|
||||
//
|
||||
// Loops the game.
|
||||
void GameLib_Loop(
|
||||
void (*gameproc)(),
|
||||
void (*gamepostproc)())
|
||||
void (*gamepostproc)(),
|
||||
void (*gamedraw)())
|
||||
{
|
||||
_running=1;
|
||||
|
||||
_gameproc=gameproc;
|
||||
_gamepostproc=gamepostproc;
|
||||
_gamedraw=gamedraw;
|
||||
t_proc=0;
|
||||
t_col=0;
|
||||
t_over=0;
|
||||
t_postproc=0;
|
||||
t_draw=0;
|
||||
f_count=0;
|
||||
Draw_Loop(GameLib_ProcLoop);
|
||||
Draw_Loop(GameLib_ProcLoop,GameLib_DrawLoop);
|
||||
|
||||
printf("Profiling::::::::::::\n");
|
||||
printf("t_proc........:%6lld\n",t_proc/f_count);
|
||||
printf("t_col.........:%6lld\n",t_col/f_count);
|
||||
printf("t_over........:%6lld\n",t_over/f_count);
|
||||
printf("t_postprocdraw:%6lld\n",t_postproc/f_count);
|
||||
printf("Profiling:::::::::\n");
|
||||
printf("t_proc.....:%6lld\n",t_proc/f_count);
|
||||
printf("t_col......:%6lld\n",t_col/f_count);
|
||||
printf("t_over.....:%6lld\n",t_over/f_count);
|
||||
printf("t_postproc.:%6lld\n",t_postproc/f_count);
|
||||
printf("t_draw.....:%6lld\n",t_draw/f_count);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -46,7 +46,8 @@ int GameLib_DelEntity(Entity *e);
|
||||
// Loops the game.
|
||||
void GameLib_Loop(
|
||||
void (*gameproc)(),
|
||||
void (*gamepostproc)());
|
||||
void (*gamepostproc)(),
|
||||
void (*gamedraw)());
|
||||
|
||||
|
||||
/////////////////////////////
|
||||
|
||||
24
Input.c
24
Input.c
@@ -9,7 +9,7 @@
|
||||
|
||||
// Globals
|
||||
InputKeyStatus _keys[InputKey_Max];
|
||||
|
||||
SDL_Joystick *_joy;
|
||||
|
||||
/////////////////////////////
|
||||
// Input_Init
|
||||
@@ -18,11 +18,33 @@ InputKeyStatus _keys[InputKey_Max];
|
||||
int Input_Init(){
|
||||
int i;
|
||||
|
||||
// Init the SDL Joystick subsistem
|
||||
SDL_InitSubSystem(SDL_INIT_JOYSTICK);
|
||||
|
||||
// Mark released all the keys
|
||||
for(i=0;i<InputKey_Max;i++){
|
||||
_keys[i]=InputKey_Released;
|
||||
}
|
||||
|
||||
// Check for joystick
|
||||
if(SDL_NumJoysticks()>0){
|
||||
// Open joystick
|
||||
_joy=SDL_JoystickOpen(0);
|
||||
|
||||
if(_joy){
|
||||
printf("Opened Joystick 0\n");
|
||||
printf("Name: %s\n", SDL_JoystickName(0));
|
||||
printf("Number of Axes: %d\n", SDL_JoystickNumAxes(_joy));
|
||||
printf("Number of Buttons: %d\n", SDL_JoystickNumButtons(_joy));
|
||||
printf("Number of Balls: %d\n", SDL_JoystickNumBalls(_joy));
|
||||
}else
|
||||
printf("Couldn't open Joystick 0\n");
|
||||
|
||||
// Close if opened
|
||||
if(SDL_JoystickOpened(0))
|
||||
SDL_JoystickClose(_joy);
|
||||
}
|
||||
|
||||
return(1);
|
||||
}
|
||||
|
||||
|
||||
3
Makefile
3
Makefile
@@ -14,12 +14,15 @@ endif # windir
|
||||
|
||||
|
||||
ifeq ($(TARGET_ARCH),mingw)
|
||||
OPENGL_DRAW=opengl
|
||||
include Makefile.win32
|
||||
else
|
||||
ifeq ($(TARGET_ARCH),linux)
|
||||
OPENGL_DRAW=opengl
|
||||
include Makefile.linux
|
||||
else
|
||||
ifeq ($(TARGET_ARCH),macosx)
|
||||
OPENGL_DRAW=opengl
|
||||
include Makefile.macosx
|
||||
endif # macosx
|
||||
endif # linux
|
||||
|
||||
@@ -37,8 +37,13 @@ $(BUILDDIR)/Time.o: Time.c $(HEADS)
|
||||
$(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)
|
||||
ifeq ($(OPENGL_DRAW),opengl)
|
||||
$(BUILDDIR)/Draw.o: DrawGL.c $(HEADS)
|
||||
$(CC) -c DrawGL.c -o $(BUILDDIR)/Draw.o $(CFLAGS)
|
||||
else
|
||||
$(BUILDDIR)/Draw.o: DrawSDL.c $(HEADS)
|
||||
$(CC) -c DrawSDL.c -o $(BUILDDIR)/Draw.o $(CFLAGS)
|
||||
endif
|
||||
|
||||
$(BUILDDIR)/Input.o: Input.c $(HEADS)
|
||||
$(CC) -c Input.c -o $(BUILDDIR)/Input.o $(CFLAGS)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
LIBS= -lSDL -lpthread -L/usr/X11R6/lib -L/usr/lib -lm
|
||||
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
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
LIBS=-lm -ldl -framework Cocoa -framework SDL macosx/SDLMain.m
|
||||
LIBS=-lm -ldl -framework Cocoa -framework SDL -framework OpenGL macosx/SDLMain.m
|
||||
CFLAGS=-g -DDEBUG -Wall -DMACOSX -ObjC -Dmain=SDL_main
|
||||
CC=gcc
|
||||
RM=rm -rf
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
LIBS= -L/usr/i486-mingw/lib -D_GNU_SOURCE=1 -Dmain=SDL_main
|
||||
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
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
LIBS=-I/mingw/include/SDL -D_GNU_SOURCE=1 -Dmain=SDL_main
|
||||
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
|
||||
|
||||
2
Util.c
2
Util.c
@@ -57,7 +57,7 @@ int Intersec_RayUnitCircle(vec2 orig,vec2 vel,vec2 center,float *t){
|
||||
*t=Rmin;
|
||||
return(1);
|
||||
}
|
||||
if(Rmax>=-0.0f && Rmin>Rmax && Rmax<=1.0f){
|
||||
if(Rmax>=-1.0f && Rmin>Rmax && Rmax<=1.0f){
|
||||
*t=Rmax;
|
||||
return(1);
|
||||
}
|
||||
|
||||
@@ -11,17 +11,18 @@
|
||||
#S.....BSSB.S#
|
||||
#SSSSS.SSSSSS#
|
||||
#mmmmm.mmmmmm#
|
||||
mBm
|
||||
mBm
|
||||
mBm
|
||||
mBm
|
||||
mBm
|
||||
######B#######
|
||||
m.m
|
||||
m.m
|
||||
m.m
|
||||
m.m
|
||||
m.m
|
||||
######.#######
|
||||
#SSSSm2..SSSS#
|
||||
#SSSSmBm.SSSS#
|
||||
#S..SSB..SSSS#
|
||||
#S.SSSSSSSSSS#
|
||||
#S.SSSSSSSSSS#
|
||||
#S.SSSSSSSSSS#
|
||||
#S.....BSSB.S#
|
||||
#SSSSS.SSSSSS#
|
||||
#mmmmm.mmmmmm#
|
||||
|
||||
70
main.c
70
main.c
@@ -24,6 +24,16 @@ DrawFnt font;
|
||||
DrawFnt font_shad;
|
||||
|
||||
int ProcTitle(){
|
||||
if( Input_GetKey(InputKey_Jump)==InputKey_Pressed||
|
||||
Input_GetKey(InputKey_Continue)==InputKey_Pressed)
|
||||
{
|
||||
play=1;
|
||||
return(0);
|
||||
}
|
||||
return(1);
|
||||
}
|
||||
|
||||
void DrawTitle(){
|
||||
Draw_Clean(0,0,0);
|
||||
|
||||
Draw_DrawImg(img_logo,170,100);
|
||||
@@ -35,16 +45,20 @@ int ProcTitle(){
|
||||
|
||||
Draw_DrawText(font ,"By Kableado (VAR)",200,440);
|
||||
|
||||
}
|
||||
|
||||
|
||||
int ProcEnd(){
|
||||
if( Input_GetKey(InputKey_Jump)==InputKey_Pressed||
|
||||
Input_GetKey(InputKey_Continue)==InputKey_Pressed)
|
||||
{
|
||||
play=1;
|
||||
return(0);
|
||||
}
|
||||
return(1);
|
||||
}
|
||||
|
||||
int ProcEnd(){
|
||||
|
||||
void DrawEnd(){
|
||||
Draw_Clean(0,0,0);
|
||||
|
||||
Draw_DrawImg(img_end,170,100);
|
||||
@@ -53,20 +67,31 @@ int ProcEnd(){
|
||||
Draw_DrawText(font ,"Thanks for playing!",250,350);
|
||||
|
||||
Draw_DrawText(font ,"Press [Space] to Title.",300,400);
|
||||
|
||||
if( Input_GetKey(InputKey_Jump)==InputKey_Pressed||
|
||||
Input_GetKey(InputKey_Continue)==InputKey_Pressed)
|
||||
{
|
||||
return(0);
|
||||
}
|
||||
return(1);
|
||||
}
|
||||
|
||||
|
||||
void ProcGame(){
|
||||
Draw_Clean(0,0,0);
|
||||
}
|
||||
|
||||
void PostProcGame(){
|
||||
|
||||
if(game_level_reset){
|
||||
if(Input_AnyKey()){
|
||||
if(GameMap_CreateLevel(game_level,game_level_point)){
|
||||
if(game_level_reset==2){
|
||||
int pos[2]={0,0};
|
||||
GameLib_SetPos(pos);
|
||||
}
|
||||
game_level_reset=0;
|
||||
}else{
|
||||
play=2;
|
||||
GameLib_BreakLoop();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void DrawGame(){
|
||||
char string[1024];
|
||||
|
||||
sprintf(string, "Level: %d.%d",game_level+1,game_level_point);
|
||||
@@ -86,24 +111,9 @@ void PostProcGame(){
|
||||
GameLib_BreakLoop();
|
||||
}
|
||||
|
||||
if(game_level_reset){
|
||||
if(Input_AnyKey()){
|
||||
if(GameMap_CreateLevel(game_level,game_level_point)){
|
||||
if(game_level_reset==2){
|
||||
int pos[2]={0,0};
|
||||
GameLib_SetPos(pos);
|
||||
}
|
||||
game_level_reset=0;
|
||||
}else{
|
||||
play=2;
|
||||
GameLib_BreakLoop();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
int main(int argc,char *argv[]){
|
||||
|
||||
srand(time(NULL));
|
||||
@@ -113,28 +123,28 @@ int main(int argc,char *argv[]){
|
||||
img_logo=Draw_LoadImage("data/logo.bmp");
|
||||
img_end=Draw_LoadImage("data/end.bmp");
|
||||
|
||||
font=Draw_DefaultFont(255,255,255,255);
|
||||
font=Draw_DefaultFont(255,0,0,255);
|
||||
font_shad=Draw_DefaultFont(0,0,0,127);
|
||||
|
||||
|
||||
GameEnts_Init();
|
||||
do{
|
||||
play=0;
|
||||
Draw_Loop(ProcTitle);
|
||||
Draw_Loop(ProcTitle,DrawTitle);
|
||||
if(play==1){
|
||||
if(!game_started){
|
||||
int pos[2]={0,0};
|
||||
GameLib_SetPos(pos);
|
||||
game_level=3;
|
||||
game_level=0;
|
||||
game_level_point=1;
|
||||
game_level_reset=0;
|
||||
GameMap_CreateLevel(game_level,game_level_point);
|
||||
}
|
||||
game_started=1;
|
||||
GameLib_Loop(ProcGame,PostProcGame);
|
||||
GameLib_Loop(ProcGame,PostProcGame,DrawGame);
|
||||
}
|
||||
if(play==2){
|
||||
Draw_Loop(ProcEnd);
|
||||
Draw_Loop(ProcEnd,DrawEnd);
|
||||
}
|
||||
}while(play);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user