Mercurial > hg > forks > pwpunix
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 |
rev | line source |
---|---|
0 | 1 /* |
2 * pwplib SDL (Simple Directmedia Layer) video "driver" | |
3 * (C) Copyright 2010 ccr/TNSP^PWP <ccr@tnsp.org> | |
4 * | |
5 * Rasterization based on the prototype X11 driver by viznut/PWP | |
6 * | |
7 * This file and other changes are distributed under same | |
8 * license as pwplib itself. | |
9 */ | |
10 #include "config.h" | |
11 | |
12 #ifdef DRIVE_SDL | |
13 | |
14 #include "pwplib.h" | |
15 #include <SDL.h> | |
16 | |
17 #define PWP_WINNAME "pwplib" | |
18 #define PWP_NCOLORS 256 | |
19 | |
20 struct { | |
21 int width, height, vflags; | |
22 SDL_Surface *screen; | |
40 | 23 SDL_Color pal[PWP_NCOLORS]; |
24 Uint32 lookup[PWP_NCOLORS]; | |
0 | 25 } pwp_SDL; |
26 | |
27 | |
28 static void pwp_SDL_dump_rast_4x4() | |
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 | 33 |
34 if (SDL_MUSTLOCK(pwp_SDL.screen) != 0) | |
35 { | |
36 if (SDL_LockSurface(pwp_SDL.screen) != 0) | |
37 { | |
38 SDL_Quit(); | |
39 pwpwrite("SDL: Could not lock video surface!\n"); | |
40 exit(1); | |
41 } | |
42 } | |
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 | 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 | 64 } |
65 | |
66 if (SDL_MUSTLOCK(pwp_SDL.screen) != 0) | |
67 SDL_UnlockSurface(pwp_SDL.screen); | |
68 | |
69 SDL_Flip(pwp_SDL.screen); | |
70 } | |
71 | |
72 static void rast2rgb(int n, int muller, int *r, int *g, int *b) | |
73 { | |
74 int fg = n & 15, bg = (n >> 4) & 15; | |
75 static const char rgb[16 * 3] = { 0, 0, 0, | |
76 0, 0, 9, | |
77 0, 9, 0, | |
78 0, 9, 9, | |
79 9, 0, 0, | |
80 9, 0, 9, | |
81 9, 9, 0, | |
82 9, 9, 9, | |
83 6, 6, 6, | |
84 0, 0, 15, | |
85 0, 15, 0, | |
86 0, 15, 15, | |
87 15, 0, 0, | |
88 15, 0, 15, | |
89 15, 15, 0, | |
90 15, 15, 15 | |
91 }; | |
92 | |
93 *r = (muller * (rgb[bg * 3 + 0] * 2 + rgb[fg * 3 + 0])) / 45; | |
94 *g = (muller * (rgb[bg * 3 + 1] * 2 + rgb[fg * 3 + 1])) / 45; | |
95 *b = (muller * (rgb[bg * 3 + 2] * 2 + rgb[fg * 3 + 2])) / 45; | |
96 } | |
97 | |
98 int pwp_SDL_init(void) | |
99 { | |
17
c60e531d19cd
Some misc. cleanups and minor warning removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
15
diff
changeset
|
100 int i; |
0 | 101 |
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 | 111 |
112 /* Set pwplib internal parameters */ | |
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 | 115 pwplib.dump_rast = pwp_SDL_dump_rast_4x4; |
116 | |
117 /* Initialize a indexed / paletted video surface */ | |
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 | 120 pwpwrite("* SDL could not initialize video surface/mode.\n"); |
121 return 0; | |
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 | 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 | 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 | 129 for (i = 0; i < PWP_NCOLORS; i++) |
130 { | |
131 int r, g, b; | |
132 rast2rgb(i, 255, &r, &g, &b); | |
40 | 133 pwp_SDL.pal[i].r = r; |
134 pwp_SDL.pal[i].g = g; | |
135 pwp_SDL.pal[i].b = b; | |
0 | 136 pwp_SDL.lookup[i] = i | (i << 8) | (i << 16) | (i << 24); |
137 } | |
40 | 138 SDL_SetColors(pwp_SDL.screen, pwp_SDL.pal, 0, PWP_NCOLORS); |
0 | 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 | 141 |
142 return 1; | |
143 } | |
144 | |
145 #endif |