File: blitting.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 (121 lines) | stat: -rw-r--r-- 2,907 bytes parent folder | download | duplicates (5)
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
#include <stdlib.h>
#include <stdio.h>
#include "SDL.h"
#include "sge.h"

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("Bouncing ball!", "bounce");

	/* Initialize the display */
	SDL_Surface *screen;
	screen = SDL_SetVideoMode(640, 480, 16, SDL_SWSURFACE);
	if ( screen == NULL ) {
		fprintf(stderr, "Couldn't set video mode: %s\n", SDL_GetError());
		exit(1);
	}

	/* Open TT font file */
	if(sge_TTF_Init()!=0){fprintf(stderr,"TT error: %s\n", SDL_GetError());exit(1);}
	sge_TTFont *font;
	font=sge_TTF_OpenFont("/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.ttf", 50);
	if(font==NULL){fprintf(stderr,"TT error: %s\n", SDL_GetError());exit(1);}
	
	/* Make a nice background */
 	sge_tt_textout(screen,font,"Super Fun", 160,180, 0,255,0, 0,0,0, SDL_ALPHA_OPAQUE);
 	sge_tt_textout(screen,font,"Bouncing Ball!!", 100,260, 0,255,0,0,0,0, SDL_ALPHA_OPAQUE); 	
	sge_TTF_CloseFont(font);

	/* Allocate buffers */
	SDL_Surface *buffer,*circle;
	circle=SDL_AllocSurface(SDL_SWSURFACE,100,100,16, 0,0,0,0);
	buffer=SDL_AllocSurface(SDL_SWSURFACE,640,480,16, 0,0,0,0);

 	/* Draws a filled circle on the circle buffer */
	sge_ClearSurface(circle,0);
	sge_AAFilledCircle(circle,50,50,49, 255,0,0);
	
	/* Set the transparity on the circle */
	SDL_SetColorKey(circle, SDL_SRCCOLORKEY, 0); //Set the transparent color
	SDL_SetAlpha(circle, SDL_SRCALPHA|SDL_RLEACCEL, 128);  //Set the alpha blending value

	/* Turns off automatic update */
	sge_Update_OFF();
	
	/* Buffers the background */
	sge_Blit(screen,buffer,0,0, 0,0 ,640,480);

	Sint32 x=250,y=200,oldx=250,oldy=200;
	bool left=false, down=false;
	int loops=0;

	/* Sets start time */
	Uint32 start = SDL_GetTicks();

	/* Main loop */
	SDL_Event event;
	do{
		SDL_Delay(10);
		
		/* Bounce at screen borders */
		if(x>540)
			left = true;
		else if(x<0)
			left = false;
		if(y>380)
			down = false;
		else if(y<0)
			down = true;  	

		/* Updates coords */
		if(left)
			x-=2;
		else
			x+=2;
		if(down)
			y+=2;
		else
			y-=2;

		/* Updates the last area */
		sge_Blit(buffer,screen,oldx-5,oldy-5, oldx-5,oldy-5, 120,120); oldx=x;oldy=y;	

		/* Draws the circel */
   		sge_Blit(circle,screen,0,0, x,y, 100,100);

		/* Updates the screen */
		sge_Update_ON();
		sge_UpdateRect(screen,x-5,y-5,120,120);
		sge_Update_OFF();

		/* Calculate fps */
    	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);

	
	/* Print FPS */
	printf("%.2f FPS (target: 100).\n",((double)loops*1000)/(SDL_GetTicks()-start));

	/* Clean up */
	SDL_FreeSurface(buffer);
	SDL_FreeSurface(circle);

	return 0;
}