File: GL.c

package info (click to toggle)
lebiniou 3.67.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,500 kB
  • sloc: ansic: 28,670; makefile: 1,276; sh: 602; awk: 432; xml: 261; javascript: 23
file content (361 lines) | stat: -rw-r--r-- 8,581 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*
 *  Copyright 1994-2022 Olivier Girondel
 *
 *  This file is part of lebiniou.
 *
 *  lebiniou is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  lebiniou is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with lebiniou. If not, see <http://www.gnu.org/licenses/>.
 */

#include <SDL2/SDL.h>
#include <SDL2/SDL_opengl.h>
#include "biniou.h"
#include "defaults.h"
#include "commands.h"
#include "commands_key.h"

#define NO_MOUSE_CURSOR

uint32_t options = BO_NONE;
uint32_t version = 0;


static SDL_Window *window = NULL;
static Plugin_t *glcube = NULL;
static SDL_GLContext glcontext;


static void
SDL_get_event(Context_t *ctx)
{
  // TODO middle = change color, right = erase (3x3)
  SDL_Event evt;

  while (SDL_PollEvent(&evt)) {
    BKey_t key;

    switch (evt.type) {
      case SDL_KEYDOWN:
        key.val = evt.key.keysym.sym;
        key.mod = evt.key.keysym.mod;

        on_key(ctx, &key);
        break;

      case SDL_QUIT:
        json_decref(Context_process_command(ctx, CMD_APP_QUIT, NULL, BC_SDL2));
        break;

      case SDL_MOUSEMOTION:
        // printf("motion.state: %d\n", evt.motion.state);
        //printf("left: %d\n", SDL_BUTTON_LEFT);
        //printf("right: %d\n", SDL_BUTTON_RIGHT);

        switch (evt.motion.state) {
          /* TODO un switch pour le mouse drag/drop mode */
          case SDL_BUTTON_LEFT:
#ifdef WITH_GL
            ctx->params3d.gl_xe = evt.motion.x;
            ctx->params3d.gl_ye = evt.motion.y;
            Params3d_rotate_GL(&ctx->params3d);
#else
            ctx->params3d.xe = evt.motion.x;
            ctx->params3d.ye = evt.motion.y;
            Params3d_rotate(&ctx->params3d);
#endif
            // printf("left button motion @ %d %d\n",  evt.motion.x, evt.motion.y);
            break;

          case SDL_BUTTON_RIGHT+SDL_BUTTON_LEFT: /* <- WTF ? */
            // printf("right button motion @ %d %d\n",  evt.motion.x, evt.motion.y);
            set_pixel_nc(active_buffer(ctx), evt.motion.x, MAXY-evt.motion.y, 255);
            break;

          default:
            break;
        }
        break;

      case SDL_MOUSEBUTTONDOWN:
        /* printf("type= %d, button= %d\n", evt.button.type, evt.button.button); */
        switch (evt.button.button) {
          case SDL_BUTTON_LEFT:
#ifdef WITH_GL /* TODO switch GL/not-GL */
            ctx->params3d.gl_xs = evt.motion.x;
            ctx->params3d.gl_ys = evt.motion.y;
#else
            ctx->params3d.xs = evt.motion.x;
            ctx->params3d.ys = evt.motion.y;
#endif
            break;

          case SDL_BUTTON_RIGHT:
            // printf("button down @ %d %d\n",  evt.motion.x, evt.motion.y);
            set_pixel_nc(active_buffer(ctx), evt.motion.x, MAXY-evt.motion.y, 255);
            break;
        }
        break;

      case SDL_MOUSEWHEEL:
        if (evt.wheel.y > 0) { // scroll up
#ifdef WITH_GL /* TODO switch GL/not-GL */
          if (ctx->params3d.gl_fov > 1) {
            ctx->params3d.gl_fov--;
          }
          //printf("FOV: %f\n", ctx->params3d.gl_fov);
#else
          ctx->params3d.scale_factor /= 0.9;
          /* printf("scale: %d\n", ctx->params3d->scale_factor); */
#endif
        } else if (evt.wheel.y < 0) { // scroll down
#ifdef WITH_GL /* TODO switch GL/not-GL */
          ctx->params3d.gl_fov++;
          //printf("FOV: %f\n", ctx->params3d.gl_fov);
#else
          if (ctx->params3d.scale_factor > 11) {
            ctx->params3d.scale_factor *= 0.9;
          }
#endif
        }
        break;

      default:
        break;
    }
  }
}


/* "Le Superbe Rectangle Noir sur son Fond Blanc" */
void
test_gl(void)
{
  glOrtho(0.0,1.0,0.0,1.0,-1.0,1.0);
  glBegin(GL_POLYGON);
  glColor3f(1,1,1);
  glVertex2f(0.1,0.1);
  glVertex2f(0.1,0.9);
  glVertex2f(0.9,0.9);
  glVertex2f(0.9,0.1);
  glEnd();
}


void
test_glbis(void)
{
  glOrtho(0.0,1.0,0.0,1.0,-1.0,1.0);
  glBegin(GL_POLYGON);
  glColor3f(1,0,0.5);
  glVertex2f(0.1,0.25);
  glVertex2f(0.75,0.25);
  glVertex2f(0.75,0.75);
  glVertex2f(0.25,0.999);
  glEnd();
}


void
test_gl2(void)
{
  //  glOrtho(0.0,1.0,0.0,1.0,-1.0,1.0);
  glBegin(GL_POLYGON);
  glColor3f(1,0,0);
  glVertex2f(0.0,0.0);
  glColor3f(0,1,0);
  glVertex2f(1.0, 0.0);
  glColor3f(0,0,1);
  glVertex2f(1,1);
  glColor3f(1,1,1);
  glVertex2f(0,1);
  glEnd();
}


static inline void
tex_point(const float x, const float y)
{
  glTexCoord2f(x, y);
  glVertex2f(x, y);
}


void
render(Context_t *ctx)
{
  Context_make_GL_RGBA_texture(ctx, ACTIVE_BUFFER);

  // TODO: quad texture
  glBegin(GL_TRIANGLE_STRIP);
  tex_point(0, 0);
  tex_point(1, 0);
  tex_point(1, 1);

  tex_point(0, 0);
  tex_point(0, 1);
  tex_point(1, 1);
  glEnd();
}


void
reshape(int w, int h)
{
  glViewport(0, 0, (GLsizei)w, (GLsizei)h);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
}


void
run(Context_t *ctx)
{
  if (!ctx->gl_done) {
    if (ctx->force_cube && (NULL != glcube)) {
      glcube->run(ctx);
    } else {
      reshape(WIDTH, HEIGHT);
      render(ctx);
    }
  }

  SDL_GL_SwapWindow(window);

  SDL_get_event(ctx);
}


void
fullscreen(const int fs)
{
  if (fs) {
    printf("[S] Set full-screen\n");
  } else {
    printf("[S] Unset full-screen\n");
  }
  SDL_SetWindowFullscreen(window, fs ? SDL_WINDOW_FULLSCREEN : 0);
}


void
destroy(Context_t *ctx)
{
  SDL_GL_DeleteContext(glcontext);
  SDL_DestroyWindow(window);
  SDL_Quit();
}


void
switch_cursor(void)
{
  SDL_ShowCursor(SDL_ShowCursor(SDL_QUERY) ? SDL_DISABLE : SDL_ENABLE);
}


static void
init_GL(void)
{
  glShadeModel(GL_SMOOTH);
  glClearColor(0, 0, 0, 0);
  glClearDepth(1.0f);
  glEnable(GL_DEPTH_TEST);
  glDepthFunc(GL_LEQUAL);
  glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}


int8_t
create(Context_t *ctx)
{
  char *icon_file;
  SDL_Surface *icon = NULL;
  Uint32 colorkey;
  char *window_title;
  const int flags = SDL_WINDOW_OPENGL | SDL_GL_DOUBLEBUFFER;

  glcube = Plugins_find("GLCube");
  if (NULL != glcube) {
    printf("[i] glcube found @%p\n", glcube);
    if (NULL != glcube->create) {
      (void)glcube->create(ctx);
    }
  }

  /* First, initialize SDL's video subsystem. */
  if (!SDL_WasInit(SDL_INIT_VIDEO))
    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
      /* Failed, exit. */
      xerror("Video initialization failed: %s\n", SDL_GetError());
    }

  /*
   * Now, we want to setup our requested
   * window attributes for our OpenGL window.
   * We want *at least* 5 bits of red, green
   * and blue. We also want at least a 16-bit
   * depth buffer.
   *
   * The last thing we do is request a double
   * buffered window. '1' turns on double
   * buffering, '0' turns it off.
   *
   * Note that we do not use SDL_DOUBLEBUF in
   * the flags to SDL_SetVideoMode. That does
   * not affect the GL attribute state, only
   * the standard 2D blitting setup.
   */
  SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
  SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
  SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
  SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
  SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);

  /* This makes our buffer swap syncronized with the monitor's vertical refresh */
  SDL_GL_SetSwapInterval(1);

  window_title = g_strdup_printf("Le Biniou OpenGL (%dx%d)", WIDTH, HEIGHT);
  window = SDL_CreateWindow(window_title,
                            SDL_WINDOWPOS_CENTERED,
                            SDL_WINDOWPOS_CENTERED,
                            WIDTH, HEIGHT, flags);
  g_free(window_title);

  if (NULL == window) {
    xerror("Couldn't set %dx%d video mode: %s\n", WIDTH, HEIGHT, SDL_GetError());
  }

  icon_file = g_strdup_printf("%s/lebiniou.bmp", DEFAULT_DATADIR);
  icon = SDL_LoadBMP(icon_file);
  g_free(icon_file);
  colorkey = SDL_MapRGB(icon->format, 0, 0, 0);
  SDL_SetColorKey(icon, SDL_TRUE, colorkey);
  SDL_SetWindowIcon(window, icon);
  SDL_FreeSurface(icon);

  /* Create an OpenGL context associated with the window. */
  glcontext = SDL_GL_CreateContext(window);

  init_GL();

#ifdef NO_MOUSE_CURSOR
  SDL_ShowCursor(SDL_DISABLE);
#endif

  glViewport(0, 0, WIDTH, HEIGHT);

  return 1;
}