annotate pwplib/sdl.c @ 40:32559c69f6e5

Cosmetics.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 24 May 2010 02:55:17 +0300
parents d83239a98d1e
children ea44e1d9eb7c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1 /*
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2 * pwplib SDL (Simple Directmedia Layer) video "driver"
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3 * (C) Copyright 2010 ccr/TNSP^PWP <ccr@tnsp.org>
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
4 *
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
5 * Rasterization based on the prototype X11 driver by viznut/PWP
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
6 *
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
7 * This file and other changes are distributed under same
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
8 * license as pwplib itself.
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
9 */
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
10 #include "config.h"
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
11
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
12 #ifdef DRIVE_SDL
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
13
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
14 #include "pwplib.h"
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
15 #include <SDL.h>
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
16
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
17 #define PWP_WINNAME "pwplib"
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
18 #define PWP_NCOLORS 256
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
19
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
20 struct {
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
21 int width, height, vflags;
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
22 SDL_Surface *screen;
40
32559c69f6e5 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
23 SDL_Color pal[PWP_NCOLORS];
32559c69f6e5 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
24 Uint32 lookup[PWP_NCOLORS];
0
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
25 } pwp_SDL;
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
26
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
27
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
28 static void pwp_SDL_dump_rast_4x4()
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
29 {
12
18e14722f58a Clean up the framebuffer rasterization code.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
30 Uint8 *dst = pwp_SDL.screen->pixels;
18e14722f58a Clean up the framebuffer rasterization code.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
31 Uint8 *src = pwplib.videobuf.d;
18e14722f58a Clean up the framebuffer rasterization code.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
32 int y;
0
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
33
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
34 if (SDL_MUSTLOCK(pwp_SDL.screen) != 0)
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
35 {
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
36 if (SDL_LockSurface(pwp_SDL.screen) != 0)
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
37 {
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
38 SDL_Quit();
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
39 pwpwrite("SDL: Could not lock video surface!\n");
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
40 exit(1);
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
41 }
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
42 }
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
43
12
18e14722f58a Clean up the framebuffer rasterization code.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
44 for (y = pwplib.videobuf.height; y; y--)
0
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
45 {
13
e5049100f6ac Optimize rasterization a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
46 int i;
e5049100f6ac Optimize rasterization a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
47 Uint8 *dsrc = dst;
e5049100f6ac Optimize rasterization a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
48 Uint32 *d = (Uint32 *)dst;
e5049100f6ac Optimize rasterization a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
49 Uint8 *s = src;
e5049100f6ac Optimize rasterization a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
50
e5049100f6ac Optimize rasterization a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
51 for (i = pwplib.videobuf.width; i; i--)
e5049100f6ac Optimize rasterization a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
52 *d++ = pwp_SDL.lookup[*s++];
e5049100f6ac Optimize rasterization a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
53
e5049100f6ac Optimize rasterization a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
54 dst += pwp_SDL.screen->pitch;
e5049100f6ac Optimize rasterization a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
55
e5049100f6ac Optimize rasterization a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
56 /* Optimization: memcpy the rasterized line 3 times below it */
e5049100f6ac Optimize rasterization a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
57 for (i = 3; i; i--)
12
18e14722f58a Clean up the framebuffer rasterization code.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
58 {
13
e5049100f6ac Optimize rasterization a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
59 memcpy(dst, dsrc, pwp_SDL.screen->pitch);
12
18e14722f58a Clean up the framebuffer rasterization code.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
60 dst += pwp_SDL.screen->pitch;
18e14722f58a Clean up the framebuffer rasterization code.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
61 }
13
e5049100f6ac Optimize rasterization a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
62
12
18e14722f58a Clean up the framebuffer rasterization code.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
63 src += pwplib.videobuf.width;
0
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
64 }
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
65
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
66 if (SDL_MUSTLOCK(pwp_SDL.screen) != 0)
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
67 SDL_UnlockSurface(pwp_SDL.screen);
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
68
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
69 SDL_Flip(pwp_SDL.screen);
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
70 }
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
71
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
72 static void rast2rgb(int n, int muller, int *r, int *g, int *b)
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
73 {
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
74 int fg = n & 15, bg = (n >> 4) & 15;
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
75 static const char rgb[16 * 3] = { 0, 0, 0,
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
76 0, 0, 9,
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
77 0, 9, 0,
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
78 0, 9, 9,
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
79 9, 0, 0,
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
80 9, 0, 9,
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
81 9, 9, 0,
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
82 9, 9, 9,
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
83 6, 6, 6,
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
84 0, 0, 15,
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
85 0, 15, 0,
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
86 0, 15, 15,
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
87 15, 0, 0,
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
88 15, 0, 15,
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
89 15, 15, 0,
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
90 15, 15, 15
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
91 };
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
92
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
93 *r = (muller * (rgb[bg * 3 + 0] * 2 + rgb[fg * 3 + 0])) / 45;
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
94 *g = (muller * (rgb[bg * 3 + 1] * 2 + rgb[fg * 3 + 1])) / 45;
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
95 *b = (muller * (rgb[bg * 3 + 2] * 2 + rgb[fg * 3 + 2])) / 45;
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
96 }
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
97
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
98 int pwp_SDL_init(void)
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
99 {
17
c60e531d19cd Some misc. cleanups and minor warning removals.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
100 int i;
0
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
101
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
102 /* Assume these settings for now */
21
f0869ef0f2d9 Add commandline settings for fullscreen and changing horiz/vert resolution of SDL output. Also hide the mouse cursor.
Matti Hamalainen <ccr@tnsp.org>
parents: 17
diff changeset
103 pwp_SDL.vflags = pwplib.setup[SETUP_FULLSCREEN] ? SDL_FULLSCREEN : 0;
f0869ef0f2d9 Add commandline settings for fullscreen and changing horiz/vert resolution of SDL output. Also hide the mouse cursor.
Matti Hamalainen <ccr@tnsp.org>
parents: 17
diff changeset
104 pwp_SDL.width = pwplib.setup[SETUP_XRES];
f0869ef0f2d9 Add commandline settings for fullscreen and changing horiz/vert resolution of SDL output. Also hide the mouse cursor.
Matti Hamalainen <ccr@tnsp.org>
parents: 17
diff changeset
105 pwp_SDL.height = pwplib.setup[SETUP_YRES];
f0869ef0f2d9 Add commandline settings for fullscreen and changing horiz/vert resolution of SDL output. Also hide the mouse cursor.
Matti Hamalainen <ccr@tnsp.org>
parents: 17
diff changeset
106
f0869ef0f2d9 Add commandline settings for fullscreen and changing horiz/vert resolution of SDL output. Also hide the mouse cursor.
Matti Hamalainen <ccr@tnsp.org>
parents: 17
diff changeset
107 if (pwp_SDL.width < 8 || pwp_SDL.height < 16) {
f0869ef0f2d9 Add commandline settings for fullscreen and changing horiz/vert resolution of SDL output. Also hide the mouse cursor.
Matti Hamalainen <ccr@tnsp.org>
parents: 17
diff changeset
108 pwpwrite("* invalid xres or yres (%d x %d)\n", pwp_SDL.width, pwp_SDL.height);
f0869ef0f2d9 Add commandline settings for fullscreen and changing horiz/vert resolution of SDL output. Also hide the mouse cursor.
Matti Hamalainen <ccr@tnsp.org>
parents: 17
diff changeset
109 return 0;
f0869ef0f2d9 Add commandline settings for fullscreen and changing horiz/vert resolution of SDL output. Also hide the mouse cursor.
Matti Hamalainen <ccr@tnsp.org>
parents: 17
diff changeset
110 }
0
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
111
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
112 /* Set pwplib internal parameters */
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
113 pwplib.videobuf.width = pwp_SDL.width / 4;
29
d83239a98d1e Take SDL into account when halving height of rendering buffer, and remove the corresponding "hack" from sdl.c. Also misc. cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
114 pwplib.videobuf.height = pwp_SDL.height / 4;
0
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
115 pwplib.dump_rast = pwp_SDL_dump_rast_4x4;
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
116
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
117 /* Initialize a indexed / paletted video surface */
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
118 pwp_SDL.screen = SDL_SetVideoMode(pwp_SDL.width, pwp_SDL.height, 8, pwp_SDL.vflags);
21
f0869ef0f2d9 Add commandline settings for fullscreen and changing horiz/vert resolution of SDL output. Also hide the mouse cursor.
Matti Hamalainen <ccr@tnsp.org>
parents: 17
diff changeset
119 if (pwp_SDL.screen == NULL) {
0
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
120 pwpwrite("* SDL could not initialize video surface/mode.\n");
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
121 return 0;
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
122 }
15
fa87f8897f21 Implement hacky and crummy SDL event handling in timer value fetching function; moved SDL general initialization into pwplib-unix.c.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
123
21
f0869ef0f2d9 Add commandline settings for fullscreen and changing horiz/vert resolution of SDL output. Also hide the mouse cursor.
Matti Hamalainen <ccr@tnsp.org>
parents: 17
diff changeset
124 /* Set window caption, if any, hide mouse cursor */
0
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
125 SDL_WM_SetCaption(PWP_WINNAME, PWP_WINNAME);
21
f0869ef0f2d9 Add commandline settings for fullscreen and changing horiz/vert resolution of SDL output. Also hide the mouse cursor.
Matti Hamalainen <ccr@tnsp.org>
parents: 17
diff changeset
126 SDL_ShowCursor(0);
0
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
127
12
18e14722f58a Clean up the framebuffer rasterization code.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
128 /* Generate palette and rasterization lookup table */
0
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
129 for (i = 0; i < PWP_NCOLORS; i++)
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
130 {
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
131 int r, g, b;
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
132 rast2rgb(i, 255, &r, &g, &b);
40
32559c69f6e5 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
133 pwp_SDL.pal[i].r = r;
32559c69f6e5 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
134 pwp_SDL.pal[i].g = g;
32559c69f6e5 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
135 pwp_SDL.pal[i].b = b;
0
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
136 pwp_SDL.lookup[i] = i | (i << 8) | (i << 16) | (i << 24);
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
137 }
40
32559c69f6e5 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
138 SDL_SetColors(pwp_SDL.screen, pwp_SDL.pal, 0, PWP_NCOLORS);
0
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
139
29
d83239a98d1e Take SDL into account when halving height of rendering buffer, and remove the corresponding "hack" from sdl.c. Also misc. cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
140 pwpwrite("* SDL video (%d x %d, pitch=%d)\n", pwp_SDL.screen->w, pwp_SDL.screen->h, pwp_SDL.screen->pitch);
0
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
141
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
142 return 1;
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
143 }
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
144
acb5694e93d9 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
145 #endif