TinyGL 0.4.1 for MinGW
gdi.c
Go to the documentation of this file.
1/*
2 * TinyGL driver for WIN32 GDI
3 *
4 * @orignal Fabrice Bellard
5 *
6 * @modify 2017-Jul-17
7 * @author Atmarkartworks t.m
8 *
9 *
10 */
11
12#include <stdio.h>
13#include <windows.h>
14
15#include <GL/gl.h>
16#include "zgl.h"
17#include <GL/gdi.h>
18
19
20
21typedef struct {
23
24 int xsize,ysize;
25 int pixtype; /* pixel type in TinyGL */
26
28 LPBITMAPINFO dib_info;
29
31
32
33
35{
36 TinyGDIContext *ctx;
37
38 if (shareList != NULL) {
39 gl_fatal_error("No sharing available in TinyGL");
40 }
41 ctx=gl_malloc(sizeof(TinyGDIContext));
42 if (!ctx)
43 return NULL;
44 ctx->gl_context=NULL;
45 ctx->dib_info = NULL;
46 return (GDIContext) ctx;
47}
48
50{
51 TinyGDIContext *ctx = (TinyGDIContext *) ctx1;
52
53 if (ctx->gl_context->zb != NULL) {
54 ZB_close(ctx->gl_context->zb);
55 }
56
57 if (ctx->dib_info != NULL) {
58 gl_free(ctx->dib_info);
59 }
60
61 if (ctx->gl_context != NULL) {
62 glClose();
63 }
64
65 gl_free(ctx);
66}
67
68
69/* resize the GDI pain : we try to use the xsize and ysize
70 given. We return the effective size which is guaranted to be smaller */
71
72static int gdi_resize_viewport(GLContext *c,int *xsize_ptr,int *ysize_ptr)
73{
74 TinyGDIContext *ctx;
75 int xsize,ysize;
76
77 ctx=(TinyGDIContext *)c->opaque;
78
79 xsize=*xsize_ptr;
80 ysize=*ysize_ptr;
81
82
83
84 /* we ensure that xsize and ysize are multiples of 2 for the zbuffer.
85 TODO: find a better solution */
86 xsize&=~3;
87 ysize&=~3;
88
89 ctx->dib_info->bmiHeader.biWidth=xsize;
90 ctx->dib_info->bmiHeader.biHeight=ysize;
91 ctx->xsize = xsize;
92 ctx->ysize = ysize;
93
94 if (xsize == 0 || ysize == 0) return -1;
95
96 *xsize_ptr=xsize;
97 *ysize_ptr=ysize;
98
99
100 /* resize the Z buffer */
101 ZB_resize(c->zb,NULL,xsize,ysize);
102 return 0;
103}
104
105
106
107
108/* we assume here that drawable is a window */
109int gdiMakeCurrent( HWND drawable, GDIContext ctx1, int width, int height)
110{
111 TinyGDIContext *ctx = (TinyGDIContext *) ctx1;
112 int mode, xsize, ysize;
113 ZBuffer *zb;
114
115 if (ctx->gl_context == NULL) {
116 /* create the TinyGL context */
117 xsize = width;
118 ysize = height;
119
120
121 if (ctx->dib_info == NULL) {
122 ctx->dib_info = gl_malloc(sizeof(BITMAPINFO));
123 if (!ctx->dib_info) {
124 fprintf(stderr, "Error while initializeing DIB info.\n");
125 exit(1);
126
127 }
128
129 ctx->dib_info->bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
130 ctx->dib_info->bmiHeader.biWidth=width;
131 ctx->dib_info->bmiHeader.biHeight=height;
132 ctx->dib_info->bmiHeader.biPlanes=1;
133 ctx->dib_info->bmiHeader.biBitCount=16;
134 ctx->dib_info->bmiHeader.biCompression=BI_RGB;
135 ctx->dib_info->bmiHeader.biSizeImage=0;
136 ctx->dib_info->bmiHeader.biXPelsPerMeter=0;
137 ctx->dib_info->bmiHeader.biYPelsPerMeter=0;
138 ctx->dib_info->bmiHeader.biClrUsed=0;
139 ctx->dib_info->bmiHeader.biClrImportant=0;
140
141 }
142
143 /* currently, we only support 16 bit rendering */
144 mode = ZB_MODE_5R6G5B;
145 //zb=ZB_open(xsize,ysize,mode,0,NULL,NULL,NULL);
146 zb=ZB_open(xsize,ysize,mode,0,NULL,NULL,NULL);
147 if (zb == NULL) {
148 fprintf(stderr, "Error while initializing Z buffer\n");
149 exit(1);
150 }
151
153
154
155 /* initialisation of the TinyGL interpreter */
156 glInit(zb);
158 ctx->gl_context->opaque=(void *) ctx;
159 ctx->gl_context->gl_resize_viewport=gdi_resize_viewport;
160
161 /* set the viewport : we force a call to gdi_resize_viewport */
162 ctx->gl_context->viewport.xsize=-1;
163 ctx->gl_context->viewport.ysize=-1;
164
165 glViewport(0, 0, xsize, ysize);
166 }
167
168 return 1;
169}
170
171void gdiSwapBuffers(HWND drawable)
172{
173 GLContext *gl_context;
174 TinyGDIContext *ctx;
175 HDC hdc;
176
177 /* retrieve the current GDIContext */
178 gl_context=gl_get_context();
179 ctx=(TinyGDIContext *)gl_context->opaque;
180
181 hdc = GetDC(drawable);
182
183 StretchDIBits(hdc,0,ctx->ysize,ctx->xsize,-ctx->ysize, 0,0,ctx->xsize,ctx->ysize, gl_context->zb->pbuf, ctx->dib_info, DIB_RGB_COLORS,SRCCOPY);
184
185 ReleaseDC(drawable, hdc);
186
187 ShowWindow (drawable, SW_SHOW);
188 UpdateWindow (drawable);
189}
190
void gl_fatal_error(char *format,...)
Definition: error.c:4
GDIContext gdiCreateContext(GDIContext shareList, int flags)
Definition: gdi.c:34
void gdiSwapBuffers(HWND drawable)
Definition: gdi.c:171
int gdiMakeCurrent(HWND drawable, GDIContext ctx1, int width, int height)
Definition: gdi.c:109
void gdiDestroyContext(GDIContext ctx1)
Definition: gdi.c:49
void * GDIContext
Definition: gdi.h:11
#define MWPF_TRUECOLOR565
Definition: gdi.h:27
void glClose(void)
Definition: init.c:184
void glViewport(int x, int y, int width, int height)
Definition: api.c:341
void glInit(void *zbuffer)
Definition: init.c:30
GLContext * gl_get_context(void)
Definition: list.c:25
void gl_free(void *p)
Definition: memory.c:8
void * gl_malloc(int size)
Definition: memory.c:13
Definition: zgl.h:159
ZBuffer * zb
Definition: zgl.h:161
void * opaque
Definition: zgl.h:272
GLViewport viewport
Definition: zgl.h:203
int(* gl_resize_viewport)(struct GLContext *c, int *xsize, int *ysize)
Definition: zgl.h:274
int ysize
Definition: zgl.h:91
int xsize
Definition: zgl.h:91
GLContext * gl_context
Definition: gdi.c:22
LPBITMAPINFO dib_info
Definition: gdi.c:28
HWND drawable
Definition: gdi.c:27
int ysize
Definition: gdi.c:24
int pixtype
Definition: gdi.c:25
int xsize
Definition: gdi.c:24
PIXEL * pbuf
Definition: zbuffer.h:80
void ZB_close(ZBuffer *zb)
Definition: zbuffer.c:75
void ZB_resize(ZBuffer *zb, void *frame_buffer, int xsize, int ysize)
Definition: zbuffer.c:89
ZBuffer * ZB_open(int xsize, int ysize, int mode, int nb_colors, unsigned char *color_indexes, int *color_table, void *frame_buffer)
Definition: zbuffer.c:12
#define ZB_MODE_5R6G5B
Definition: zbuffer.h:27
if(p1->y > p2->y||(p1->y==p2->y &&p1->x > p2->x))
Definition: zline.h:17