summaryrefslogtreecommitdiffstats
path: root/sdl/platform.c
blob: 2d770a8ee0fdebb9e2fbfe864eed3d5c4281b5b6 (plain)
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
/* platform.c
 *
 * Platform
 *
 * Notes: Assumes multisampling is always enabled
 *
 */

#include <lauxlib.h>
#include <lualib.h>
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <GL/glew.h>
#include <string.h>
#include "gldraw.h"
#include "gltools.h"
#include "luatools.h"
#include "platform.h"
#include "sdltools.h"

/* few light arrays */
const GLfloat fNoLight[]     = { 0.0f, 0.0f, 0.0f, 1.0f    };
const GLfloat fLowLight[]    = { 0.25f, 0.25f, 0.25f, 1.0f };
const GLfloat fMedLight[]    = { 0.50f, 0.50f, 0.50f, 1.0f };
const GLfloat fShinyLight[]  = { 0.70f, 0.70f, 0.70f, 1.0f };
const GLfloat fBrightLight[] = { 1.0f, 1.0f, 1.0f, 1.0f    };

/* light values and coordinates */
const GLfloat lightPos[]     = { -10.f, 5.0f, 5.0f, 1.0f   };

/* variables that should be already defined and declared for us by main program */
extern unsigned int xres_w;
extern unsigned int yres_w;
extern unsigned int bpp_w;
extern unsigned int af_w;
extern const char *window_caption;
extern const char *window_icon_path;
extern const unsigned int sdl_video_flags;
extern unsigned int maxfps_w;

void platform_init(struct platform *p)
{
    /* Lua */
    /* create new lua state */
    p->L = luaL_newstate();

    /* load lua libraries */
    luaL_openlibs(p->L);

    /* initialize SDL, GLEW, and OpenGL */
    load_config(p);
    p->screen = setup_sdl();
    setup_glew();
    setup_opengl();

    /* add a simple timer / callback function */
    p->timer_id = SDL_AddTimer(5000, sdlTimerCallback, &(p->camera));

    /* apply a custom cursor */
    p->my_cursor = sdlInitCursor(arrow);
    SDL_SetCursor(p->my_cursor);

    /* set the camera to <0,0,0> */
    glframe_reset(&p->camera);

    /* init client */
    p->client_init(p->c);
}

/* it's a good idea to destroy things in reverse order */
void platform_destroy(struct platform *p)
{
    /* destroy client */
    p->client_destroy(p->c);

    SDL_FreeSurface(p->screen);
    lua_close(p->L);
}

/* load Lua config file from the disk */
void load_config(struct platform *p)
{
    if (luaLoadConfig(p->L, "config.lua") ||
        luaFillTablePlatform(p->L, &p->config_table) != -1)
    {
        /* print was was loaded from config.lua to stdout */
        luaPrintTablePlatform(&p->config_table);

        /* override any globals that config.lua redefines */
        if (p->config_table.xres)
            xres_w = p->config_table.xres;

        if (p->config_table.yres)
            yres_w = p->config_table.yres;

        if (p->config_table.bpp)
            bpp_w = p->config_table.bpp;

        if (p->config_table.af)
            af_w = p->config_table.af;

        if (p->config_table.name)
            window_caption = p->config_table.name;

        if (p->config_table.icon)
            window_icon_path = p->config_table.icon;

        if (p->config_table.maxfps)
            maxfps_w = p->config_table.maxfps;
    }
}

void setup_opengl(void)
{
    /* setup fog */
    glEnable(GL_FOG);
    glFogfv(GL_FOG_COLOR, fLowLight); /* set fog color to match background */
    glFogf(GL_FOG_START, 4.0f);
    glFogf(GL_FOG_END, 20.0f);
    glFogi(GL_FOG_MODE, GL_LINEAR);   /* fog equation */

    glEnable(GL_DEPTH_TEST); /* hidden surface removal, aka Z-buffer */
    glFrontFace(GL_CCW);     /* counter clock-wise polygons face out */
    glEnable(GL_CULL_FACE);  /* do not calculate inside of a pyramid */

    /* enable lighting, primitives now need to define color properties */
    glEnable(GL_LIGHTING);

    /* global illumination, ambient RGBA intensity of the entire scene */
    glLightModelfv(GL_LIGHT_MODEL_AMBIENT, fLowLight);

    /* setup and enable light 0 */
    glLightfv(GL_LIGHT0, GL_AMBIENT,  fMedLight);
    glLightfv(GL_LIGHT0, GL_DIFFUSE,  fShinyLight);
    glLightfv(GL_LIGHT0, GL_SPECULAR, fBrightLight);
    glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
    glEnable(GL_LIGHT0);

    /* set material properties to follow all glColor values */
    /* all primitives specified after the glMaterial call are affected by the
     * last values set, until another call to glMaterial is made */
    glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);

    /* enable color tracking, should be called after glColorMaterial */
    glEnable(GL_COLOR_MATERIAL);

    /* set globally, sets specular reflection to a white color,
     * this call follows all subsequent primitives */
    glMaterialfv(GL_FRONT, GL_SPECULAR, fBrightLight);

    /* default (0), by increasing this value you reduce the size and increase
     * the focus of the specular highlight, causing a shiny spot to appear */
    glMateriali(GL_FRONT, GL_SHININESS, 64);

    /* multisampling for polygons, conflicts with *polygon* anti-aliasing */
    /* enabled by default, that's what man page says, hmmm! */
    glEnable(GL_MULTISAMPLE);

    if (glIsEnabled(GL_MULTISAMPLE) == GL_FALSE)
    {
        fprintf(stderr, "OpenGL error: multisampling cannot be enabled!\n");
        exit(-1);
    }

    /* turn off AA forever, it enables blending globally, so depth test goes out
     * of the window, blended objects should be rendered in back to front order,
     * also, following lines are ignored by OpenGL if multisampling is enabled */
#if 0
    /* turn on anti aliasing for points and lines */
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glEnable(GL_BLEND);
    glEnable(GL_POINT_SMOOTH);
    glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
    glEnable(GL_LINE_SMOOTH);
    glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
#endif

#if 0
    /* screws up snowman, this has been replaced by superior multisampling */
    glEnable(GL_POLYGON_SMOOTH);
    glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
#endif

    /* we will keep the blending function and hints though */
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
    glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);

    /* anisotropic filtering */
    if (gltQueryExtension("GL_EXT_texture_filter_anisotropic") == GL_TRUE)
    {
        GLint af_amount;
        glGetIntegerv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &af_amount);
        printf("platform: anisotropic filtering is supported, with max amount "
               "of: %d\n", af_amount);

        if (af_amount < af_w)
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, af_amount);
        else
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, af_w);

        /* see what value was set */
        glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, &af_amount);
        printf("platform: set anisotropic filtering to %d\n", af_amount);
    }
    else
        printf("platform: anisotropic filtering is not supported\n");

    /* texture compression */
    if (gltQueryExtension("GL_ARB_texture_compression") == GL_TRUE)
    {
        printf("platform: host gfx device supports texture compression\n");
        /* ToDo */
    }

    /* how OpenGL combines the colors from texels with the color of the underlying
     * geometry is controlled by the texture environment mode */
    glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

    /* once texture is loaded and enabled, it will applied to every primitive
     * that specifies coordinates, ORLY? */
    glEnable(GL_TEXTURE_2D);

    /* specular highlights for textured items */
    glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
    /* glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_COLOR_SINGLE); */
    /* only if lighting is disabled */
    /* glEnable(GL_COLOR_SUM); */

    /* draw fragments that pass this test, fragments with lower than 0.2f
     * alpha are discarded, this is just for the kicks */
    glAlphaFunc(GL_GREATER, 0.2f);
    glEnable(GL_ALPHA_TEST);

    /* gray background */
    glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
}

SDL_Surface *setup_sdl_video(int w, int h, int bpp, unsigned int flags)
{
    SDL_Surface *srfc;

    if (!bpp)
    {
        const SDL_VideoInfo* info = NULL;

        /* get some video information. */
        info = SDL_GetVideoInfo();
        if (!info)
        {
            fprintf( stderr, "SDL: video query failed: %s\n", SDL_GetError());
            exit(-1);
        }

        bpp = info->vfmt->BitsPerPixel;
        printf("SDL: bpp was not specified, chose %d bits\n", bpp);
    }

    if (!flags)
        flags = SDL_OPENGL | SDL_RESIZABLE;

    if ((srfc = SDL_SetVideoMode(w, h, bpp, flags)) == NULL)
    {
        fprintf(stderr, "SDL: unable to set video mode: %s\n", SDL_GetError());
        exit(-1);
    }

    return srfc;
}

SDL_Surface *setup_sdl(void)
{
    SDL_Surface *screen;
    const SDL_VideoInfo* info = NULL;

    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER) == -1)
    {
        fprintf(stderr, "SDL: unable to init, %s\n", SDL_GetError());
        exit(-1);
    }
    atexit(SDL_Quit);

    SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);

    SDL_WM_SetCaption(window_caption, NULL);
    SDL_WM_SetIcon(IMG_Load(window_icon_path), NULL);

    /* set video mode */
    screen = setup_sdl_video(xres_w, yres_w, bpp_w, sdl_video_flags);

    /* get some video information. */
    info = SDL_GetVideoInfo();
    if (!info)
    {
        fprintf( stderr, "SDL: video query failed: %s\n", SDL_GetError());
        exit(-1);
    }

    int bpp = info->vfmt->BitsPerPixel;
    printf("SDL: applied %d bits per pixel\n", bpp);

    /* query opengl attributes after SetVideoMode call */
    int fb_red_comp;
    int fb_green_comp;
    int fb_blue_comp;
    int fb_alpha_comp;
    int double_buff;
    int depth_sz;

    SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &fb_red_comp);
    SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &fb_green_comp);
    SDL_GL_GetAttribute(SDL_GL_BLUE_SIZE, &fb_blue_comp);
    SDL_GL_GetAttribute(SDL_GL_ALPHA_SIZE, &fb_alpha_comp);
    SDL_GL_GetAttribute(SDL_GL_DOUBLEBUFFER, &double_buff);
    SDL_GL_GetAttribute(SDL_GL_DEPTH_SIZE, &depth_sz);
    printf("SDL: framebuffer; size of components, red: %d bits, green: %d bits, "
           "blue: %d bits, alpha: %d bits\n"
           "SDL: double-buffering: %s, depth buffer size: %d bits\n",
           fb_red_comp, fb_green_comp, fb_blue_comp, fb_alpha_comp,
           double_buff ? "enabled" : "disabled", depth_sz);

    SDL_EnableUNICODE(1);

    /* SDL doesn't trigger off a ResizeEvent at startup, but as we need this
     * for OpenGL, we do this ourselves */
    SDL_Event resizeEvent;
    resizeEvent.type = SDL_VIDEORESIZE;
    resizeEvent.resize.w = xres_w;
    resizeEvent.resize.h = yres_w;
    SDL_PushEvent(&resizeEvent);

    /* save the surf pointer in platform */
    return screen;
}

void setup_glew(void)
{
    /* initalize glew */
    GLenum glewerr = glewInit();
    if (GLEW_OK != glewerr)
    {
        fprintf(stderr, "GLEW error: %s\n", glewGetErrorString(glewerr));
        exit(-1);
    }
    else
        fprintf(stdout, "GLEW: using version %s\n", glewGetString(GLEW_VERSION));

    /* display OpenGL version */
    GLint major;
    GLint minor;
    glGetIntegerv(GL_MAJOR_VERSION, &major);
    glGetIntegerv(GL_MINOR_VERSION, &minor);
    fprintf(stdout, "GLEW: initialized OpenGL %d.%d\n", major, minor);
}