awasm: new code file format; fixes for (g|t)vars
[awish.git] / src / video.c
blob84402a181ec44f6f54669d20a91e3e985dbca2d5
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"
21 #ifndef AWISH_NO_SOUND
22 # include "SDL_mixer.h"
23 #endif
26 ////////////////////////////////////////////////////////////////////////////////
27 SDL_Surface *screen = NULL;
28 SDL_Surface *backbuf = NULL;
29 Uint32 palette[256]; // converted
30 Uint8 font[256][8];
33 ////////////////////////////////////////////////////////////////////////////////
34 static void quitCleanupCB (void) {
35 //if (TTF_WasInit()) TTF_Quit();
36 Mix_CloseAudio();
37 SDL_Quit();
41 ////////////////////////////////////////////////////////////////////////////////
42 void sdlInit (void) {
43 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER
44 #ifndef AWISH_NO_SOUND
45 | SDL_INIT_AUDIO
46 #endif
47 ) < 0) {
48 fprintf(stderr, "FATAL: can't set videomode!\n");
49 exit(1);
51 SDL_WM_SetCaption("Awish", "Awish");
52 SDL_EnableKeyRepeat(200, 25);
53 //SDL_EnableUNICODE(1); // just4fun
54 SDL_EnableUNICODE(0); // just4fun
55 #ifndef AWISH_NO_SOUND
56 Mix_Init(0);
57 if (Mix_OpenAudio(22050, AUDIO_U8, 0, 8192)) {
58 SDL_Quit();
59 fprintf(stderr, "FATAL: unable to open audio!\n");
60 exit(1);
62 #endif
63 atexit(quitCleanupCB);
67 void initVideo (void) {
68 screen = SDL_SetVideoMode(640, 400, 32, SDL_SWSURFACE/*|SDL_DOUBLEBUF*/);
69 if (screen == NULL) {
70 fprintf(stderr, "FATAL: can't set videomode!\n");
71 exit(1);
74 backbuf = createSurface(640, 480);
75 if (backbuf == NULL) {
76 fprintf(stderr, "FATAL: can't create backbuffer!\n");
77 exit(1);
79 SDL_SetColorKey(backbuf, 0, 0);
80 //fprintf(stderr, "BPP: %u\n", screen->format->BytesPerPixel);
81 SDL_ShowCursor(0);
85 ////////////////////////////////////////////////////////////////////////////////
86 // create 24bpp surface with color key
87 SDL_Surface *createSurface (int w, int h) {
88 //Uint32 rmask, gmask, bmask/*, amask*/;
89 SDL_Surface *s;
91 if (w < 1 || h < 1) return NULL;
93 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
94 rmask = 0xff000000;
95 gmask = 0x00ff0000;
96 bmask = 0x0000ff00;
97 //amask = 0x000000ff;
98 #else
99 rmask = 0x000000ff;
100 gmask = 0x0000ff00;
101 bmask = 0x00ff0000;
102 //amask = 0xff000000;
103 #endif
104 s = SDL_CreateRGBSurface(SDL_SRCCOLORKEY, w, h, 32, rmask, gmask, bmask, amask);
106 s = SDL_CreateRGBSurface(SDL_SRCCOLORKEY, w, h, screen->format->BitsPerPixel, screen->format->Rmask, screen->format->Gmask, screen->format->Bmask, screen->format->Amask);
107 if (s != NULL) {
108 SDL_SetColorKey(s, SDL_SRCCOLORKEY, palette[255]);
110 return s;
114 void blitSurfaceEx (SDL_Surface *dests, int destx, int desty, SDL_Surface *srcs, int srcx, int srcy, int srcw, int srch) {
115 SDL_Rect src, dst;
117 if (srcw < 0) srcw = srcs->w;
118 if (srch < 0) srch = srcs->h;
119 src.x = srcx;
120 src.y = srcy;
121 src.w = srcw;
122 src.h = srch;
123 dst.x = destx;
124 dst.y = desty;
125 SDL_BlitSurface(srcs, &src, dests, &dst);
129 ////////////////////////////////////////////////////////////////////////////////
130 void lockSurface (SurfaceLock *lock, SDL_Surface *s) {
131 if (lock) {
132 lock->s = s;
133 lock->needUnlock = 0;
134 if (s != NULL && SDL_MUSTLOCK(s)) {
135 lock->needUnlock = (SDL_LockSurface(s) == 0);
141 void unlockSurface (SurfaceLock *lock) {
142 if (lock && lock->s && lock->needUnlock) {
143 SDL_UnlockSurface(lock->s);
144 lock->needUnlock = 0;
149 ////////////////////////////////////////////////////////////////////////////////
150 // surface must be locked!
151 void drawChar (SDL_Surface *s, char ch, int x, int y, Uint8 clr) {
152 unsigned char c = (unsigned char)(ch);
154 for (int dy = 0; dy < 8; ++dy) {
155 Uint8 b = font[c][dy];
157 for (int dx = 0; dx < 8; ++dx) {
158 if (b&0x80) putPixel2x(s, x+dx, y+dy, clr);
159 b = (b&0x7f)<<1;
165 void drawString (SDL_Surface *s, const char *str, int x, int y, Uint8 clr) {
166 for (; *str; ++str) {
167 drawChar(s, *str, x, y, clr);
168 x += 8;