simplified video module
[awish.git] / src / video.h
blobc0137150ac479959cfc830c6fdc1a23a30bc251b
1 /*
2 * This program is free software: you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation, either version 3 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 #ifndef _VIDEO_H_
16 #define _VIDEO_H_
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
22 #include "SDL.h"
25 ////////////////////////////////////////////////////////////////////////////////
26 extern SDL_Surface *screen;
27 extern SDL_Surface *backbuf; // 'background' buffer
28 extern Uint32 palette[256]; // converted
29 extern Uint8 font[256][8];
32 ////////////////////////////////////////////////////////////////////////////////
33 extern void sdlInit (void);
34 extern void initVideo (void);
37 ////////////////////////////////////////////////////////////////////////////////
38 // create 24bpp surface with color key
39 extern SDL_Surface *createSurface (int w, int h);
42 ////////////////////////////////////////////////////////////////////////////////
43 // NO LOCKING!
44 static inline void putPixelC32 (SDL_Surface *s, int x, int y, Uint32 col) {
45 if (s && x >= s->clip_rect.x && y >= s->clip_rect.y && x < s->clip_rect.x+s->clip_rect.w && y < s->clip_rect.y+s->clip_rect.h) {
46 Uint8 bpp = s->format->BytesPerPixel;
47 Uint8 *bits = ((Uint8 *)s->pixels)+(y*s->pitch)+(x*bpp);
49 switch (bpp) {
51 case 1:
52 *((Uint8 *)(bits)) = (Uint8)col;
53 break;
54 case 2:
55 *((Uint16 *)(bits)) = (Uint16)col;
56 break;
57 case 3:
58 *((bits)+s->format->Rshift/8) = (col>>s->format->Rshift)&0xFF;
59 *((bits)+s->format->Gshift/8) = (col>>s->format->Gshift)&0xFF;
60 *((bits)+s->format->Bshift/8) = (col>>s->format->Bshift)&0xFF;
61 break;
63 case 4:
64 *((Uint32 *)(bits)) = col;
65 break;
71 static inline void putPixel2x (SDL_Surface *s, int x, int y, Uint8 col) {
72 x *= 2;
73 y *= 2;
74 if (s && x >= s->clip_rect.x && y >= s->clip_rect.y && x < s->clip_rect.x+s->clip_rect.w && y < s->clip_rect.y+s->clip_rect.h) {
75 Uint8 bpp = s->format->BytesPerPixel;
76 Uint32 *bits = (Uint32 *)(((Uint8 *)s->pixels)+(y*s->pitch)+(x*bpp));
77 Uint32 pix = palette[col];
79 *bits = pix;
80 if (x+1 < s->clip_rect.x+s->clip_rect.w) bits[1] = pix;
81 if (y+1 < s->clip_rect.y+s->clip_rect.h) {
82 bits = (Uint32 *)((Uint8 *)bits+s->pitch);
83 *bits = pix;
84 if (x+1 < s->clip_rect.x+s->clip_rect.w) bits[1] = pix;
90 static inline void hlineC32 (SDL_Surface *s, int x, int y, int len, Uint32 col) {
91 if (s && y >= s->clip_rect.y && x < s->clip_rect.x+s->clip_rect.w && y < s->clip_rect.y+s->clip_rect.h) {
92 if (x < s->clip_rect.x) {
93 len -= s->clip_rect.x-x;
94 x = s->clip_rect.x;
96 if (x+len > s->clip_rect.x+s->clip_rect.w) len = s->clip_rect.x+s->clip_rect.w-x;
97 if (len > 0) {
98 Uint8 bpp = s->format->BytesPerPixel;
99 Uint32 *bits = (Uint32 *)(((Uint8 *)s->pixels)+(y*s->pitch)+(x*bpp));
101 for (; len > 0; --len) *bits++ = col;
107 static inline void hline2x (SDL_Surface *s, int x, int y, int len, Uint8 col) {
108 x *= 2;
109 y *= 2;
110 len *= 2;
111 hlineC32(s, x, y, len, palette[col]);
112 hlineC32(s, x, y+1, len, palette[col]);
116 static inline void putPixelA32 (SDL_Surface *s, int x, int y, Uint32 col, Uint8 alpha) {
117 if (s && x >= s->clip_rect.x && y >= s->clip_rect.y && x < s->clip_rect.x+s->clip_rect.w && y < s->clip_rect.y+s->clip_rect.h) {
118 Uint8 bpp = s->format->BytesPerPixel;
119 Uint8 *bits = ((Uint8 *)s->pixels)+(y*s->pitch)+(x*bpp);
120 Uint32 pixel = 0;
121 unsigned char r, pr, g, pg, b, pb;
123 SDL_GetRGB(col, s->format, &pr, &pg, &pb);
124 pixel = *((Uint32 *)(bits));
125 // get RGB values from pixel
126 r = (((pixel&s->format->Rmask)>>s->format->Rshift)<<s->format->Rloss);
127 g = (((pixel&s->format->Gmask)>>s->format->Gshift)<<s->format->Gloss);
128 b = (((pixel&s->format->Bmask)>>s->format->Bshift)<<s->format->Bloss);
129 // blend with alpha
130 r = (((pr-r)*alpha)>>8)+r;
131 g = (((pg-g)*alpha)>>8)+g;
132 b = (((pb-b)*alpha)>>8)+b;
133 // set
134 pixel = SDL_MapRGB(s->format, r, g, b);
135 *((Uint32 *)(bits)) = pixel;
140 static inline void putPixelA2x (SDL_Surface *s, int x, int y, Uint8 clr, Uint8 alpha) {
141 if (clr != 0) {
142 if (clr != 255) {
143 x *= 2;
144 y *= 2;
145 putPixelA32(s, x+0, y+0, palette[clr], alpha);
146 putPixelA32(s, x+1, y+0, palette[clr], alpha);
147 ++y;
148 putPixelA32(s, x+0, y+1, palette[clr], alpha);
149 putPixelA32(s, x+1, y+1, palette[clr], alpha);
150 } else {
151 putPixel2x(s, x, y, clr);
157 // NO LOCKING!
158 static inline void putPixel2xC32 (SDL_Surface *s, int x, int y, Uint32 clr) {
159 x *= 2;
160 y *= 2;
161 putPixelC32(s, x+0, y+0, clr);
162 putPixelC32(s, x+1, y+0, clr);
163 ++y;
164 putPixelC32(s, x+0, y, clr);
165 putPixelC32(s, x+1, y, clr);
169 static inline void putPixel (SDL_Surface *s, int x, int y, Uint8 clr) {
170 putPixelC32(s, x, y, palette[clr]);
174 ////////////////////////////////////////////////////////////////////////////////
175 extern void blitSurfaceEx (SDL_Surface *dests, int destx, int desty, SDL_Surface *srcs, int srcx, int srcy, int srcw, int srch);
178 static inline void blitSurface (SDL_Surface *dests, int destx, int desty, SDL_Surface *srcs) {
179 blitSurfaceEx(dests, destx, desty, srcs, 0, 0, -1, -1);
183 static inline void blitSurface2x (SDL_Surface *dests, int destx, int desty, SDL_Surface *srcs) {
184 blitSurfaceEx(dests, destx*2, desty*2, srcs, 0, 0, -1, -1);
188 ////////////////////////////////////////////////////////////////////////////////
189 typedef struct {
190 SDL_Surface *s;
191 int needUnlock;
192 } SurfaceLock;
195 extern void lockSurface (SurfaceLock *lock, SDL_Surface *s);
196 extern void unlockSurface (SurfaceLock *lock);
199 ////////////////////////////////////////////////////////////////////////////////
200 // surface must be locked!
201 extern void drawChar (SDL_Surface *s, char ch, int x, int y, Uint8 clr);
202 extern void drawString (SDL_Surface *s, const char *str, int x, int y, Uint8 clr);
205 #endif