]> git.tdb.fi Git - gldbg.git/blob - source/glprint.c
Describe some texture parameters when printing
[gldbg.git] / source / glprint.c
1 /* $Id$
2
3 This file is part of gldbg
4 Copyright © 2009  Mikko Rasa, Mikkosoft Productions
5 Distributed under the GPL
6 */
7
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include <string.h>
11 #include <ctype.h>
12 #include "arraysize.h"
13 #include "enums.h"
14 #include "glprint.h"
15 #include "tmpalloc.h"
16
17 #define UNUSED __attribute__((unused))
18
19 typedef struct sGlPrintData
20 {
21         char *buffer;
22         unsigned bufsize;
23         int free_buf;
24 } GlPrintData;
25
26 static void init_print(GlDecoder *);
27 static void glprint_free(void *);
28 static void print_gldError(void *, GLenum);
29 static void print_unhandled(void *, unsigned short);
30
31 GlDecoder *glprint_new(char *buffer, unsigned bufsize)
32 {
33         GlDecoder *dec;
34         GlPrintData *gpd;
35
36         gpd = (GlPrintData *)malloc(sizeof(GlPrintData));
37         gpd->buffer = buffer;
38         gpd->bufsize = bufsize;
39         gpd->free_buf = 0;
40         if(!gpd->buffer)
41         {
42                 if(!gpd->bufsize)
43                         gpd->bufsize = 1024;
44                 gpd->buffer = (char *)malloc(gpd->bufsize);
45                 gpd->free_buf = 1;
46         }
47         dec = gldecoder_new(gpd, glprint_free);
48
49         init_print(dec);
50         dec->gldError = print_gldError;
51         dec->unhandled = print_unhandled;
52
53         return dec;
54 }
55
56 char *glprint_get_buffer(GlDecoder *dec)
57 {
58         return ((GlPrintData *)dec->user_data)->buffer;
59 }
60
61 static void glprint_free(void *data)
62 {
63         GlPrintData *gpd = (GlPrintData *)data;
64         if(gpd->free_buf)
65         {
66                 free(gpd->buffer);
67                 free(gpd);
68         }
69         tmpfree();
70 }
71
72 static const char *print_array(const char *fmt, const void *data, unsigned elem_size, unsigned count)
73 {
74         const char *cptr;
75         char type = 0;
76         char *buffer;
77         unsigned buf_size;
78         char *ptr;
79         unsigned i;
80
81         if(!data)
82                 return "NULL";
83
84         for(cptr=fmt; (type<2 && *cptr); ++cptr)
85         {
86                 if(*cptr=='%')
87                         type = 1;
88                 else if(type==1 && isalpha(*cptr))
89                         type = *cptr;
90         }
91
92         count /= elem_size;
93         buf_size = count*20;
94         if(type=='s')
95                 buf_size *= 50;
96         buffer = tmpalloc(buf_size);
97         ptr = buffer;
98         *ptr++ = '{';
99         for(i=0; i<count; ++i)
100         {
101                 long long element = 0;
102                 unsigned len;
103
104                 if(i>0)
105                 {
106                         *ptr++ = ',';
107                         *ptr++ = ' ';
108                 }
109                 memcpy(&element, (const char *)data+i*elem_size, elem_size);
110                 if(type>='e' && type<='g' && elem_size==sizeof(float))
111                         *(double *)&element = *(float *)&element;
112                 len = snprintf(ptr, buf_size, fmt, element);
113                 ptr += len;
114                 buf_size -= len;
115         }
116         *ptr++ = '}';
117         *ptr = 0;
118
119         return buffer;
120 }
121
122 static const char *print_array_described(const char *(*describe)(GLenum, const char *), const char *categ, const void *data, unsigned elem_size, unsigned count)
123 {
124         char *buffer;
125         unsigned buf_size;
126         char *ptr;
127         unsigned i;
128
129         if(!data)
130                 return NULL;
131
132         count /= elem_size;
133         buf_size = count*50;
134         buffer = tmpalloc(buf_size);
135         ptr = buffer;
136         *ptr++ = '{';
137         for(i=0; i<count; ++i)
138         {
139                 int element = 0;
140                 unsigned len;
141
142                 if(i>0)
143                 {
144                         *ptr++ = ',';
145                         *ptr++ = ' ';
146                 }
147                 memcpy(&element, (const char *)data+i*elem_size, elem_size);
148                 len = snprintf(ptr, buf_size, "%s", describe(element, categ));
149                 ptr += len;
150                 buf_size -= len;
151         }
152         *ptr++ = '}';
153         *ptr = 0;
154
155         return buffer;
156 }
157
158 static const char *print_parameter(int pname, int param)
159 {
160         char *buffer;
161
162         // XXX Need to move the param names to flavor
163         switch(pname)
164         {
165         case GL_TEXTURE_MIN_FILTER:
166         case GL_TEXTURE_MAG_FILTER:
167         case GL_TEXTURE_WRAP_S:
168         case GL_TEXTURE_WRAP_T:
169                 return describe_enum(param, "");
170         }
171
172         buffer = tmpalloc(11);
173         snprintf(buffer, 11, "%i", param);
174         return buffer;
175 }
176
177 static const char *print_internal_format(int fmt)
178 {
179         char *buffer;
180
181         if(fmt>4)
182                 return describe_enum(fmt, "PixelFormat");
183
184         buffer = tmpalloc(2);
185         snprintf(buffer, 2, "%i", fmt);
186         return buffer;
187 }
188
189 static const char *print_data(const void *data, unsigned size)
190 {
191         if(!data)
192                 return "NULL";
193         else if((unsigned long)data<0x100000)
194         {
195                 char *buffer = tmpalloc(20);
196                 snprintf(buffer, 20, "%p", data);
197                 return buffer;
198         }
199         else if(!size)
200                 return "/* data */";
201         else
202         {
203                 char *buffer = tmpalloc(50);
204                 snprintf(buffer, 50, "/* data: %d bytes */", size);
205                 return buffer;
206         }
207 }
208
209 #include "gensrc/glprint.funcs"
210
211 static void print_gldError(void *user_data, GLenum code)
212 {
213         GlPrintData *gpd = (GlPrintData *)user_data;
214         snprintf(gpd->buffer, gpd->bufsize, "ERROR: %s", describe_enum(code, "ErrorCode"));
215 }
216
217 static void print_unhandled(void *user_data, unsigned short func UNUSED)
218 {
219         GlPrintData *gpd = (GlPrintData *)user_data;
220         gpd->buffer[0] = 0;
221 }