diff options
Diffstat (limited to 'matchblox/common')
-rw-r--r-- | matchblox/common/bitmap.c | 114 | ||||
-rw-r--r-- | matchblox/common/bitmap.h | 24 | ||||
-rw-r--r-- | matchblox/common/font.c | 90 | ||||
-rw-r--r-- | matchblox/common/font.h | 30 | ||||
-rw-r--r-- | matchblox/common/message_queue.c | 101 | ||||
-rw-r--r-- | matchblox/common/message_queue.h | 37 |
6 files changed, 396 insertions, 0 deletions
diff --git a/matchblox/common/bitmap.c b/matchblox/common/bitmap.c new file mode 100644 index 0000000..2d69bc9 --- /dev/null +++ b/matchblox/common/bitmap.c @@ -0,0 +1,114 @@ +#ifdef G_OS_WIN32
+ #define WIN32_LEAN_AND_MEAN 1
+ #include <windows.h>
+ #define GL_BGR 0x80E0
+ #define GL_BGRA 0x80E1
+ #define GL_CLAMP_TO_EDGE 0x812F
+#endif
+
+#include <GL/gl.h>
+#include <GL/glu.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "bitmap.h"
+
+#define BITMAP_FILESIZE 0x02
+#define BITMAP_OFFSET 0x0a
+#define BITMAP_HEADERSIZE 0x0e
+#define BITMAP_WIDTH 0x12
+#define BITMAP_HEIGHT 0x16
+#define BITMAP_DEPTH 0x1c
+#define BITMAP_IMAGE_SIZE 0x22
+
+
+struct BitmapStruct BitmapLoad(char *filename)
+{
+ struct BitmapStruct l_sImage;
+ GLuint texture;
+ FILE *bitmap;
+
+ // try to open the file
+ bitmap = fopen(filename, "r");
+
+ // does the bitmap exist?
+ if (bitmap > 0)
+ {
+ unsigned int dataoffset, filesize, imagesize;
+ short depth;
+ GLsizei width, height;
+ unsigned char *imagedata;
+
+ fseek(bitmap, BITMAP_FILESIZE, SEEK_SET);
+ fread(&filesize, 4, 1, bitmap);
+ fseek(bitmap, BITMAP_OFFSET, SEEK_SET);
+ fread(&dataoffset, 4, 1, bitmap);
+ fseek(bitmap, BITMAP_WIDTH, SEEK_SET);
+ fread(&width, 4, 1, bitmap);
+ fread(&height, 4, 1, bitmap);
+ fseek(bitmap, BITMAP_DEPTH, SEEK_SET);
+ fread(&depth, 2, 1, bitmap);
+ fseek(bitmap, BITMAP_IMAGE_SIZE, SEEK_SET);
+ fread(&imagesize, 4, 1, bitmap);
+
+ imagedata = (unsigned char *)malloc((size_t)imagesize);
+
+ fseek(bitmap, dataoffset, SEEK_SET);
+ fread(imagedata, (size_t)imagesize, 1, bitmap);
+
+ glGenTextures(1, &texture);
+ glBindTexture(GL_TEXTURE_2D, texture);
+
+ if (depth == 24) // 24 bits
+ {
+ gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, width, height, GL_BGR, GL_UNSIGNED_BYTE, imagedata);
+ }
+ else // depth == 32 bits
+ {
+ gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, width, height, GL_BGRA, GL_UNSIGNED_BYTE, imagedata);;
+ }
+
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
+
+ l_sImage.m_iImageId = texture;
+ l_sImage.m_iWidth = width;
+ l_sImage.m_iHeight = height;
+
+ free(imagedata);
+ fclose(bitmap);
+
+ return l_sImage;
+ }
+ else
+ {
+ // couldn't find bitmap
+ exit(0);
+ }
+}
+
+
+void BitmapRender(int f_iXPos, int f_iYPos, int f_iWidth, int f_iHeight, int f_iImageId)
+{
+ glBindTexture(GL_TEXTURE_2D, f_iImageId);
+
+ glBegin(GL_TRIANGLE_STRIP);
+ glTexCoord2i(0, 1); glVertex2i(f_iXPos, f_iYPos);
+ glTexCoord2i(1, 1); glVertex2i(f_iXPos + f_iWidth, f_iYPos);
+ glTexCoord2i(0, 0); glVertex2i(f_iXPos, f_iYPos + f_iHeight);
+ glTexCoord2i(1, 0); glVertex2i(f_iXPos + f_iWidth, f_iYPos + f_iHeight);
+ glEnd();
+
+} // BitmapRender
+
+
+void BitmapConvertWidth(struct BitmapStruct *f_sImage, double f_dHeight)
+{
+ double l_dRatio;
+
+ l_dRatio = (double)f_sImage->m_iHeight / f_dHeight;
+ f_sImage->m_iWidth = (int)(f_sImage->m_iWidth / l_dRatio);
+
+} // ConvertButton
diff --git a/matchblox/common/bitmap.h b/matchblox/common/bitmap.h new file mode 100644 index 0000000..5aa9be6 --- /dev/null +++ b/matchblox/common/bitmap.h @@ -0,0 +1,24 @@ +#ifndef _CBITMAP_H
+#define _CBITMAP_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+
+struct BitmapStruct {
+ int m_iImageId;
+ int m_iWidth;
+ int m_iHeight;
+};
+
+struct BitmapStruct BitmapLoad(char *filename);
+void BitmapRender(int f_iXPos, int f_iYPos, int f_iWidth, int f_iHeight, int f_iImageId);
+void BitmapConvertWidth(struct BitmapStruct *f_sImage, double f_dHeight);
+
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/matchblox/common/font.c b/matchblox/common/font.c new file mode 100644 index 0000000..0332174 --- /dev/null +++ b/matchblox/common/font.c @@ -0,0 +1,90 @@ +#ifdef G_OS_WIN32
+ #define WIN32_LEAN_AND_MEAN 1
+ #include <windows.h>
+#else
+ #define FALSE 0
+ #define TRUE !FALSE
+#endif
+
+#include <GL/gl.h>
+#include <string.h>
+
+#include "font.h"
+#include "bitmap.h"
+
+struct BitmapStruct g_sFont;
+
+int g_iWinWidth;
+int g_iWinHeight;
+
+GLuint g_iBase;
+
+
+void FontInit(int f_iWinWidth, int f_iWinHeight)
+{
+ float cx;
+ float cy;
+ int l_iImageId;
+ int i = 0;
+
+ float x = (float)(1.0f/FONT_XCHAR);
+
+ g_iWinWidth = f_iWinWidth;
+ g_iWinHeight = f_iWinHeight;
+
+ g_sFont = BitmapLoad("img/continuum_textured_alpha.bmp");
+ l_iImageId = g_sFont.m_iImageId;
+
+ g_iBase = glGenLists(FONT_COUNT);
+ glBindTexture(GL_TEXTURE_2D, l_iImageId);
+
+ for (i = 0; i < FONT_COUNT; i++)
+ {
+ cx = (float)(i % 16) / 16.0f;
+ cy = (float)(i / 16) / 16.0f;
+
+ glNewList(g_iBase + i, GL_COMPILE);
+
+ glBegin(GL_TRIANGLE_STRIP);
+ glTexCoord2f(cx, 1 - cy); glVertex2i(0, 0);
+ glTexCoord2f(cx + x, 1 - cy); glVertex2i(FONT_WIDTH, 0);
+ glTexCoord2f(cx, 1 - cy - x); glVertex2i(0, FONT_HEIGHT);
+ glTexCoord2f(cx + x, 1 - cy - x); glVertex2i(FONT_WIDTH, FONT_HEIGHT);
+ glEnd();
+
+ glTranslated(FONT_SPACING, 0, 0);
+ glEndList();
+ }
+
+} // BuildFont
+
+
+void FontDelete(void)
+{
+ glDeleteLists(g_iBase, FONT_COUNT);
+
+} // FontDelete
+
+
+void glPrint(GLint x, GLint y, char *string, struct ColorStruct f_sColor)
+{
+ glColor3d(f_sColor.m_dRed, f_sColor.m_dGreen, f_sColor.m_dBlue);
+ glBindTexture(GL_TEXTURE_2D, g_sFont.m_iImageId);
+ glDisable(GL_DEPTH_TEST);
+ glMatrixMode(GL_PROJECTION);
+
+ glPushMatrix();
+ glLoadIdentity();
+ glOrtho(0, g_iWinWidth, g_iWinHeight, 0, 0, 1);
+ glMatrixMode(GL_MODELVIEW);
+ glPushMatrix();
+ glLoadIdentity();
+ glTranslated(x, y, 0);
+ glListBase(g_iBase - 32);
+ glCallLists((GLsizei)strlen(string), GL_UNSIGNED_BYTE, string);
+ glMatrixMode(GL_PROJECTION);
+ glPopMatrix();
+ glMatrixMode(GL_MODELVIEW);
+ glPopMatrix();
+
+} // glPrint
diff --git a/matchblox/common/font.h b/matchblox/common/font.h new file mode 100644 index 0000000..ec84ccb --- /dev/null +++ b/matchblox/common/font.h @@ -0,0 +1,30 @@ +#ifndef _CFONT_H +#define _CFONT_H + +#define FONT_WIDTH 32 // width of a character (in the bitmap source!) +#define FONT_HEIGHT 32 // height of a character (in the bitmap source!) +#define FONT_SPACING 20 // spacing between characters when printing +#define FONT_XCHAR 16 // number of characters aligned on the x-axis in the bitmap source +#define FONT_COUNT 256 // number of characters in the bitmap source + + +#ifdef __cplusplus +extern "C" { +#endif + +struct ColorStruct { + double m_dRed; + double m_dGreen; + double m_dBlue; +}; + +void FontInit(int f_iWinWidth, int f_iWinHeight); +void FontDelete(GLvoid); +void glPrint(GLint x, GLint y, char *string, struct ColorStruct f_sColor); + + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/matchblox/common/message_queue.c b/matchblox/common/message_queue.c new file mode 100644 index 0000000..ad28b6e --- /dev/null +++ b/matchblox/common/message_queue.c @@ -0,0 +1,101 @@ +/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 2 -*- */ +/* + * message_queue.c + * Copyright (C) Oliver 2008 <o.m.schinagl@student.tue.nl> + * + * main.c is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * main.c is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifdef G_OS_WIN32 +#define WIN32_LEAN_AND_MEAN 1 +#include <windows.h> +#endif + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "message_queue.h" + + +#define MAX_MESSAGES MESSAGE_WINDOW_SIZE + + +struct messageq_s messageq[MAX_MESSAGES]; +static int messageq_index, messageq_serial; +static int messageq_last_free; + + +void messageq_init(void) +{ + int i; + + messageq_index = 0; + messageq_serial = 0; + messageq_last_free = 0; + for( i = 0; i < MAX_MESSAGES; i++) { + messageq[i].sender = 0; + messageq[i].recipient = 0; + messageq[i].payload = NULL; + messageq[i].payload_size = 0; + } +} + + +static void msgcpy(struct messageq_s *dest, const struct messageq_s *src) +{ + dest->sender = src->sender; + dest->recipient = src->recipient; + dest->payload = (void *)malloc(src->payload_size); + memcpy(dest->payload, src->payload, src->payload_size); + dest->payload_size = src->payload_size; +} + +static void msgfree(int index) { + messageq[index].sender = 0; + messageq[index].recipient = 0; + free(messageq[index].payload); + messageq[index].payload_size = 0; +} + + +struct messageq_s *messageq_get(int recipient) +{ + int i; + + i = messageq_index; + while (messageq[i].recipient != recipient && i != (messageq_index -1)) { + i = (i < MAX_MESSAGES -1) ? i +1 : 0; + } + + /* + * We found the message for our recipient. We'll now set the recipient field to 0 since the recipient + * should know the message was for him, he asked for it. This is needed to prefent the above loop + * to find the same message again, since the message will stay alive in the system until its expired. + */ + messageq[i].recipient = MESSAGE_NONE; + + return &messageq[i]; +} + + +void messageq_send(struct messageq_s *message) +{ + /* Before sending the message, we clean out old cruft. We do this here + * since we will be overwriting the contens of the message here anyway. + */ + msgfree(messageq_index); + msgcpy(&messageq[messageq_index], message); + messageq_index = ++messageq_serial %MAX_MESSAGES; +} diff --git a/matchblox/common/message_queue.h b/matchblox/common/message_queue.h new file mode 100644 index 0000000..9fd1241 --- /dev/null +++ b/matchblox/common/message_queue.h @@ -0,0 +1,37 @@ +/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 2 -*- */ +#ifndef _CMESSAGE_QUEUE_H +#define _CMESSAGE_QUEUE_H + + +#define MESSAGE_NONE 0x000000 +#define MESSAGE_MENU 0x001000 +#define MESSAGE_RENDERER 0x002000 + +#define MESSAGE_WINDOW_SIZE 16 + + +#ifdef __cplusplus +extern "C" { +#endif + + +struct messageq_s +{ + int recipient; + int sender; + void *payload; + size_t payload_size; +}; + + +void messageq_init(void); + +void messageq_send(struct messageq_s *message); + +struct messageq_s *messageq_get(int recipient); + +#ifdef __cplusplus +} +#endif + +#endif |