QuadArray2D: Queues all the draw calls and then performs the in the most optimal way.

This commit is contained in:
2013-04-13 14:30:49 +02:00
committed by Valeriano A.R
parent 4e111d881e
commit 11f5aad3ab
8 changed files with 194 additions and 50 deletions

View File

@@ -67,4 +67,5 @@ void GameEnts_Init();
#endif _GAMEENTS_H_
#endif

View File

@@ -24,9 +24,8 @@
#include "Time.h"
#include "Util.h"
#include "QuadArray2D.h"
#include "Draw.h"
#include "Input.h"
#include "Audio.h"
// Globals
@@ -35,6 +34,9 @@ int _width;
int _height;
long long proc_t_frame=33333;
long long draw_t_frame=16667;
QuadArray2D *_quadArray=NULL;
GLuint _tex=-1;
float _color[4];
/////////////////////////////
// Draw_Init
@@ -88,7 +90,7 @@ int Draw_Init(int width,int height,char *title,int pfps,int fps){
// Set the desired state
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glEnable(GL_CULL_FACE);
glDisable(GL_CULL_FACE);
glEnable(GL_TEXTURE_2D);
glDisable(GL_LIGHTING);
glDisable(GL_DEPTH_TEST);
@@ -126,6 +128,11 @@ 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);
}
@@ -184,6 +191,7 @@ void Draw_Loop(int (*proc)(),void (*draw)()){
}
if(draw){
draw();
Draw_Flush();
}
// Measure time
@@ -369,13 +377,44 @@ void Draw_GetOffset(DrawImg img,int *x,int *y){
}
/////////////////////////////
// Draw_Flush
//
// Performs all the queued draw actions.
void Draw_Flush(){
// Draw the quad array
glBindTexture(GL_TEXTURE_2D, _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;
float x1,x2,y1,y2;
// Prepare
x1=x+image->x;
@@ -384,20 +423,14 @@ void Draw_DrawImg(DrawImg img,int x,int y){
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 ();
if(_tex!=image->tex){
Draw_Flush();
_tex=image->tex;
}
QuadArray2D_AddQuad(_quadArray,
x1,y1,0.0f,0.0f,
x2,y2,1.0f,1.0f,
_color);
}
@@ -416,20 +449,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(_tex!=image->tex){
Draw_Flush();
_tex=image->tex;
}
QuadArray2D_AddQuad(_quadArray,
x1,y1,0.0f,0.0f,
x2,y2,1.0f,1.0f,
_color);
}
@@ -453,20 +480,14 @@ void Draw_DrawImgPart(DrawImg img,int x,int y,int w,int i){
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(_tex!=image->tex){
Draw_Flush();
_tex=image->tex;
}
QuadArray2D_AddQuad(_quadArray,
x1,y1,u1,0.0f,
x2,y2,u2,1.0f,
_color);
}
@@ -476,6 +497,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;
}
@@ -517,7 +542,7 @@ SDL_Surface *Draw_DefaultFontSurface(
color2=SDL_MapRGBA(surf->format,b,g,r,0);
// Draw the font
SDL_LockSurface(surf);
//SDL_LockSurface(surf);
for(c=0;c<256;c++){
for(y=0;y<8;y++){
for(x=0;x<8;x++){
@@ -533,7 +558,7 @@ SDL_Surface *Draw_DefaultFontSurface(
}
}
}
SDL_UnlockSurface(surf);
//SDL_UnlockSurface(surf);
return(surf);
}

View File

@@ -28,6 +28,12 @@ void Draw_Clean(
unsigned char b);
/////////////////////////////
// Draw_Flush
//
// Performs all the queued draw actions.
void Draw_Flush();
////////////////////////////////////////////////
// DrawImg //
/////////////
@@ -58,6 +64,7 @@ void Draw_SetOffset(DrawImg img,int x,int y);
void Draw_GetOffset(DrawImg img,int *x,int *y);
/////////////////////////////
// Draw_DrawImg
//

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(QuadArray2D));
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);
}

31
GameLib/QuadArray2D.h Normal file
View File

@@ -0,0 +1,31 @@
// 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
typedef struct Tag_QuadArray2D {
float *vertexData;
int nVertex;
int resVertex;
} QuadArray2D;
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

@@ -2,6 +2,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "GameLib.h"

View File

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

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 \
@@ -67,6 +69,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)