All pastes #633950 Raw Edit

Anonymous

public text v1 · immutable
#633950 ·published 2007-07-26 01:33 UTC
rendered paste body
#include <SDL/SDL.h>
#include <stdio.h>
#include <unistd.h>

#define WIDTH 800
#define HEIGHT 600
#define DEPTH 8 
#define ICON_NAME "SDL_is-teh-pwn"
#define TITLE "Axis Test"
#define BUFFER 20
#define AXIS 50
#define DOT 15

int main(void)
{
    int x,y;
    SDL_Surface *screen;
    char axis[AXIS];
    SDL_Rect dot;
    Uint32 color, dotcolor;
    dot.w = DOT;
    dot.h = DOT;

    /*SDL_Event event; */
   
    /* Initialize screen */
    if ( SDL_Init(SDL_INIT_VIDEO) != 0 )
    {
        fprintf( stderr,"Error: %s\n", SDL_GetError() );
        exit(1);
    }
    screen = SDL_SetVideoMode( WIDTH, HEIGHT, DEPTH, SDL_SWSURFACE | SDL_RESIZABLE );
    if (screen == NULL)
    {
        fprintf( stderr,"Error: %s\n", SDL_GetError() );
        exit(1);
    }

    SDL_WM_SetCaption( TITLE, NULL);

    color = SDL_MapRGB( screen->format, 51, 74, 157); /* Maps screen format to blu-ish something... */
    dotcolor = SDL_MapRGB( screen->format , 0, 0, 0); /* Maps format to black */
    SDL_FillRect(screen, NULL, color); /* Fill screen with color white */
 
    while (1)
    {
        SDL_PumpEvents();
        SDL_GetMouseState(&x,&y);
        snprintf(axis, BUFFER, "%s %dx%d", TITLE, x, y);
        SDL_WM_SetCaption(axis, ICON_NAME);
        
        /* This is so the coordinates doesn't exceed the window and mess up the rect */
        if ( x + DOT > WIDTH ) 
            dot.x = (WIDTH - DOT);
        else
            dot.x = x ; 
        
        if (y + DOT > HEIGHT) 
            dot.y = (HEIGHT - DOT);
        else
            dot.y = y;

        SDL_FillRect(screen,NULL,color);
        SDL_FillRect(screen, &dot, dotcolor);
        SDL_Flip(screen);
        
        if ( SDL_GetMouseState(NULL, NULL) & SDL_BUTTON(1) || SDL_GetMouseState(NULL, NULL) & SDL_BUTTON(3) )
        {
            printf("A mouse button was used, exiting!\n");
            break;
        }
        /* Just isn't as fast as the above, though you can resize...
        SDL_PollEvent(&event);
        switch (event.type) 
        {
            case SDL_MOUSEBUTTONDOWN:
                printf("A mouse button press was detected. Quitting!\n");
                SDL_Quit();
                exit(0);
            case SDL_VIDEORESIZE:
                screen = SDL_SetVideoMode(event.resize.w, event.resize.h, 8, SDL_SWSURFACE | SDL_RESIZABLE);
                SDL_Flip(screen);
            case SDL_MOUSEMOTION:
                snprintf(axis ,BUFFER,"%s %dx%d", TITLE, event.motion.x, event.motion.y);
                SDL_WM_SetCaption( axis , ICON_NAME );
                SDL_Flip(screen);
                continue;
            default:
                continue;
        }*/
    }

    /* Clean up and return */
    SDL_FreeSurface(screen);
    SDL_Quit();
    return (0);
}