]> icculus.org git repositories - dana/openbox.git/blob - render2/texture.c
remove debug print
[dana/openbox.git] / render2 / texture.c
1 #include "instance.h"
2 #include "texture.h"
3 #include "surface.h"
4 #include "font.h"
5 #include "glft/glft.h"
6 #include <stdlib.h>
7 #include <assert.h>
8 #include <string.h>
9
10 void RrTextureFreeContents(struct RrTexture *tex)
11 {
12     switch (tex->type) {
13     case RR_TEXTURE_NONE:
14         break;
15     case RR_TEXTURE_TEXT:
16         free(tex->data.text.string);
17         break;
18     case RR_TEXTURE_RGBA:
19         glDeleteTextures(1, &tex->data.rgba.texid);
20         break;
21     }
22     tex->type = RR_TEXTURE_NONE;
23 }
24
25 void RrTextureSetRGBA(struct RrSurface *sur,
26                       int texnum,
27                       RrData32 *data,
28                       int x,
29                       int y,
30                       int w,
31                       int h)
32 {
33     unsigned int num;
34     struct RrTexture *tex = RrSurfaceTexture(sur, texnum);
35
36     if (!tex) return;
37     RrTextureFreeContents(tex);
38     tex->type = RR_TEXTURE_RGBA;
39     tex->data.rgba.x = x;
40     tex->data.rgba.y = y;
41     tex->data.rgba.w = w;
42     tex->data.rgba.h = h;
43     glGenTextures(1, &num);
44     tex->data.rgba.texid = num;
45     glBindTexture(GL_TEXTURE_2D, num);
46     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
47     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
48     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
49     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
50     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
51     glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND);
52     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h,
53                  0, GL_RGBA, GL_UNSIGNED_BYTE, data);
54 }
55
56 void RrTextureSetText(struct RrSurface *sur,
57                       int texnum,
58                       struct RrFont *font,
59                       enum RrLayout layout,
60                       struct RrColor *color,
61                       const char *text)
62 {
63     struct RrTexture *tex = RrSurfaceTexture(sur, texnum);
64     int l;
65
66     if (!tex) return;
67     RrTextureFreeContents(tex);
68     tex->type = RR_TEXTURE_TEXT;
69     tex->data.text.font = font;
70     tex->data.text.layout = layout;
71     tex->data.text.color = *color;
72
73     l = strlen(text) + 1;
74     tex->data.text.string = malloc(l);
75     memcpy(tex->data.text.string, text, l);
76 }
77
78 void RrTextureSetNone(struct RrSurface *sur,
79                       int texnum)
80 {
81     struct RrTexture *tex = RrSurfaceTexture(sur, texnum);
82
83     if (!tex) return;
84     RrTextureFreeContents(tex);
85 }
86
87 void RrTexturePaint(struct RrSurface *sur, struct RrTexture *tex)
88 {
89     struct GlftColor col;
90
91     glEnable(GL_TEXTURE_2D);
92     
93     switch (tex->type) {
94     case RR_TEXTURE_NONE:
95         break;
96     case RR_TEXTURE_TEXT:
97         assert(tex->data.text.font);
98         col.r = tex->data.text.color.r;
99         col.g = tex->data.text.color.g;
100         col.b = tex->data.text.color.b;
101         col.a = tex->data.text.color.a;
102
103         GlftRenderString(tex->data.text.font->font, tex->data.text.string, 
104                          strlen(tex->data.text.string), &col,
105                          RrSurfaceX(sur) + 2, RrSurfaceY(sur) + 4);
106         break;
107     }
108     glDisable(GL_TEXTURE_2D);
109 }