Merge pull request #10 from the-real-orca/mingw
[simavr] / examples / parts / hd44780_glut.c
1 /*
2         hd44780_glut.c
3
4         Copyright Luki <humbell@ethz.ch>
5         Copyright 2011 Michel Pollet <buserror@gmail.com>
6
7         This file is part of simavr.
8
9         simavr is free software: you can redistribute it and/or modify
10         it under the terms of the GNU General Public License as published by
11         the Free Software Foundation, either version 3 of the License, or
12         (at your option) any later version.
13
14         simavr is distributed in the hope that it will be useful,
15         but WITHOUT ANY WARRANTY; without even the implied warranty of
16         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17         GNU General Public License for more details.
18
19         You should have received a copy of the GNU General Public License
20         along with simavr.  If not, see <http://www.gnu.org/licenses/>.
21  */
22
23 #include "hd44780_glut.h"
24
25 #if __APPLE__
26 #include <GLUT/glut.h>
27 #else
28 #include <GL/glut.h>
29 #endif
30
31 #include "font.h"       // generated with gimp
32
33 static GLuint font_texture;
34 static int charwidth = 5;
35 static int charheight = 7;
36
37 void
38 hd44780_gl_init()
39 {
40         glGenTextures(1, &font_texture);
41         glBindTexture(GL_TEXTURE_2D, font_texture);
42         glTexImage2D(GL_TEXTURE_2D, 0, 4,
43                         lcd_font.width,
44                         lcd_font.height, 0, GL_RGBA,
45                 GL_UNSIGNED_BYTE,
46                 lcd_font.pixel_data);
47         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
48         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
49
50         glMatrixMode(GL_TEXTURE);
51         glLoadIdentity();
52         glScalef(1.0f / (GLfloat) lcd_font.width, 1.0f / (GLfloat) lcd_font.height, 1.0f);
53
54         glMatrixMode(GL_MODELVIEW);
55 }
56
57 static inline void
58 glColor32U(uint32_t color)
59 {
60         glColor4f(
61                         (float)((color >> 24) & 0xff) / 255.0f,
62                         (float)((color >> 16) & 0xff) / 255.0f,
63                         (float)((color >> 8) & 0xff) / 255.0f,
64                         (float)((color) & 0xff) / 255.0f );
65 }
66
67 void
68 glputchar(char c,
69                 uint32_t character,
70                 uint32_t text,
71                 uint32_t shadow)
72 {
73         int index = c;
74         int left = index * charwidth;
75         int right = index * charwidth + charwidth;
76         int top = 0;
77         int bottom = 7;
78
79         glEnable(GL_BLEND);
80         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
81
82         glDisable(GL_TEXTURE_2D);
83         glColor32U(character);
84         glBegin(GL_QUADS);
85         glVertex3i(5, 7, 0);
86         glVertex3i(0, 7, 0);
87         glVertex3i(0, 0, 0);
88         glVertex3i(5, 0, 0);
89         glEnd();
90
91         glEnable(GL_BLEND);
92         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
93
94         glEnable(GL_TEXTURE_2D);
95         glBindTexture(GL_TEXTURE_2D, font_texture);
96         if (shadow) {
97                 glColor32U(shadow);
98                 glPushMatrix();
99                 glTranslatef(.2f, .2f, 0);
100                 glBegin(GL_QUADS);
101                 glTexCoord2i(right, top);               glVertex3i(5, 0, 0);
102                 glTexCoord2i(left, top);                glVertex3i(0, 0, 0);
103                 glTexCoord2i(left, bottom);             glVertex3i(0, 7, 0);
104                 glTexCoord2i(right, bottom);    glVertex3i(5, 7, 0);
105                 glEnd();
106                 glPopMatrix();
107         }
108         glColor32U(text);
109         glBegin(GL_QUADS);
110         glTexCoord2i(right, top);               glVertex3i(5, 0, 0);
111         glTexCoord2i(left, top);                glVertex3i(0, 0, 0);
112         glTexCoord2i(left, bottom);             glVertex3i(0, 7, 0);
113         glTexCoord2i(right, bottom);    glVertex3i(5, 7, 0);
114         glEnd();
115
116 }
117
118 void
119 hd44780_gl_draw(
120                 hd44780_t *b,
121                 uint32_t background,
122                 uint32_t character,
123                 uint32_t text,
124                 uint32_t shadow)
125 {
126         int rows = b->w;
127         int lines = b->h;
128         int border = 3;
129
130         glDisable(GL_TEXTURE_2D);
131         glDisable(GL_BLEND);
132         glColor32U(background);
133         glTranslatef(border, border, 0);
134         glBegin(GL_QUADS);
135         glVertex3f(rows * charwidth + (rows - 1) + border, -border, 0);
136         glVertex3f(-border, -border, 0);
137         glVertex3f(-border, lines * charheight + (lines - 1) + border, 0);
138         glVertex3f(rows * charwidth + (rows - 1) + border, lines * charheight
139                 + (lines - 1) + border, 0);
140         glEnd();
141
142         glColor3f(1.0f, 1.0f, 1.0f);
143         const uint8_t offset[] = { 0, 0x40, 0x20, 0x60 };
144         for (int v = 0 ; v < b->h; v++) {
145                 glPushMatrix();
146                 for (int i = 0; i < b->w; i++) {
147                         glputchar(b->vram[offset[v] + i], character, text, shadow);
148                         glTranslatef(6, 0, 0);
149                 }
150                 glPopMatrix();
151                 glTranslatef(0, 8, 0);
152         }
153         hd44780_set_flag(b, HD44780_FLAG_DIRTY, 0);
154 }