Hello,
I tried my best: I’ve read,read,read and coded,coded,coded but what I want
to do still didn’t work…
My goal is to create a window in which :
1- the background comes from an image
2- a box filled by a color chosen by me of a certain size can be blitted on
the screen
3- click on the right button modifies the direction (Nord, East, South,
West, Nord, East, South, West etc etc) and click on the left button applies
the move. e.g:the default direction is East. If I click on the left, the box
goes 1 pixel on the right, I click on the right button, the box goes south,
etc…etc…
My problem is, when I create this box, I CANNOT fill it with a certain
color. It never works.
Here is my code:
(only the east/south thing is defined)*********************************
#include <stdio.h>
#include <SDL/SDL.h>
#define Set_Flags SDL_HWSURFACE|SDL_DOUBLEBUF/*|SDL_FULLSCREEN */
#define Set_Width 640
#define Set_Height 384
#define X 1
#define Y 0
int DEST_X=100, DEST_Y=100, STATE=X;
int main(int argc, char **argv){
SDL_Surface image; / images from the bmp file */
SDL_Surface screen; / on what EVERYTHING is mapped to */
SDL_Surface cases; / surface on top of screen */
SDL_Surface *temp;
SDL_Rect src,dest;
int ncolors,i,j;
SDL_Color *colors;
SDL_Event event;
if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER) <0) {
fprintf(stderr, “Unable to init SDL: %s\n”, SDL_GetError());
exit(1);
}
atexit(SDL_Quit);
SDL_WM_SetCaption(“test by Aurelien M.”,“Test”);
image= SDL_LoadBMP("…/…/images/tiles/test2.bmp");
if
((cases=SDL_CreateRGBSurface(SDL_HWSURFACE,100,100,image->format->BitsPerPix
el,image->format->Rmask,image->format->Gmask,image->format->Bmask,0)) ==
NULL){
fprintf(stderr,“cases can’t be created: %s\n”,SDL_GetError());
exit(1);
}
if
((temp=SDL_CreateRGBSurface(SDL_HWSURFACE,640,384,image->format->BitsPerPixe
l,image->format->Rmask,image->format->Gmask,image->format->Bmask,0)) ==
NULL){
fprintf(stderr,“temp can’t be created: %s\n”,SDL_GetError());
exit(1);
}
screen=
SDL_SetVideoMode(Set_Width,Set_Height,image->format->BitsPerPixel,Set_Flags)
;
if (screen==NULL){
fprintf(stderr,“Couldn’t set 640x480x8 Video mode:
%s\n”,SDL_GetError());
exit(1);
}
if (image==NULL){
fprintf(stderr,“Couldn’t load test.bmp: %s\n”,SDL_GetError());
exit(1);
}
if (image->format->palette){
ncolors=image->format->palette->ncolors;
colors= (SDL_Color )malloc(ncolorssizeof(SDL_Color));
memcpy(colors,image->format->palette->colors,ncolors);
} else {
fprintf(stderr,“image not palettized: %s\n”,SDL_GetError());
exit(1);
}
SDL_FillRect(screen,NULL,SDL_MapRGB(image->format,0,0,255));
if ( SDL_Flip(screen) == 0){
fprintf(stderr,“Support of double buffering: ok!\n”);
} else {
fprintf(stderr,“No support of double buffering: %s”,SDL_GetError());
}
SDL_FillRect(cases,NULL,123);
SDL_UpdateRect(cases,0,0,0,0);
dest.x=dest.y=0;
dest.w=Set_Width;
dest.h=Set_Height;
for(j=0;j<6;j++)
for(i=0;i<10;i++){
dest.x=64i;
dest.y=64j;
dest.w=image->w;
dest.h=image->h;
SDL_BlitSurface(image,NULL,screen,&dest);
}
SDL_Flip(screen);
src.x=0;
src.y=0;
dest.h=src.h=cases->h;
dest.w=src.w=cases->w;
dest.x=DEST_X;
dest.y=DEST_Y;
SDL_BlitSurface(cases,&src,screen,&dest);
SDL_Flip(screen);
while(SDL_WaitEvent(&event) >=0){
if(event.type == SDL_KEYDOWN){
if(event.key.keysym.sym == SDLK_BACKQUOTE){
break;
} else {
fprintf(stderr,"%s has been
pressed\n",SDL_GetKeyName(event.key.keysym.sym));
}
} else {
if(event.type == SDL_MOUSEBUTTONDOWN){
fprintf(stderr,“bouton %d pressed.\n”,event.button.button);
if(event.button.button == 1){
/* button 1 presse /
if(STATE == X)
DEST_X=DEST_X+1;
else
DEST_Y=DEST_Y+1;
} else {
/ button 3 presse */
STATE=1-STATE;
}
}
dest.x=DEST_X;
dest.y=DEST_Y;
dest.w=screen->w;
dest.h=screen->h;
src.x=DEST_X;
src.y=DEST_Y;
src.w=cases->w;
src.h=cases->h;
SDL_BlitSurface(cases,NULL,screen,&src);
SDL_Flip(screen);
}
}
SDL_FreeSurface(cases);
SDL_FreeSurface(image);
SDL_FreeSurface(screen);
return;
}
thanks for your help…
Aurelien Marchand