File: fire.cpp

package info (click to toggle)
libsdl-sge 030809dfsg-11
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,224 kB
  • sloc: cpp: 8,973; makefile: 119; ansic: 44; sh: 19
file content (150 lines) | stat: -rw-r--r-- 3,471 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "SDL.h"
#include "sge.h"

#define NCOLORS 1200
#define XRES 300
#define YRES 100

void setup_palette(SDL_Surface *Surface, Uint32 *ctab, int start,int stop);
void fade(SDL_Surface *surface, int speed);

/* Globals */
int cb[YRES][XRES];   //The color buffer
Uint32 ctab[NCOLORS]; //The color table

int main(int argc, char** argv)
{	
	/* Init SDL */
	if ( SDL_Init(SDL_INIT_TIMER|SDL_INIT_VIDEO) < 0 ) {
		fprintf(stderr, "Couldn't load SDL: %s\n", SDL_GetError());
		exit(1);
	}

	/* Clean up on exit */
	atexit(SDL_Quit);

	/* Set window title */
	SDL_WM_SetCaption("Fire!", "fire");

	/* Initialize the display */
	SDL_Surface *screen;
	screen = SDL_SetVideoMode(XRES, YRES, 16, SDL_SWSURFACE);
	if ( screen == NULL ) {
		fprintf(stderr, "Couldn't set video mode: %s\n", SDL_GetError());
		exit(1);
	}	
	
	/* Clear the color buffer */
	for(int y=0; y<YRES; y++){
		memset(cb[y],0,sizeof(int)*XRES);
	}	
	
	/* Make the nice fire pallete */
	setup_palette(screen,ctab,0,NCOLORS-1);
	
	sge_Randomize();
	
	sge_Update_OFF();
	
	/* Main loop */
	SDL_Event event;
	int loops=0,x,lc=0,speed=1;
	Uint32 start=SDL_GetTicks();
	
	do{
		/* Insert random line */
		for(x=0; x<XRES; x++){
			if(sge_Random(0,10)==0)
				cb[YRES-2][x]=ctab[NCOLORS-1]; //Er, should be =NCOLORS-1 but this looks better
		}	
		
		/* Raise the flame? */
		if(sge_Random(0,700)==0){
			speed=0;
			lc=loops;
		}
		else if(speed==0){
			if(loops-lc>60) // Lower the flame
				speed=1;	
		}
			
		fade(screen,speed); // Do the fading
   	
		/* Update screen */
		SDL_UpdateRect(screen,0,0,0,0);
	
		loops++;
		
		/* Check events */
		if(SDL_PollEvent(&event)==1){
			if(event.type==SDL_KEYDOWN && event.key.keysym.sym==SDLK_ESCAPE){break;}
			if(event.type==SDL_QUIT){break;}
		}
	}while(true);
	
	//Show FPS
	printf("%.2f FPS.\n",((double)loops*1000)/(SDL_GetTicks()-start));
	
	return 0;
}


//====================================================================
// This function creates a "flame" palette (using sge_Fader)
//====================================================================
void setup_palette(SDL_Surface *Surface, Uint32 *ctab, int start, int stop)
{
	int slice=(int)((stop-start)/5);

	sge_Fader(Surface, 0,0,0, 0,0,40, ctab, start,slice);       /* black - dark blue */
	sge_Fader(Surface, 0,0,40, 255,0,0, ctab, slice+1,2*slice); /* - red */
	sge_Fader(Surface, 255,0,0, 255,255,0, ctab, 2*slice+1, 3*slice); /* -yellow */
	sge_Fader(Surface, 255,255,0, 255,255,255, ctab, 3*slice+1,stop); /* -white */

}


//====================================================================
// Do the fading magic
//====================================================================
void fade(SDL_Surface *surface, int speed)
{
	int color,x,y;
	Uint16 block[XRES];

	if(SDL_MUSTLOCK(surface))
		SDL_LockSurface(surface);
		
	for(y=0; y<YRES; y++){
		for(x=0; x<XRES; x++){					
			color=cb[y][x];
			if(x>0){
				color+=cb[y][x-1];  //pixel to the left
			}
			if(x<XRES-1){
				color+=cb[y][x+1];  //pixel to the right
			}
			if(y<YRES-1){
				color+=cb[y+1][x];
			}
			
			color=(color>>2)-speed; //average (well, often) and fade
			
			if(color>NCOLORS-1)
				color=NCOLORS-1;
			if(color<0)
				color=0;
				
			block[x]=(Uint16)ctab[color];
			cb[y][x]=color;									
		}
		sge_write_block16(surface, block, y); //Copy the new block to screen
	}	
	
	if(SDL_MUSTLOCK(surface))
		SDL_UnlockSurface(surface);
				
}