Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
- &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
- R O U T I N E S :
- &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
- &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
- // frees a buffer
- void cleanbuf(void*buffer) {
- SDL_FreeSurface((SDL_Surface*)buffer);
- buffer = NULL;
- }
- // pauses for given time in milliseconds
- // THIS IS DEPRECATED, KEPT FOR POSSIBLE
- // TESTING USES ONLY
- inline void pausems_deprecated(unsigned int ms) {
- SDL_Delay(ms);
- }
- // flips the non-intermediate screen
- int updatescreen(void) {
- return (SDL_Flip((SDL_Surface*)screen)==-1)?(ERROR_EXIT):(CLEAN_EXIT);
- }
- // loads an image of pretty much any image format
- // and returns the subsequent SDL_Surface made
- SDL_Surface*load_image(char*filename) {
- SDL_Surface* img = IMG_Load(filename);
- if(img) { img = SDL_DisplayFormatAlpha(img); }
- return img;
- }
- // simply draws data from one surface to another
- // no locking/unlocking done, nothing special
- void draw(int x, int y, SDL_Surface* source, SDL_Surface* destination) {
- SDL_Rect offset;
- offset.x = x;
- offset.y = y;
- SDL_BlitSurface(source, NULL, destination, &offset);
- }
- // creates a new surface with a width and height
- // creates as an SDL_Surface
- SDL_Surface*create_surface(int width, int height) {
- SDL_Surface*img = SDL_CreateRGBSurface(0, width, height, SCREEN_BPP, 0, 0, 0, 0);
- return img;
- }
- // set to fullscreen (really no use to this)
- void set_fullscreen(void) {
- unsigned int flags = screen->flags;
- screen = SDL_SetVideoMode(0, 0, 0, screen->flags ^ SDL_FULLSCREEN);
- if(screen == NULL) { screen = SDL_SetVideoMode(0, 0, 0, flags); }
- if(screen == NULL) { exit(ERROR_EXIT); }
- }
- // draws a rectangle on the given buffer
- void rectangle(SDL_Rect*rect, SDL_Surface*buffer, Uint32 color) {
- SDL_FillRect(buffer, rect, color);
- }
- // sets buffer to all black
- void blackout(SDL_Surface*buffer) {
- SDL_Rect rect = {0, 0, buffer->w, buffer->h};
- rectangle(&rect, buffer, COLOR_BLACK);
- }
- // returns pointer to new rectangle struct
- SDL_Rect*rect(int x, int y, int w, int h) {
- static SDL_Rect rect;
- rect.x = x; rect.y = y; rect.w = w; rect.h = h;
- return ▭
- }
- // returns pointer to new rectangle struct
- // only with an x and y
- SDL_Rect*coord(int x, int y) {
- static SDL_Rect rect;
- rect.x = x; rect.y = y;
- return ▭
- }
- // displays the small buffer onto the big buffer and updates and stuff
- // good stuff
- void updatesmallscreen(void) {
- Uint32 color = 0x00000000;
- Uint32*pixels = (Uint32*)intscreen->pixels;
- if(should_lock) {
- SDL_LockSurface(intscreen);
- }
- blackout(screen);
- for(int j = 0; j < 128; j++) {
- for(int i = 0; i < 192; i++) {
- color = pixels[j * intscreen->w + i];
- rectangle(rect(i*SCREEN_ZOOM_FACTOR, j*SCREEN_ZOOM_FACTOR, SCREEN_ZOOM_FACTOR,
- SCREEN_ZOOM_FACTOR), screen, color);
- }
- }
- if(should_lock) {
- SDL_UnlockSurface(intscreen);
- }
- if(updatescreen() != CLEAN_EXIT) {
- exit(ERROR_EXIT);
- }
- }
- // draws a line on the given buffer
- void line(int startx, int starty, int endx, int endy, SDL_Surface*buffer, Uint32 color) {
- int t, distance;
- int xerr=0, yerr=0, delta_x, delta_y;
- int incx, incy;
- delta_x=endx-startx;
- delta_y=endy-starty;
- if(delta_x>0) incx=1;
- else if(delta_x==0) incx=0;
- else incx=-1;
- if(delta_y>0) incy=1;
- else if(delta_y==0) incy=0;
- else incy=-1;
- delta_x=abs(delta_x);
- delta_y=abs(delta_y);
- if(delta_x>delta_y) distance=delta_x;
- else distance=delta_y;
- for(t=0; t<=distance+1; t++) {
- rectangle(pixel(startx, starty), buffer, color);
- xerr+=delta_x;
- yerr+=delta_y;
- if(xerr>distance) {
- xerr-=distance;
- startx+=incx;
- }
- if(yerr>distance) {
- yerr-=distance;
- starty+=incy;
- }
- }
- }
- // constructor for a pixel (rectangle-based)
- // no attached color
- SDL_Rect*pixel(int x, int y) {
- static SDL_Rect rect;
- rect.x = x; rect.y = y; rect.w = 1; rect.h = 1;
- return ▭
- }
- // exits the program quickly and cleanly
- void quit_quickly(void) {
- cleanbuf(intscreen);
- musicend();
- Mix_CloseAudio();
- SDL_Quit();
- exit(CLEAN_EXIT);
- }
- // starts playing music in a loop
- void musicset(Mix_Music*music) {
- Mix_PlayMusic(music, -1);
- }
- // starts playing music, no loop
- void musicsetstart(Mix_Music*music) {
- Mix_PlayMusic(music, 0);
- }
- // stops the music
- inline void musicpause(void) {
- Mix_PauseMusic();
- }
- // starts the music
- inline void musicresume(void) {
- Mix_ResumeMusic();
- }
- // opens a music file
- Mix_Music*open_music(char*filename) {
- return Mix_LoadMUS(filename);
- }
- // stop all music
- inline void musicend(void) {
- Mix_HaltMusic();
- }
- // check if music is playing
- inline int musicisplaying(void) {
- return Mix_PlayingMusic();
- }
- //pauses without bad effects
- void pausems(int ms) {
- SDL_Event*event = malloc(sizeof(SDL_Event));
- int timer = SDL_GetTicks();
- while(SDL_GetTicks() - timer < ms) {
- SDL_PollEvent(event);
- if(event->type == SDL_QUIT) {
- quit_quickly();
- }
- }
- }
- // draws sprite to intscreen
- void drawsprite(int x, int y, int w, int h, SDL_Surface*surface, int xl, int yl) {
- SDL_Rect*rec = rect(xl, yl, w, h);
- SDL_Rect*cord = coord(x, y);
- SDL_BlitSurface(surface, rec, intscreen, cord);
- }
- // draws a pixel to intscreen
- void drawpixel(SDL_Rect*rec, Uint32 color) {
- rectangle(rect(rec->x, rec->y, 1, 1), intscreen, color);
- }
Add Comment
Please, Sign In to add comment