added backbuffer for level statics; -10% in CPU meter
[awish.git] / src / video.c
blob8d432679692796542abc47ef0819a32c04811847
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 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
19 #include "video.h"
22 ////////////////////////////////////////////////////////////////////////////////
23 SDL_Surface *screen = NULL;
24 SDL_Surface *backbuf = NULL;
25 Uint32 palette[256]; // converted
26 Uint8 font[256][8];
29 ////////////////////////////////////////////////////////////////////////////////
30 static void quitCleanupCB (void) {
31 //if (TTF_WasInit()) TTF_Quit();
32 SDL_Quit();
36 ////////////////////////////////////////////////////////////////////////////////
37 void sdlInit (void) {
38 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) < 0) {
39 fprintf(stderr, "FATAL: can't set videomode!\n");
40 exit(1);
42 atexit(quitCleanupCB);
44 SDL_WM_SetCaption("Awish", "Awish");
45 SDL_EnableKeyRepeat(200, 25);
46 //SDL_EnableUNICODE(1); // just4fun
47 SDL_EnableUNICODE(0); // just4fun
51 void initVideo (void) {
52 screen = SDL_SetVideoMode(640, 400, 32, SDL_SWSURFACE/*|SDL_DOUBLEBUF*/);
53 if (screen == NULL) {
54 fprintf(stderr, "FATAL: can't set videomode!\n");
55 exit(1);
58 backbuf = createSurface(640, 480);
59 if (backbuf == NULL) {
60 fprintf(stderr, "FATAL: can't create backbuffer!\n");
61 exit(1);
63 SDL_SetColorKey(backbuf, 0, 0);
64 //fprintf(stderr, "BPP: %u\n", screen->format->BytesPerPixel);
68 ////////////////////////////////////////////////////////////////////////////////
69 // create 24bpp surface with color key
70 SDL_Surface *createSurface (int w, int h) {
71 //Uint32 rmask, gmask, bmask/*, amask*/;
72 SDL_Surface *s;
74 if (w < 1 || h < 1) return NULL;
76 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
77 rmask = 0xff000000;
78 gmask = 0x00ff0000;
79 bmask = 0x0000ff00;
80 //amask = 0x000000ff;
81 #else
82 rmask = 0x000000ff;
83 gmask = 0x0000ff00;
84 bmask = 0x00ff0000;
85 //amask = 0xff000000;
86 #endif
87 s = SDL_CreateRGBSurface(SDL_SRCCOLORKEY, w, h, 32, rmask, gmask, bmask, amask);
89 s = SDL_CreateRGBSurface(SDL_SRCCOLORKEY, w, h, screen->format->BitsPerPixel, screen->format->Rmask, screen->format->Gmask, screen->format->Bmask, screen->format->Amask);
90 if (s != NULL) {
91 SDL_SetColorKey(s, SDL_SRCCOLORKEY, palette[255]);
93 return s;
97 void blitSurfaceEx (SDL_Surface *dests, int destx, int desty, SDL_Surface *srcs, int srcx, int srcy, int srcw, int srch) {
98 SDL_Rect src, dst;
100 if (srcw < 0) srcw = srcs->w;
101 if (srch < 0) srch = srcs->h;
102 src.x = srcx;
103 src.y = srcy;
104 src.w = srcw;
105 src.h = srch;
106 dst.x = destx;
107 dst.y = desty;
108 SDL_BlitSurface(srcs, &src, dests, &dst);
112 ////////////////////////////////////////////////////////////////////////////////
113 void lockSurface (SurfaceLock *lock, SDL_Surface *s) {
114 if (lock) {
115 lock->s = s;
116 lock->needUnlock = 0;
117 if (s != NULL && SDL_MUSTLOCK(s)) {
118 lock->needUnlock = (SDL_LockSurface(s) == 0);
124 void unlockSurface (SurfaceLock *lock) {
125 if (lock && lock->s && lock->needUnlock) {
126 SDL_UnlockSurface(lock->s);
127 lock->needUnlock = 0;
132 ////////////////////////////////////////////////////////////////////////////////
133 // surface must be locked!
134 void drawChar (SDL_Surface *s, char ch, int x, int y, Uint8 clr) {
135 unsigned char c = (unsigned char)(ch);
137 for (int dy = 0; dy < 8; ++dy) {
138 Uint8 b = font[c][dy];
140 for (int dx = 0; dx < 8; ++dx) {
141 if (b&0x80) putPixel2x(s, x+dx, y+dy, clr);
142 b = (b&0x7f)<<1;
148 void drawString (SDL_Surface *s, const char *str, int x, int y, Uint8 clr) {
149 for (; *str; ++str) {
150 drawChar(s, *str, x, y, clr);
151 x += 8;