summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatt Turner <mattst88@gmail.com>2010-01-21 15:38:03 -0500
committerMatt Turner <mattst88@gmail.com>2011-04-04 17:14:35 -0400
commitce7f4caaf9fa41e383efbc4854106558b3ff0e19 (patch)
tree67fa037c36f5470e7d2a57a722cec778f9b55bae
parentbf6f824b24b18197eabb7737f8bac8cc8a4baa8c (diff)
Rework GLPong_Init and text init.
Signed-off-by: Matt Turner <mattst88@gmail.com>
-rw-r--r--Makefile4
-rw-r--r--fps.c23
-rw-r--r--fps.h11
-rw-r--r--glpong3d.c140
-rw-r--r--glpong3d.h10
-rw-r--r--text.c64
-rw-r--r--text.h9
7 files changed, 179 insertions, 82 deletions
diff --git a/Makefile b/Makefile
index eed7203..e9f658d 100644
--- a/Makefile
+++ b/Makefile
@@ -1,7 +1,7 @@
CC=gcc
STRIP=strip
WARN=-Wall -W -ansi -pedantic
-CFLAGS=-g -Os -pipe -std=c99 ${WARN}
+CFLAGS=-ggdb -g3 -pipe -std=c99 ${WARN}
LDFLAGS=-Wl,-O1,-s
INCLUDES=`sdl-config --cflags`
LIBRARIES=`sdl-config --libs` -lSDL_image -lSDL_net -lGL -lGLU -lGLC
@@ -9,7 +9,7 @@ EXT=.exe
EXE=glpong3d${EXT}
-OBJS=glpong3d.o # ball.o paddle.o
+OBJS=glpong3d.o fps.o text.o # ball.o paddle.o
all: ${OBJS}
${CC} ${LDFLAGS} ${OBJS} -o ${EXE} ${LIBRARIES}
diff --git a/fps.c b/fps.c
new file mode 100644
index 0000000..65ee4a0
--- /dev/null
+++ b/fps.c
@@ -0,0 +1,23 @@
+#include <time.h>
+
+#include "SDL.h"
+#include "SDL_opengl.h"
+
+#include "fps.h"
+
+static clock_t start;
+static GLfloat fps;
+
+void
+GLPong_FPSInit(void) {
+ start = SDL_GetTicks();
+}
+
+GLfloat
+GLPong_FPSCount(void) {
+ /* 10.0f is the number of frames we let draw before calculating FPS */
+ fps = 10.0f / (SDL_GetTicks() - start) * 1000; /* 1 sec / 1 millisec == 1000 */
+ start = SDL_GetTicks();
+ return fps;
+}
+
diff --git a/fps.h b/fps.h
new file mode 100644
index 0000000..48c8a84
--- /dev/null
+++ b/fps.h
@@ -0,0 +1,11 @@
+#ifndef FPS_H
+#define FPS_H
+
+#include "SDL.h"
+#include "SDL_opengl.h"
+
+void GLPong_FPSInit(void);
+GLfloat GLPong_FPSCount(void);
+
+#endif
+
diff --git a/glpong3d.c b/glpong3d.c
index c15ba62..f6d270d 100644
--- a/glpong3d.c
+++ b/glpong3d.c
@@ -9,8 +9,9 @@
#include <GL/glc.h>
#include "glpong3d.h"
+#include "text.h"
-static void GLPong_Init(void);
+static int GLPong_Init(GLPong_t * GLPong);
static int GLPong_HandleEvents(void);
static void GLPong_Draw(void);
static void GLPong_CleanUp(void);
@@ -28,7 +29,10 @@ int main(int argc, char * argv[]) {
if (argv) {}
}
- GLPong_Init();
+ if (GLPong_Init(&GLPong) < 0) {
+ fprintf(stderr, "Bailing out.\n");
+ return -1;
+ }
start = SDL_GetTicks();
@@ -56,68 +60,57 @@ int main(int argc, char * argv[]) {
return 0;
}
-void GLPong_Init(void) {
+static int
+GLPong_Init(GLPong_t * GLPong) {
SDL_Surface * temp = NULL;
- int i;
- int count;
-
- GLPong.Ball.w = 0.5f;
- GLPong.Ball.h = 0.5f;
- GLPong.Ball.x = -(GLPong.Ball.w / 2);
- GLPong.Ball.y = -(GLPong.Ball.h / 2);
- GLPong.Ball.z = -3.0f;
- GLPong.Ball.r = 1.0f;
- GLPong.Ball.g = 0.0f;
- GLPong.Ball.b = 0.0f;
- GLPong.Ball.xv = 0.02f;
- GLPong.Ball.yv = 0.01f;
- GLPong.Ball.zv = -0.05f;
-
- GLPong.Front.w = 0.66f;
- GLPong.Front.h = 0.5f;
- GLPong.Front.x = -1.0f;
- GLPong.Front.y = -1.0f;
- GLPong.Front.z = -3.0f;
- GLPong.Front.r = 1.0f;
- GLPong.Front.g = 1.0f;
- GLPong.Front.b = 1.0f;
-
- GLPong.Back.w = 0.66f;
- GLPong.Back.h = 0.5f;
- GLPong.Back.x = -(GLPong.Front.w / 2);
- GLPong.Back.y = -(GLPong.Front.h / 2);
- GLPong.Back.z = -9.0f;
- GLPong.Back.r = 1.0f;
- GLPong.Back.g = 1.0f;
- GLPong.Back.b = 1.0f;
-
- SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE);
- atexit(SDL_Quit);
- GLPong.GLC.context = glcGenContext();
- glcContext(GLPong.GLC.context);
-#ifdef _WIN32
- glcAppendCatalog("C:\\WINDOWS\\Fonts");
-#endif
- count = glcGeti(GLC_CATALOG_COUNT);
- for (i = 0; i < count; i++) {
- printf("%s\n", (char *)glcGetListc(GLC_CATALOG_LIST, i));
+ GLPong->Ball.w = 0.5f;
+ GLPong->Ball.h = 0.5f;
+ GLPong->Ball.x = -(GLPong->Ball.w / 2);
+ GLPong->Ball.y = -(GLPong->Ball.h / 2);
+ GLPong->Ball.z = -3.0f;
+ GLPong->Ball.r = 1.0f;
+ GLPong->Ball.g = 0.0f;
+ GLPong->Ball.b = 0.0f;
+ GLPong->Ball.xv = 0.02f;
+ GLPong->Ball.yv = 0.01f;
+ GLPong->Ball.zv = -0.05f;
+
+ GLPong->Front.w = 0.66f;
+ GLPong->Front.h = 0.5f;
+ GLPong->Front.x = -1.0f;
+ GLPong->Front.y = -1.0f;
+ GLPong->Front.z = -3.0f;
+ GLPong->Front.r = 1.0f;
+ GLPong->Front.g = 1.0f;
+ GLPong->Front.b = 1.0f;
+
+ GLPong->Back.w = 0.66f;
+ GLPong->Back.h = 0.5f;
+ GLPong->Back.x = -(GLPong->Front.w / 2);
+ GLPong->Back.y = -(GLPong->Front.h / 2);
+ GLPong->Back.z = -9.0f;
+ GLPong->Back.r = 1.0f;
+ GLPong->Back.g = 1.0f;
+ GLPong->Back.b = 1.0f;
+
+ if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE) < 0) {
+ fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
+ return -1;
}
+ atexit(SDL_Quit);
+
+ SDL_WM_SetCaption("glpong3d", "glpong3d");
+ SDL_ShowCursor(SDL_DISABLE);
- GLPong.GLC.font = glcGenFontID();
- glcNewFontFromFamily(GLPong.GLC.font, "DejaVu Sans");
- glcFontFace(GLPong.GLC.font, "Normal");
- glcRenderStyle(GLC_TEXTURE);
- glcFont(GLPong.GLC.font);
-
- SDLNet_Init();
- atexit(SDLNet_Quit);
+ temp = IMG_Load("ball.png");
+ if (!temp) {
+ fprintf(stderr, "Unable to open ball.png\n");
+ return -1;
+ }
- SDL_WM_SetCaption("glpong3d", "glpong3d");
- /*temp = IMG_Load("ball.png");
SDL_WM_SetIcon(temp, NULL);
- SDL_FreeSurface(temp);*/
-
+
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
@@ -125,7 +118,19 @@ void GLPong_Init(void) {
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);;
- GLPong.Surface = SDL_SetVideoMode(GLPONG_WIDTH, GLPONG_HEIGHT, 32, SDL_OPENGL);
+ if (GLPong_TextInit() < 0) {
+ fprintf(stderr, "Cannot initialize GLC context: %d\n", glcGetError());
+ return -1;
+ }
+ atexit(GLPong_TextDeinit);
+
+ if (SDLNet_Init() < 0) {
+ fprintf(stderr, "Cannot initialize SDL_net: %s\n", SDL_GetError());
+ return -1;
+ }
+ atexit(SDLNet_Quit);
+
+ GLPong->Surface = SDL_SetVideoMode(GLPONG_WIDTH, GLPONG_HEIGHT, 32, SDL_OPENGL);
glViewport(0, 0, GLPONG_WIDTH, GLPONG_HEIGHT);
glMatrixMode(GL_PROJECTION);
@@ -147,12 +152,7 @@ void GLPong_Init(void) {
printf("OpenGL Version: %s\n", glGetString(GL_VERSION));
printf("OpenGL Extensions: %s\n", glGetString(GL_EXTENSIONS));
- temp = IMG_Load("background.jpg");
- if (temp != NULL) {
- background = SDL_GL_SurfaceToTexture(temp);
- SDL_FreeSurface(temp);
- }
- temp = IMG_Load("ball.png");
+ /*temp = IMG_Load("ball.png");*/ /* disabled since it's already loaded for the icon */
ball_texture = SDL_GL_SurfaceToTexture(temp);
SDL_FreeSurface(temp);
temp = IMG_Load("paddle-skyos.png");
@@ -172,6 +172,8 @@ void GLPong_Init(void) {
glEnd();
glEndList();
glPointSize(10.0f);
+
+ return 0;
}
static int
@@ -263,7 +265,6 @@ void GLPong_Move(void) {
}
void GLPong_Draw(void) {
- char buffer[10];
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
@@ -448,13 +449,6 @@ void GLPong_Draw(void) {
glVertex3f(0.0f, 0.0f, 0.0f);
glEnd();
- sprintf(buffer, "%.0f FPS", GLPong.fps);
- glLoadIdentity();
- glTranslatef(-1.5f, 1.05f, -3.0f);
- glColor3f(1.0f, 1.0f, 1.0f);
- glScalef(0.2f, 0.2f, 0.0f);
- glcRenderString(buffer);
-
SDL_GL_SwapBuffers();
}
diff --git a/glpong3d.h b/glpong3d.h
index 55ac799..80d6cb0 100644
--- a/glpong3d.h
+++ b/glpong3d.h
@@ -45,19 +45,15 @@ typedef struct {
} Mouse_t;
typedef struct {
- GLuint context;
- GLuint font;
-} GLC_t;
-
-struct {
SDL_Surface * Surface;
Ball_t Ball;
Paddle_t Front;
Paddle_t Back;
Mouse_t Mouse;
- GLC_t GLC;
GLfloat fps;
-} GLPong;
+} GLPong_t;
+
+GLPong_t GLPong;
void GLPong_Collide(Ball_t * ball, const Paddle_t * paddle);
diff --git a/text.c b/text.c
new file mode 100644
index 0000000..60d6e4c
--- /dev/null
+++ b/text.c
@@ -0,0 +1,64 @@
+#include <stdio.h>
+
+#include <GL/glc.h>
+
+#include "text.h"
+
+static GLuint font;
+
+int
+GLPong_TextInit(void) {
+#ifdef DEBUG
+ int count, i;
+#endif
+ GLuint context = glcGenContext();
+ glcContext(context);
+
+
+ if (!glcIsContext(context)) {
+ return -1;
+ }
+ glcAppendCatalog("/usr/share/fonts/dejavu/");
+
+#ifdef DEBUG
+ count = glcGeti(GLC_CATALOG_COUNT);
+ for (i = 0; i < count; i++) {
+ printf("%s\n", (char *)glcGetListc(GLC_CATALOG_LIST, i));
+ }
+#endif
+
+ font = glcGenFontID();
+ glcNewFontFromFamily(font, "Deja Vu Sans");
+ glcFontFace(font, "Normal");
+/* glcRenderStyle(GLC_TEXTURE);*/
+ glcRenderStyle(GLC_TRIANGLE);
+ if (glcIsFont(font)) {
+ glcFont(font);
+ } else {
+ return -1;
+ }
+
+ return 0;
+}
+
+void
+GLPong_TextDeinit(void) {
+ glcDeleteFont(font);
+}
+
+void
+GLPong_TextDrawFPS(GLfloat fps) {
+ char buffer[10];
+
+ sprintf(buffer, "%.0f FPS", fps);
+#ifdef DEBUG
+ printf("%s frames per second\n", buffer);
+#endif
+
+ glLoadIdentity();
+ glTranslatef(40.0f, 400.0f, 0.0f);
+ glScalef(25.0f, 25.0f, 0.0f);
+ glColor3f(1.0f, 0.0f, 0.0f);
+ glcRenderString(buffer);
+}
+
diff --git a/text.h b/text.h
new file mode 100644
index 0000000..8891fc7
--- /dev/null
+++ b/text.h
@@ -0,0 +1,9 @@
+#ifndef TEXT_H
+#define TEXT_H
+
+int GLPong_TextInit(void);
+void GLPong_TextDeinit(void);
+void GLPong_TextDrawFPS(GLfloat fps);
+
+#endif
+