0 Members and 1 Guest are viewing this topic.
I am a proud cynic.
#include <SDL.h>#include <SDL_image.h>#include <stdio.h>#include <stdlib.h>typedef struct{ uint8_t quit; uint8_t clic; uint8_t key[SDLK_LAST]; SDL_Rect mouse;} eventsManager;void updateEvents(eventsManager *evts){ SDL_Event event; while (SDL_PollEvent(&event)) { switch (event.type) { case SDL_KEYDOWN: evts->key [event.key.keysym.sym] = 1; break; case SDL_KEYUP: evts->key [event.key.keysym.sym] = 0; break; case SDL_QUIT: evts->quit=1; break; case SDL_MOUSEBUTTONDOWN: evts->clic=1; break; case SDL_MOUSEBUTTONUP: evts->clic=0; break; case SDL_MOUSEMOTION: evts->mouse.x=event.motion.x; evts->mouse.y=event.motion.y; break; default: break; } }}int main(int argc, char **argv){ int width,height,startx,starty,i; int current=2; char title[100]; char str[100]; FILE *filein = fopen("in.txt","r"); fscanf(filein,"%d %d %d %d",&width,&height,&startx,&starty); fclose(filein); if (width>40) width=40; if (width<10) width=10; if (height>20) height=20; if (height<7) height=7; sprintf(title, "Bobby Carrot Level Creator - %d x %d - start : %d;%d",width,height,startx,starty); uint8_t *array=malloc(width*height*sizeof(uint8_t)); memset(array,2,width*height*sizeof(uint8_t)); //vars SDL_Surface *window = NULL; SDL_Surface *toolbar = NULL; SDL_Surface* sprites[14]; SDL_Rect postb = {0,0}; SDL_Rect posblit; uint8_t end=0; unsigned int ellapsed, time; //SDL routines eventsManager events; memset(&events, 0, sizeof(eventsManager)); SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER); window=SDL_SetVideoMode(width*30,height*30+30,32,SDL_HWSURFACE|SDL_DOUBLEBUF); SDL_WM_SetCaption(title,NULL); toolbar=IMG_Load("toolbar.jpg"); for (i=0;i<14;i++) { sprintf(str,"Sprites/Bobby-%d.gif",i+1); sprites[i]=IMG_Load(str); } //render loop while (!end) { //FPS management time=SDL_GetTicks(); //Events management updateEvents(&events); if (events.quit||events.key[SDLK_ESCAPE]) end=1; if (events.key[SDLK_a]) current=1; if (events.key[SDLK_z]) current=2; if (events.key[SDLK_e]) current=3; if (events.key[SDLK_r]) current=4; if (events.key[SDLK_t]) current=5; if (events.key[SDLK_y]) current=6; if (events.key[SDLK_u]) current=7; if (events.key[SDLK_i]) current=8; if (events.key[SDLK_o]) current=9; if (events.key[SDLK_p]) current=10; if (events.key[SDLK_q]) current=11; if (events.key[SDLK_s]) current=12; if (events.key[SDLK_d]) current=13; if (events.key[SDLK_f]) current=14; if (events.mouse.y>30 && events.clic) { if (current==14) array[(events.mouse.y-30)/30*width+events.mouse.x/30]=0; else array[(events.mouse.y-30)/30*width+events.mouse.x/30]=current; } SDL_FillRect(window, NULL, SDL_MapRGB(window->format, 180, 115, 53)); for(i=0;i<width*height;i++) { if (array[i]!=0) { posblit.x=(i%width)*30; posblit.y=(i/width)*30+30; SDL_BlitSurface(sprites[array[i]-1],NULL,window,&posblit); } } SDL_BlitSurface(sprites[current-1],NULL,window,&events.mouse); SDL_BlitSurface(toolbar,NULL,window,&postb); SDL_Flip(window); //50 FPS ellapsed = SDL_GetTicks() - time; if (ellapsed < 20) SDL_Delay (20 - ellapsed); } filein=fopen("out.txt","w+"); fprintf(filein,"{%d,%d,%d,%d",width,height,startx,starty); for (i=0;i<width*height;i++) { fprintf(filein,",%d",array[i]); } fprintf(filein,"}"); fclose(filein); //Free free(array); SDL_FreeSurface(window); SDL_FreeSurface(toolbar); for (i=0;i<14;i++) { SDL_FreeSurface(sprites[i]); } SDL_Quit(); return 0;}
The graphics look really nice. This is why I wish I had an nspire...