The great mystery

This is a discussion on The great mystery within the A Brief History of Cprogramming.com forums, part of the Community Boards category; /* very crude probably slow way of drawing a box assumes screen is setup to 32 bits */ should be ...

  1. #31
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    /* very crude probably slow way of drawing a box
    assumes screen is setup to 32 bits
    */
    should be 16 bits.

    My erase_box and draw_box are so slow that
    they eat up 99% of the time running.

  2. #32
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    I guess I don't need to be setting xaccel and yaccel to 0 in
    the update function.

  3. #33
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267
    >What do you use for input? I've tried sdl but I've run in to 3 probloms

    1. Must press key for input (no holding it down)
    2. No way to detrerm mouse postion
    3. No gamepads.

    Don't tell me you use glut.<

    dont use glut or sdl...

    for key input use GetAsyncKeyState() joyGetpos() can be used for joysticks as well...or just use windows messaging(HAH)
    or write your own(though few will do such i'm not gonna yet)... dependes on what your need are for viewers WM should be sufficient for Gaming GetAsyncKeyState() or custom should be sufficient...

    >2. No way to detrerm mouse postion

    WTF does this even mean???

    BTWA: im meant as im in For OpenGL not a Pro at OpenGL
    Last edited by no-one; 08-15-2001 at 02:40 AM.

  4. #34
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    >>"What if the doorman was a homosexual. Could you get the 'home time' with a complaint of not being able to walk properly" ??

    Woah - didnt think of that! Futher pondering needed.

    Anyway I'm not prepared to test it.

  5. #35
    ggs
    ggs is offline
    C > C++ duders ggs's Avatar
    Join Date
    Aug 2001
    Posts
    435

    this is about SDL, OT

    That stuff with the switch(event.stuff) always bothered me. There's another, better way of grabbing input... I've lost all of my SDL related source in a disk crash a while ago, but I know you can grab the entire state of every key in the keyboard into a char[256] array and you can use that to determine which keys are on and off at once. I think it automatically updates everytime you do an SDL_PumpEvents() (I think that's the function anyways..)
    .sect signature

  6. #36
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    Here's some better code and I used SDL_Rect so it should be



    easier.







    Yea I gues you can use SDL_GetKeyState







    NAME



    SDL_GetKeyState- Get a snapshot of the current keyboard



    state







    SYNOPSIS



    #include "SDL.h"







    Uint8 *SDL_GetKeyState(int *numkeys);







    DESCRIPTION



    Gets a snapshot of the current keyboard state. The current



    state is return as a pointer to an array, the size of this



    array is stored in numkeys. The array is indexed by the



    SDLK_* symbols. A value of 1 means the key is pressed and



    a value of 0 means its not.







    Note:







    Use SDL_PumpEvents to update the state array.







    EXAMPLE



    Uint8 *keystate = SDL_GetKeyState(NULL);



    if ( keystate[SDLK_RETURN] ) printf("Return Key Pressed.



    ");





    Code:
    #include <SDL/SDL.h>
    #include <memory.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    struct Box {
        int x, y;
        int w, h;
        int xvel, yvel;
        int xaccel, yaccel;
        Uint32 color;
    };
    
    #define USE_DOUBLEBUFFER 0
    
    typedef struct Box Box;
    
    SDL_Surface* global_screen;
    Box global_my_box;
    
    
    void init_box(Box* box, int x, int y, int w, int h,
                  int xvel, int yvel, Uint32 color);
    
    void draw_box(Box* box);
    void erase_box(Box* box);
    void update_box(Box* box);
    void quit_box_game(void);
    void handle_events(void);
    
    
    int main(void)
    {
        Uint32 video_flags;
        Uint32 color_blue;
        Uint32 time_stamp;
        Uint32 delta_time;
        double frame_rate;
    
        video_flags = SDL_FULLSCREEN | SDL_HWSURFACE;
    
    #if USE_DOUBLEBUFFER
        video_flags |= SDL_FULLSCREEN;
    #endif
    
        if (SDL_Init(SDL_INIT_VIDEO) < 0) {
            fprintf(stderr, "Error initializing SDL: %s\n", SDL_GetError());
            return EXIT_FAILURE;
        }
        atexit(SDL_Quit);
    
        global_screen = SDL_SetVideoMode(800, 600, 16, video_flags);
        if (global_screen == NULL) {
            fprintf(stderr, "Error setting video mode: %s\n", SDL_GetError());
            return EXIT_FAILURE;
        }
    
        color_blue = SDL_MapRGB(global_screen->format, 0, 0, 255);
        init_box(&global_my_box, 30, 30, 50, 100, 1, 2, color_blue);
    
        for(;;) {        
            time_stamp = SDL_GetTicks();
            erase_box(&global_my_box);
            handle_events();
            update_box(&global_my_box);
            draw_box(&global_my_box);        
    #if USE_DOUBLEBUFFER
            SDL_Flip(global_screen);
    #endif
            delta_time = SDL_GetTicks() - time_stamp;
            if (delta_time < 20) {
                SDL_Delay(20 - delta_time);
            }
        }
    
        return 0;
    }
    
    void init_box(Box* box, int x, int y, int w, int h,
                  int xvel, int yvel, Uint32 color)
    {
        box->x = x;
        box->y = y;
        box->w = w;
        box->h = h;
        box->xvel= xvel;
        box->yvel = yvel;
        box->color = color;
    }
    
    void draw_box(Box* box)
    {
        SDL_Rect rect;
        
        rect.x = box->x;
        rect.y = box->y;
        rect.w = box->w;
        rect.h = box->h;
        SDL_FillRect(global_screen, &rect, box->color);
    
    #if !USE_DOUBLEBUFFER
        SDL_UpdateRect(global_screen, rect.x, rect.y, rect.w, rect.h);
    #endif
    }
    
    
    void erase_box(Box* box)
    {
        SDL_Rect rect;
        
        rect.x = box->x;
        rect.y = box->y;
        rect.w = box->w;
        rect.h = box->h;
        SDL_FillRect(global_screen, &rect, 0);
    
    #if !USE_DOUBLEBUFFER
        SDL_UpdateRect(global_screen, rect.x, rect.y, rect.w, rect.h);
    #endif
    }
    
    void update_box(Box* box)
    {
        box->xvel += box->xaccel;
        box->yvel += box->yaccel;
    
        if (box->xvel > 30) box->xvel = 20;
        if (box->yvel > 30) box->yvel = 20;
    
        box->x += box->xvel;
        box->y += box->yvel;
    
        if (box->y < 0) {
            box->y = 0;
            box->yvel = -box->yvel;
        }
        if (box->y + box->h >= global_screen->h) {
            box->y = global_screen->h - box->h - 1;
            box->yvel = -box->yvel;
        }
    
        if (box->x < 0) {
            box->x = 0;
            box->xvel = -box->xvel;
        }
        if (box->x + box->w >= global_screen->w) {
            box->x = global_screen->w - box->w - 1;
            box->xvel = -box->xvel;
        }
    }
    
    
    void handle_events()
    {
        SDL_Event event;
    
        while(SDL_PollEvent(&event)) {
            switch(event.type) {
            case SDL_KEYDOWN:
                switch(event.key.keysym.sym) {
                case SDLK_RIGHT:
                    global_my_box.xaccel = 2;
                    break;
                case SDLK_LEFT:
                    global_my_box.xaccel = -2;
                    break;
                case SDLK_UP:
                    global_my_box.yaccel = -2;
                    break;
                case SDLK_DOWN:
                    global_my_box.yaccel = 2;
                    break;
                case SDLK_ESCAPE:
                    quit_box_game();
                    break;
                default:
                    break;
                }
                break;
            case SDL_KEYUP:
                switch(event.key.keysym.sym) {
                case SDLK_RIGHT:     /* fall through */
                case SDLK_LEFT:
                    global_my_box.xaccel = 0;
                    break;
                case SDLK_UP:       /* fall through */
                case SDLK_DOWN:
                    global_my_box.yaccel = 0;
                    break;
                default:
                    break;
                }
                break;
            case SDL_QUIT:
                quit_box_game();
                break;
            default:
                break;
            }
        }
    }
    
    
    void quit_box_game(void)
    {
        exit(EXIT_SUCCESS);
    }

  7. #37
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,230

    aa!!

    it's grown huge since last I checked...

    Oh my, are you really prepared to f**k the doorman to get off of work? I never knew you were like that, govtcheez (and Fordy...) what's with the GF, then?

    Dean, I could have done just fine without hearing that you wear a diaper.

  8. #38
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    >>Anyway I'm not prepared to test it.

    >>Anyway I'm not prepared to test it.

    >>Anyway I'm not prepared to test it.

    Thats what I said in my last post. Anyway, its not me that mentioned getting f#ucked, all I was suggesting was having the sh#t kicked out of me - far more acceptable.

    If Dean wears a diaper - do you think it would have a M$ logo on the front and .NET on the back?

  9. #39
    Registered User rick barclay's Avatar
    Join Date
    Aug 2001
    Posts
    835
    And brown splotchy pictures of Bill Gates plastered
    everywhere imaginable.

    rick barclay
    No. Wait. Don't hang up!

    This is America calling!

  10. #40
    .
    Join Date
    Aug 2001
    Posts
    598
    Thanks nick, Ill try to implant what you said. I am going to try to upload what I have now. Look for a thread called God Pong -update more details will be posted there.
    To Err Is To Be Human. To Game Is Divine!"

Page 3 of 3 FirstFirst 123
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Making great graphics
    By MadCow257 in forum Game Programming
    Replies: 1
    Last Post: 02-20-2006, 10:59 PM
  2. Great Teacher..
    By vasanth in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 03-03-2003, 05:40 AM
  3. programming was great until c came along
    By Brian in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 10-04-2002, 07:37 PM
  4. What's so great about C++?
    By InFeStEd-ArCh0n in forum C++ Programming
    Replies: 32
    Last Post: 05-07-2002, 09:49 PM
  5. Mystery.
    By Nutshell in forum C Programming
    Replies: 1
    Last Post: 01-27-2002, 12:41 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21