[Add] More sane toolkit API. -- Improved landing graphics.

[Change] Moved colours out of opengl specific stuff.
This commit is contained in:
Allanis 2013-02-18 18:37:19 +00:00
parent 074e60afa5
commit d19f8c4ae8
8 changed files with 119 additions and 78 deletions

18
src/colour.c Normal file
View File

@ -0,0 +1,18 @@
#include "colour.h"
// Default colors.
glColour cWhite = { .r = 1.00, .g = 1.00, .b = 1.00, .a = 1 };
glColour cGrey90 = { .r = 0.90, .g = 0.90, .b = 0.90, .a = 1 };
glColour cGrey80 = { .r = 0.80, .g = 0.80, .b = 0.80, .a = 1 };
glColour cGrey70 = { .r = 0.70, .g = 0.70, .b = 0.70, .a = 1 };
glColour cGrey60 = { .r = 0.60, .g = 0.60, .b = 0.60, .a = 1 };
glColour cGrey50 = { .r = 0.50, .g = 0.50, .b = 0.50, .a = 1 };
glColour cGrey40 = { .r = 0.40, .g = 0.40, .b = 0.40, .a = 1 };
glColour cGrey30 = { .r = 0.30, .g = 0.30, .b = 0.30, .a = 1 };
glColour cGrey20 = { .r = 0.20, .g = 0.20, .b = 0.20, .a = 1 };
glColour cGrey10 = { .r = 0.10, .g = 0.10, .b = 0.10, .a = 1 };
glColour cBlack = { .r = 0.00, .g = 0.00, .b = 0.00, .a = 1 };
glColour cGreen = { .r = 0.20, .g = 0.80, .b = 0.20, .a = 1 };
glColour cRed = { .r = 0.80, .g = 0.20, .b = 0.20, .a = 1 };

26
src/colour.h Normal file
View File

@ -0,0 +1,26 @@
#pragma once
// Colours.
typedef struct {
double r, g, b, a;
} glColour;
// Default colors.
// -- Greyscale.
extern glColour cWhite;
#define cGrey cGrey70
extern glColour cBlack;
extern glColour cGrey90;
extern glColour cGrey80;
extern glColour cGrey70;
extern glColour cGrey60;
extern glColour cGrey50;
extern glColour cGrey40;
extern glColour cGrey30;
extern glColour cGrey20;
extern glColour cGrey10;
extern glColour cGreen;
extern glColour cRed;

View File

@ -2,6 +2,11 @@
#include "pause.h"
#include "land.h"
#define LAND_WIDTH 500
#define LAND_HEIGHT 400
#define BUTTON_WIDTH 80
#define BUTTON_HEIGHT 40
int landed = 0;
static int land_wid = 0;
@ -10,10 +15,14 @@ static Planet* planet = NULL;
// Land the player.
void land(Planet* p) {
if(landed) return;
planet = p;
land_wid = window_create(-1, -1, 400, 300);
window_addButton(land_wid, 400-80-20, 20, 80, 40, "takeoff", "Takeoff", (void(*)(char*))takeoff);
land_wid = window_create(-1, -1, LAND_WIDTH, LAND_HEIGHT);
// Pretty display.
window_addText(land_wid, 0., -20., LAND_WIDTH, 1, p->name, NULL, &cBlack);
// Buttons.
window_addButton(land_wid, -20, 20, BUTTON_WIDTH, BUTTON_HEIGHT,
"takeoff", "Takeoff", (void(*)(char*))takeoff);
landed = 1;
}

View File

@ -18,23 +18,6 @@
#define FONT_DEF "../gfx/fonts/font.ttf"
// Default colors.
glColour cWhite = { .r = 1.00, .g = 1.00, .b = 1.00, .a = 1 };
glColour cGrey90 = { .r = 0.90, .g = 0.90, .b = 0.90, .a = 1 };
glColour cGrey80 = { .r = 0.80, .g = 0.80, .b = 0.80, .a = 1 };
glColour cGrey70 = { .r = 0.70, .g = 0.70, .b = 0.70, .a = 1 };
glColour cGrey60 = { .r = 0.60, .g = 0.60, .b = 0.60, .a = 1 };
glColour cGrey50 = { .r = 0.50, .g = 0.50, .b = 0.50, .a = 1 };
glColour cGrey40 = { .r = 0.40, .g = 0.40, .b = 0.40, .a = 1 };
glColour cGrey30 = { .r = 0.30, .g = 0.30, .b = 0.30, .a = 1 };
glColour cGrey20 = { .r = 0.20, .g = 0.20, .b = 0.20, .a = 1 };
glColour cGrey10 = { .r = 0.10, .g = 0.10, .b = 0.10, .a = 1 };
glColour cBlack = { .r = 0.00, .g = 0.00, .b = 0.00, .a = 1 };
glColour cGreen = { .r = 0.20, .g = 0.80, .b = 0.20, .a = 1 };
glColour cRed = { .r = 0.80, .g = 0.20, .b = 0.20, .a = 1 };
// offsets to Adjust the pilot's place onscreen to be in the middle, even with the GUI.
extern double gui_xoff;
extern double gui_yoff;
@ -656,15 +639,15 @@ static void glFontMakeDList(FT_Face face, char ch, GLuint list_base, GLuint* tex
// Draw the texture mapped quad.
glBindTexture(GL_TEXTURE_2D, tex_base[(int)ch]);
glBegin(GL_TRIANGLE_STRIP);
glBegin(GL_QUADS);
glTexCoord2d(0, 0);
glVertex2d(0, bitmap.rows);
glTexCoord2d(x, 0);
glVertex2d(bitmap.width, bitmap.rows);
glTexCoord2d(0, y);
glVertex2d(0, 0);
glTexCoord2d(x, y);
glVertex2d(bitmap.width, 0);
glTexCoord2d(0, y);
glVertex2d(0, 0);
glEnd();
glPopMatrix();

View File

@ -1,6 +1,7 @@
#pragma once
#include <SDL.h>
#include <SDL_opengl.h>
#include "colour.h"
#include "physics.h"
// Recommended for compatibility bullshit.
@ -32,31 +33,8 @@ typedef struct {
} glInfo;
extern glInfo gl_screen; // Local structure set with gl_init etc.
// Colours.
typedef struct {
double r, g, b, a;
} glColour;
#define COLOUR(x) glColor4d((x).r, (x).g, (x).b, (x).a)
// Default colors.
// -- Greyscale.
extern glColour cWhite;
#define cGrey cGrey70
extern glColour cBlack;
extern glColour cGrey90;
extern glColour cGrey80;
extern glColour cGrey70;
extern glColour cGrey60;
extern glColour cGrey50;
extern glColour cGrey40;
extern glColour cGrey30;
extern glColour cGrey20;
extern glColour cGrey10;
extern glColour cGreen;
extern glColour cRed;
// Spritesheet info.
typedef struct {
double w, h; // Real size of the image (excluding POT buffer.

View File

@ -6,7 +6,8 @@
typedef enum {
WIDGET_NULL,
WIDGET_BUTTON,
WIDGET_TEXT
WIDGET_TEXT,
WIDGET_IMAGE
} WidgetType;
typedef enum {
@ -34,6 +35,11 @@ typedef struct {
struct {
glFont* font;
glColour* colour;
int centered;
};
struct {
// Widget image.
glTexture* texture;
};
};
} Widget;
@ -57,8 +63,9 @@ static Window* windows = NULL;
static int nwindows = 0;
static int mwindows = 0;
static Widget* window_newWidget(const unsigned int wid);
static Widget* window_newWidget(Window* w);
static void widget_cleanup(Widget* widget);
static Window* window_get(const unsigned int wid);
// Render.
static void window_render(Window* w);
static void toolkit_renderButton(Widget* btn, double bx, double by);
@ -69,56 +76,66 @@ static void toolkit_renderText(Widget* txt, double bx, double by);
void window_addButton(const unsigned int wid, const int x, const int y, const int w,
const int h, char* name, char* display, void (*call)(char*)) {
Widget* wgt = window_newWidget(wid);
Window* wdw = window_get(wid);
Widget* wgt = window_newWidget(wdw);
wgt->type = WIDGET_BUTTON;
wgt->name = strdup(name);
wgt->string = strdup(display);
// Set the properties.
wgt->x = (double) x;
wgt->y = (double) y;
wgt->w = (double) w;
wgt->h = (double) h;
if(x < 0) wgt->x = wdw->w - wgt->w + x;
else wgt->x = (double)x;
if(y < 0) wgt->y = wdw->h - wgt->h + y;
else wgt->y = (double)y;
wgt->fptr = call;
}
void window_addText(const unsigned int wid, const int x, const int y, const int w,
const int h, char* name, glFont* font, glColour* colour) {
Widget* wgt = window_newWidget(wid);
const int centered, char* name, glFont* font, glColour* colour) {
Window* wdw = window_get(wid);
Widget* wgt = window_newWidget(wdw);
wgt->type = WIDGET_TEXT;
wgt->name = strdup(name); // Display the widgets name.
// Set the properties.
wgt->x = (double) x;
wgt->y = (double) y;
wgt->w = (double) w;
wgt->h = (double) h;
wgt->font = font;
if(font == NULL) wgt->font = &gl_defFont;
else wgt->font = font;
if(x < 0) wgt->x = wdw->w - wgt->w + x;
else wgt->x = (double)x;
if(y < 0) wgt->y = wdw->h - wgt->font->h + y;
else wgt->y = (double) y;
wgt->colour = colour;
wgt->centered = centered;
}
// Return pointer to newly allocated widget.
static Widget* window_newWidget(const unsigned int wid) {
static Widget* window_newWidget(Window* w) {
Widget* wgt = NULL;
w->widgets = realloc(w->widgets, sizeof(Widget)*(++w->nwidgets));
if(w->widgets == NULL) WARN("Out of memory");
wgt = &w->widgets[w->nwidgets - 1];
wgt->type = WIDGET_NULL;
wgt->status = WIDGET_STATUS_NORMAL;
return wgt;
}
// Return the window of id wid.
static Window* window_get(const unsigned int wid) {
int i;
for(i = 0; i < nwindows; i++)
if(windows[i].id == wid)
break;
if(i == nwindows) return NULL;
Widget* w = NULL;
windows[i].widgets = realloc(windows[i].widgets, sizeof(Widget)*(++windows[i].nwidgets));
if(windows[i].widgets == NULL) WARN("Out of memory");
w = &windows[i].widgets[windows[i].nwidgets - 1];
w->type = WIDGET_NULL;
w->status = WIDGET_STATUS_NORMAL;
return w;
return &windows[i];
return NULL;
}
// Create a window.
@ -162,7 +179,7 @@ unsigned int window_create(const int x, const int y, const int w, const int h) {
static void widget_cleanup(Widget* widget) {
if(widget->name) free(widget->name);
if((widget->type == WIDGET_TEXT) && widget->string)
if((widget->type == WIDGET_BUTTON) && widget->string)
free(widget->string);
}
@ -365,6 +382,8 @@ static void window_render(Window* w) {
case WIDGET_TEXT:
toolkit_renderText(&w->widgets[i], x, y);
break;
case WIDGET_IMAGE:
break;
}
}
}
@ -372,7 +391,6 @@ static void window_render(Window* w) {
static void toolkit_renderButton(Widget* btn, double bx, double by) {
glColour* c, *dc, *oc, *lc;
double x, y;
int j;
x = bx + btn->x;
y = by + btn->y;
@ -474,8 +492,16 @@ static void toolkit_renderButton(Widget* btn, double bx, double by) {
}
static void toolkit_renderText(Widget* txt, double bx, double by) {
gl_printMax(txt->font, txt->w, bx+(double)gl_screen.w/2. + txt->x,
by + (double)gl_screen.h/2. + txt->y, txt->colour, txt->name);
if(txt->centered)
gl_printMid(txt->font, txt->w,
bx + (double)gl_screen.w/2. + txt->x,
by + (double)gl_screen.h/2. + txt->y,
txt->colour, txt->name);
else
gl_printMax(txt->font, txt->w,
bx + (double)gl_screen.w/2. + txt->x,
by + (double)gl_screen.h/2. + txt->y,
txt->colour, txt->name);
}
// Render the window.

View File

@ -12,7 +12,8 @@ void window_addButton(const unsigned int wid, const int x, const int y,
void(*call)(char*));
void window_addText(const unsigned int wid, const int x, const int y,
const int w, const int h, char* name, glFont* font, glColour* colour);
const int w, const int cenetered,
char* name, glFont* font, glColour* colour);
// Destroy window.
void window_destroy(const unsigned int wid);