Źródło:
Komentarze (0)
| | źródło pochodzi od: fafek Opis: Silnik graficzny mojej pierwszej gry. Projekt od ponad dwóch lat nierozwijany, ale może udałoby się go wznowić? Podejrzewam, że bardziej opłacalnym byłoby napisanie tego od nowa. Ale może komuś się coś przydać.
Do obsługi grafiki i zdarzeń wykorzystana biblioteka SDL. Język: c |
#include "global.h"
/*
*
functions getpixel and putpixel are from http://libsdl.org page.
*/
U
int32 getpixel(SDL_Surface *surface,
int x,
int y)
{
int bpp = surface->
format->BytesPerPixel;
U
int8 *p = (U
int8 *)surface->pixels + y * surface->pitch + x * bpp;
switch(bpp) {
case 1:
return *p;
case 2:
return *(U
int16 *)p;
case 3:
if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
return p[
0] <<
16 | p[
1] <<
8 | p[
2];
else
return p[
0] | p[
1] <<
8 | p[
2] <<
16;
case 4:
return *(U
int32 *)p;
default:
return 0;
}
}
void putpixel(SDL_Surface *surface,
int x,
int y, U
int32 pixel)
{
int bpp = surface->
format->BytesPerPixel;
U
int8 *p = (U
int8 *)surface->pixels + y * surface->pitch + x * bpp;
switch(bpp) {
case 1:
*p = pixel;
break;
case 2:
*(U
int16 *)p = pixel;
break;
case 3:
if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
p[
0] = (pixel >>
16) &
0xff;
p[
1] = (pixel >>
8) &
0xff;
p[
2] = pixel &
0xff;
}
else {
p[
0] = pixel &
0xff;
p[
1] = (pixel >>
8) &
0xff;
p[
2] = (pixel >>
16) &
0xff;
}
break;
case 4:
*(U
int32 *)p = pixel;
break;
}
}
void drawCursor(
unsigned int x,
unsigned int y)
{
SDL_Rect img, dest;
img.w=
33; img.h=
15;
img.x=
0; img.y=
45;
dest.x=x; dest.y=y;
SDL_BlitSurface(__menuSurface, &img, screen, &dest);
}
void showMap()
{
word i;
short int _sX=
0, _sY=
0;
SDL_Rect img, dest;
/*
if(strcmp(_opcjeMapy.tlo,
"none")!=
0)
SDL_BlitSurface(__bgSurface,
NULL, screen,
NULL);
else*/
Clear(screen,
0x00ffffff);
if(__shake>
0)
{
_sX=
rand()%
5-
3;
_sY=
rand()%
5-
3;
}
for (i=
0; i<_opcjeMapy.FieldsCount; i++)
if(_pola[i].animID !=
0 && _obiekty[_pola[i].objectID].obType ==
0)
{
if(_ramki[_pola[i].animID].Count==
1) img.x=_ramki[_pola[i].animID].StartX;
else img.x=_ramki[_pola[i].animID].StartX+_ramki[_pola[i].animID].Width*(_ramkaKlatka [_pola[i].animID]-
1);
img.y=_ramki[_pola[i].animID].StartY;
img.w=_ramki[_ pola[i].animID].Width;
img.h=_ramki[_pola[i].animID].Height;
if(__shak e>
0)
{
dest.x=_pola[i].posX-__HeroPositionX+
290+_sX;
if(__HeroPositi onY>
200)
dest.y=
480-_ramki[_pola[i].animID].Height-_pola[i].posY+__HeroPositionY-
20 0+_sY;
else
dest.y=
480-_ramki[_pola[i].animID].Height-_pola[i].posY+_sY ;
}
else
{
dest.x=_pola[i].posX-__HeroPositionX+
290;
if(__Her oPositionY>
200)
dest.y=
480-_ramki[_pola[i].animID].Height-_pola[i].posY+__HeroPositionY-
20 0;
else
dest.y=
480-_ramki[_pola[i].animID].Height-_pola[i].posY;
}
SDL_BlitSurface( __chipsetSurface, &img, screen, &dest);
}
for(i=
0; i<_opcjeMapy.FieldsCount; i++)
if(_pola[i].animID !=
0 && (_obiekty[_pola[i].objectID].obType >
1/* || _pola[i].posY> __HeroPositionY*/) )
{
if(_ramki[_pola[i].animID].Count==
1 || _ramki[_pola[i].animID].Count==
0) img.x=_ramki[_pola[i].animID].StartX;
else img.x=_ramki[_pola[i].animID].StartX+_ramki[_pola[i].animID].Width*(_ramkaKlatka [_pola[i].animID]-
1);
img.y=_ramki[_pola[i].animID].StartY;
img.w=_ramki[_ pola[i].animID].Width;
img.h=_ramki[_pola[i].animID].Height;
if(__shak e>
0)
{
dest.x=_pola[i].posX-__HeroPositionX+
290+_sX;
if(__HeroPositi onY>
200)
dest.y=
480-_ramki[_pola[i].animID].Height-_pola[i].posY+__HeroPositionY-
20 0+_sY;
else
dest.y=
480-_ramki[_pola[i].animID].Height-_pola[i].posY+_sY ;
}
else
{
dest.x=_pola[i].posX-__HeroPositionX+
290;
if(__Her oPositionY>
200)
dest.y=
480-_ramki[_pola[i].animID].Height-_pola[i].posY+__HeroPositionY-
20 0;
else
dest.y=
480-_ramki[_pola[i].animID].Height-_pola[i].posY;
}
SDL_BlitSurface( __chipsetSurface, &img, screen, &dest);
}
img.y=
100*(
get_hero_status(
1)-
1);
/*
if(get_hero_status(
1)==
1 && _heroKlatka>
1)
printf(
"ERRORn");*/
img.x=
60*_heroKlatka;
img.w=
60; img.h=
100;
dest.x=
290;
if(__HeroPositionY>
200) dest.y=
180;
else dest.y=
380- __HeroPositionY;
SDL_BlitSurface(__heroSurface, &img, screen, &dest);
/*this loop is for that objects, who should cover hero*/
for(i=
0; i<_opcjeMapy.FieldsCount; i++)
if(_pola[i].animID !=
0 && _obiekty[_pola[i].objectID].obType ==
1/* && _pola[i].posY < __HeroPositionY*/ )
{
if(_ramki[_pola[i].animID].Count==
1) img.x=_ramki[_pola[i].animID].StartX;
else img.x=_ramki[_pola[i].animID].StartX+_ramki[_pola[i].animID].Width*(_ramkaKlatka [_pola[i].animID]-
1);
img.y=_ramki[_pola[i].animID].StartY;
img.w=_ramki[_ pola[i].animID].Width;
img.h=_ramki[_pola[i].animID].Height;
if(__shak e>
0)
{
dest.x=_pola[i].posX-__HeroPositionX+
290+_sX;
if(__HeroPositi onY>
200)
dest.y=
480-_ramki[_pola[i].animID].Height-_pola[i].posY+__HeroPositionY-
20 0+_sY;
else
dest.y=
480-_ramki[_pola[i].animID].Height-_pola[i].posY+_sY ;
}
else
{
dest.x=_pola[i].posX-__HeroPositionX+
290;
if(__Her oPositionY>
200)
dest.y=
480-_ramki[_pola[i].animID].Height-_pola[i].posY+__HeroPositionY-
20 0+_sY;
else
dest.y=
480-_ramki[_pola[i].animID].Height-_pola[i].posY+_sY ;
}
SDL_BlitSurface( __chipsetSurface, &img, screen, &dest);
}
char s[
8];
sprintf(s,
"%i", __lives);
print_string(_(
"Lives:"),
500,
2);
print_string(s,
570,
0);
print_string(_(
"Points:"),
500,
20);
sprintf(s,
"%lu", __po
ints);
print_string(s,
500,
38);
if(__fight>
0)
print_string(
"BANG!",
295,
360- __HeroPositionY);
}
void Clear(SDL_Surface *dest, U
int32 color)
{
if(__plane!=
100 && __bgSurface!=
NULL)
SDL_BlitSurface(__bgSurface,
NULL, screen,
NULL);
else
{
SDL_Rect rect = {
0,
0, dest->w, dest->h};
SDL_FillRect(dest, &rect, color);
}
}
void showMenu(word menu_level)
{
SDL_Rect img, dest;
switch(__plane)
{
case 50:
/*menu shown during game*/
{
img.y=img.x=
0;
/*button image*/
img.w=
145; img.h=
44;
dest.y=dest.x=
100;
SDL_BlitSurface(__menuSurface, &img, screen, &dest);
dest.y+=
44;
SDL_BlitSurface(__menuSurface, &img, screen, &dest);
dest.y+=
44;
SDL_BlitSurface(__menuSurface, &img, screen, &dest);
drawCursor(
60,
120+(__cursorPos*
45));
img.w=
170; img.h=
100;
/*fafek prod.*/
img.x=
0; img.y=
60;
dest.x=
350; dest.y=
60;
SDL_BlitSurface(__menuSurface, &img, screen, &dest);
print_string(_(
"Game menu:"),
100,
75);
print_string(_(
"Resume"),
133,
117);
print_string(_(
"Save"),
133,
161);
print_string(_(
"Quit"),
133,
205);
break;
}
case 51:
{
print_string(_(
"Save game as:"),
100,
100);
print_string(__savegamestr,
100,
120);
break;
}
case 100:
/*display main menu buttons*/
{
img.y=img.x=
0;
/*button image*/
img.w=
145; img.h=
44;
dest.y=dest.x=
100;
SDL_BlitSurface(__menuSurface, &img, screen, &dest);
dest.y+=
44;
SDL_BlitSurface(__menuSurface, &img, screen, &dest);
dest.y+=
44;
SDL_BlitSurface(__menuSurface, &img, screen, &dest);
dest.y+=
44;
SDL_BlitSurface(__menuSurface, &img, screen, &dest);
drawCursor(
60,
120+__cursorPos*
45);
/* img.w=
170; img.h=
100; fafek prod.
img.x=
0; img.y=
60;
dest.x=
350; dest.y=
60;
SDL_BlitSurface(__menuSurface, &img, screen, &dest);*/
static word ran
dom, ran
dom
1;
SDL_LockSurface(screen);
SDL_LockSurface(__menuSurface);
i
f(ran
dom<
999) ran
dom++;
else ran
dom=
1;
for(img.w=
0; img.w<=
170; img.w++)
for(img.h=
0; img.h<=
100; img.h++)
{
if(ran
dom<=
500) img.x=ran
dom;
else img.x=
1000-ran
dom;
if(rand() % img.x <
rand() %
100)
putpixel(screen, img.w+
350, img.h+
60,
0x00ffffff);
/*biały*/
else
putpixel(screen, img.w+
350, img.h+
60, getpixel(__menuSurface, img.w, img.h+
60));
/*norm*/
}
SDL_UnlockSurface(screen);
SDL_UnlockSurface(__menuSurface) ;
if(__show_mail==
1)
{
SDL_LockSurface(screen);
SDL_LockSurface(__ menuSurface);
if(ran
dom
1<
999) ran
dom
1++;
else ran
dom
1=
1;
for(img.w=
0; img.w<=
213; img.w++)
for(img.h=
0; img.h<=
34; img.h++)
{
if(ran
dom
1<=
500) img.x=ran
dom
1;
else img.x=
1000-ran
dom
1;
if(rand() % img.x <
rand() %
100)
putpixel(screen, img.w+
350, img.h+
400,
0x00ffffff);
/*biały*/
else
putpixel(screen, img.w+
350, img.h+
400, getpixel(__menuSurface, img.w, img.h+
160));
/*norm*/
}
SDL_UnlockSurface(screen);
SDL_UnlockSurface(__menuSurface) ;
}
print_string(_(
"New game"),
133,
117);
print_string(_(
"Load"),
133,
161);
print_string(_(
"Options"),
133,
205);
print_string(_(
"Exit"),
133,
249);
break;
}
case 103:
/*options*/
print_string(_(
"Options inavaliable from here now. "),
100,
100);
print_string(_(
"Please set it from command line (see '--help')."),
100,
120);
print_string(_(
"Press Escape to back"),
100,
200);
break;
case 102:
{
print_string(_(
"Load game:"),
100,
100);
print_string(__savegamestr,
100,
120);
break;
}
case 110:
/*scenario list*/
{
showList(__scenarioCount, _maps, _(
"Select scenario:"));
break;
}
}
}
powrótTagi do źródła:
sdl, c
Nikt jeszcze nie skomentował tego źródła.