awasm: new code file format; fixes for (g|t)vars
[awish.git] / src / video.h
blob0f54f0372050d3bd583ef09ff8784808ba2058d1
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 *(Uint32 *)(((Uint8 *)s->pixels)+(y*s->pitch)+(x*s->format->BytesPerPixel)) = col;
51 static inline void putPixel2xC32 (SDL_Surface *s, int x, int y, Uint32 col) {
52 x *= 2; y *= 2;
53 if (s && x >= s->clip_rect.x && y >= s->clip_rect.y && x+1 < s->clip_rect.x+s->clip_rect.w && y+1 < s->clip_rect.y+s->clip_rect.h) {
54 Uint32 *bits = (Uint32 *)(((Uint8 *)s->pixels)+(y*s->pitch)+(x*s->format->BytesPerPixel));
56 bits[0] = col;
57 bits[1] = col;
58 bits = (Uint32 *)((Uint8 *)bits+s->pitch);
59 bits[0] = col;
60 bits[1] = col;
65 #define _INTERNAL_VIDEO_ABLEND_MACRO_(n) \
66 SDL_GetRGB(bits[n], s->format, &r, &g, &b); \
67 r = (((pr-r)*alpha)>>8)+r; \
68 g = (((pg-g)*alpha)>>8)+g; \
69 b = (((pb-b)*alpha)>>8)+b; \
70 bits[n] = SDL_MapRGB(s->format, r, g, b); \
73 static inline void putPixelA32 (SDL_Surface *s, int x, int y, Uint32 col, Uint8 alpha) {
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 unsigned char r, pr, g, pg, b, pb;
79 SDL_GetRGB(col, s->format, &pr, &pg, &pb);
80 _INTERNAL_VIDEO_ABLEND_MACRO_(0)
85 static inline void putPixel2xA32 (SDL_Surface *s, int x, int y, Uint32 col, Uint8 alpha) {
86 x *= 2;
87 y *= 2;
88 if (s && x >= s->clip_rect.x && y >= s->clip_rect.y && x+1 < s->clip_rect.x+s->clip_rect.w && y+1 < s->clip_rect.y+s->clip_rect.h) {
89 Uint32 *bits = (Uint32 *)(((Uint8 *)s->pixels)+(y*s->pitch)+(x*s->format->BytesPerPixel));
90 unsigned char r, pr, g, pg, b, pb;
92 SDL_GetRGB(col, s->format, &pr, &pg, &pb);
93 _INTERNAL_VIDEO_ABLEND_MACRO_(0)
94 _INTERNAL_VIDEO_ABLEND_MACRO_(1)
95 bits = (Uint32 *)((Uint8 *)bits+s->pitch);
96 _INTERNAL_VIDEO_ABLEND_MACRO_(0)
97 _INTERNAL_VIDEO_ABLEND_MACRO_(1)
102 static inline void putPixel2x (SDL_Surface *s, int x, int y, Uint8 col) {
103 putPixel2xC32(s, x, y, palette[col]);
107 static inline void putPixelA2x (SDL_Surface *s, int x, int y, Uint8 clr, Uint8 alpha) {
108 if (alpha != 0) {
109 if (alpha != 255) {
110 putPixel2xA32(s, x, y, palette[clr], alpha);
111 } else {
112 putPixel2x(s, x, y, palette[clr]);
118 static inline void putPixel (SDL_Surface *s, int x, int y, Uint8 clr) {
119 putPixelC32(s, x, y, palette[clr]);
124 static inline void hlineC32 (SDL_Surface *s, int x, int y, int len, Uint32 col) {
125 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) {
126 if (x < s->clip_rect.x) {
127 len -= s->clip_rect.x-x;
128 x = s->clip_rect.x;
130 if (x+len > s->clip_rect.x+s->clip_rect.w) len = s->clip_rect.x+s->clip_rect.w-x;
131 if (len > 0) {
132 Uint8 bpp = s->format->BytesPerPixel;
133 Uint32 *bits = (Uint32 *)(((Uint8 *)s->pixels)+(y*s->pitch)+(x*bpp));
135 for (; len > 0; --len) *bits++ = col;
141 static inline void hline2x (SDL_Surface *s, int x, int y, int len, Uint8 col) {
142 x *= 2;
143 y *= 2;
144 len *= 2;
145 hlineC32(s, x, y, len, palette[col]);
146 hlineC32(s, x, y+1, len, palette[col]);
151 ////////////////////////////////////////////////////////////////////////////////
152 extern void blitSurfaceEx (SDL_Surface *dests, int destx, int desty, SDL_Surface *srcs, int srcx, int srcy, int srcw, int srch);
155 static inline void blitSurface (SDL_Surface *dests, int destx, int desty, SDL_Surface *srcs) {
156 blitSurfaceEx(dests, destx, desty, srcs, 0, 0, -1, -1);
160 static inline void blitSurface2x (SDL_Surface *dests, int destx, int desty, SDL_Surface *srcs) {
161 blitSurfaceEx(dests, destx*2, desty*2, srcs, 0, 0, -1, -1);
165 ////////////////////////////////////////////////////////////////////////////////
166 typedef struct {
167 SDL_Surface *s;
168 int needUnlock;
169 } SurfaceLock;
172 extern void lockSurface (SurfaceLock *lock, SDL_Surface *s);
173 extern void unlockSurface (SurfaceLock *lock);
176 ////////////////////////////////////////////////////////////////////////////////
177 // surface must be locked!
178 extern void drawChar (SDL_Surface *s, char ch, int x, int y, Uint8 clr);
179 extern void drawString (SDL_Surface *s, const char *str, int x, int y, Uint8 clr);
182 #endif