First Project: SDL_Pong

Member
Posts: 24
Joined: 2011.06
Post: #16
Going to try and make a simple app like you suggested. Small window, move an image around, ETC.

Wish me luck! I'll post what I'm working on in a bit.

Ok I have the program up and running, but it won't load the image file I put in it. Am I calling it wrong? It doesn't seem like it because the program runs fine, but instead of a basic mac window background, it shows a black background.

It seems like it is able to draw something, but I don't know if I'm putting the image ("title.jpg") in the right place. Is that even the right function?

Thank you! Is this a little better?

EDIT: I might need to try this new framework that was released. SDL_Image that supports other file formats. I'm going to try that and let you all know.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#include "SDL.h"
int x,y;
int image, screen, dest;
int main(int argc, char *argv[])
{
    Uint32 initflags = SDL_INIT_VIDEO;  /* See documentation for details */
    SDL_Surface *screen;
    Uint8  video_bpp = 0;
    Uint32 videoflags = SDL_SWSURFACE;
    int    done;
        SDL_Event event;

    /* Initialize the SDL library */
    if ( SDL_Init(initflags) < 0 ) {
        fprintf(stderr, "Couldn't initialize SDL: %s\n",
            SDL_GetError());
        exit(1);
    }

    /* Set 640x480 video mode */
    screen=SDL_SetVideoMode(640,480, video_bpp, videoflags);
        if (screen == NULL) {
        fprintf(stderr, "Couldn't set 640x480x%d video mode: %s\n",
                        video_bpp, SDL_GetError());
        SDL_Quit();
        exit(2);
    }

    done = 0;
    while ( !done ) {

        /* Check for events */
        while ( SDL_PollEvent(&event) ) {
        /* Load and draw graphics */
            SDL_BlitSurface(screen, NULL, screen, &dest);
        /* Update the changed portion of the screen */
            SDL_UpdateRects(screen, 1, &dest);
            switch (event.type) {
                case SDL_MOUSEMOTION:
                    break;
                case SDL_MOUSEBUTTONDOWN:
                    break;
                case SDL_KEYDOWN:
                    /* Any keypress quits the app... */
                case SDL_QUIT:
                    done = 1;
                    break;
                default:
                    break;
            }
        }
    }
    
    /* Clean up the SDL library */
    SDL_Quit();
    return(0);
}
void ShowBMP(char *file, SDL_Surface *screen, int x, int y)
{
    SDL_Surface *image;
    SDL_Rect dest;

    /* Load the BMP file into a surface */
    image = SDL_LoadBMP("~Users/Carrie/SDL_Testbed/title.jpg");
    if ( image == NULL ) {
        fprintf(stderr, "Couldn't load %s: %s\n", file, SDL_GetError());
        return;
    }

    /* Blit onto the screen surface.
       The surfaces should not be locked at this point.
     */
    dest.x = x;
    dest.y = y;
    dest.w = image->w;
    dest.h = image->h;
  
}
Quote this message in a reply
Moderator
Posts: 697
Joined: 2002.04
Post: #17
Okay, I've just taken a glance at the code you posted; the code is better than the code you posted previously, but still has a few fundamental flaws, namely:
  • you're passing an int where a SDL_Rect would be expected by SDL_BlitSurface() - I'm not sure how that compiles without casts, let alone runs without crashing;
  • your call SDL_BlitSurface() is drawing the window surface to the window surface - whilst there are contexts in which you would want to do that, this isn't one of them;
  • your calls to SDL_BlitSurface() and SDL_UpdateRects() are inside the loop you use for checking for events, a loop which is only entered into if SDL_PollEvent() returns true - this means that the window surface will only be drawn to/updated if an event has occurred;
  • you never call ShowBMP();
  • you're passing an image suffixed .JPG to a function named SDL_LoadBMP() - why might it not be able to load that image? Wink
  • by default, the image path you pass to SDL_LoadBMP() should be relative to the .APP - eg. in the code I posted I passed "image.bmp" to SDL_LoadBMP() and the file structure looked like:[Image: SDLtest%20screenshot.jpg]
  • whilst the function is named ShowBMP(), it only loads the image and doesn't draw it (although you do pass the SDL_Surface* parameter necessary for drawing the image surface to the window surface) - also, even if the function did draw the image, you don't want to load the image every time you draw the image.

Mark Bishop
--
Student and freelance OS X & iOS developer
Quote this message in a reply
Member
Posts: 24
Joined: 2011.06
Post: #18
Thank you very much! I'll try these fixes tomorrow morning and see if I can get it to show my picture.

As far as the .jpg goes, I figured there was a separate function for calling the different formats of graphic files. If one just works all, then awesome.
Quote this message in a reply
Moderator
Posts: 697
Joined: 2002.04
Post: #19
(Jun 26, 2011 07:08 PM)Injury Wrote:  [...]I figured there was a separate function for calling the different formats of graphic files. If one just works all, then awesome.

I think you may have misunderstood what I was trying to say Wink The philosophy of SDL is to try to keep the size of the 'core' library small, to add features to SDL not by adding them to the 'core' library but by adding them to 'expansion' libraries – so the SDL_LoadBMP() function of the 'core' SDL library loads only .BMP images (hence the name of the function), whilst the IMG_Load() function of the SDL_image 'expansion' library loads .GIF, .JPEG, .PNG, etc. images; as at the moment you're just learning the fundamentals of SDL I can't think of a compelling reason why you'd want to use SDL_image/IMG_Load() – just re-save your images as .BMP and use SDL_LoadBMP() Smile

Mark Bishop
--
Student and freelance OS X & iOS developer
Quote this message in a reply
Member
Posts: 24
Joined: 2011.06
Post: #20
I just read what you posted on the last page and tried it out. It's way easier to understand that than what is posted online in tutorials.

It works now! I know its just rehashed code, but I think I get it now Rasp

now....how would you move that picture around with the arrow keys? too advanced?

TEACH ME GREAT ONE!
Quote this message in a reply
Moderator
Posts: 697
Joined: 2002.04
Post: #21
(Jun 27, 2011 10:06 AM)Injury Wrote:  now....how would you move that picture around with the arrow keys? too advanced?

Not at all; a few hints: you are already checking for key-down events in the code you posted here; you want to change the values of the x and y attributes of the SDL_Rect you use when blitting the image; you want to look at the documentation pertaining to the SDL_Event structure - whilst you'll want to ignore most of what that structure contains, that structure contains the keycode of the key pressed (eg. SDLK_UP for the up arrow key.)

Mark Bishop
--
Student and freelance OS X & iOS developer
Quote this message in a reply
Member
Posts: 24
Joined: 2011.06
Post: #22
Haha, awesome! I got it moving! Smile

EDIT: Awesomeness! I cut up a sprite and made him turn around in 4 directions. I just need to figure out how to make him transparent, and then I'll have a real sprite layer!

EDIT: I just got the layers thing figured out! I now have a sprite layer! Now to finish the pong game!

I looked up a bit on text and font...seems tricky/weird. I just want to be able to show a string of text along the bottom and know how to manipulate that function.

Thank you! It's coming together! Smile
UPDATE: Updated the files! Now with fully working app! in sigg! xD
Quote this message in a reply
Member
Posts: 24
Joined: 2011.06
Post: #23
Is there some reason my background layer won't load? I thought I created the right variables and called it but maybe I didn't...Anywho, I'm a bit confused how to call another layer.

I've updated main.c in the project directory in my signature. It's hosted on my private server.

Any thoughts on animation? I wanted to use a spritesheet instead of single pictures too. Oh well. I think I can get some of the leg work done on the SDL_Pong game as well.

Thanks again, seriously! I feel like I can make something fun now LOL
Quote this message in a reply
Member
Posts: 24
Joined: 2011.06
Post: #24
Anyone know anything about fonts? I'm trying to get SDL_ttf working but it throws an error when it tries to show the message. Check the main.c file in my sig for a look. Thank you!
Quote this message in a reply
Moderator
Posts: 697
Joined: 2002.04
Post: #25
You're not calling TTF_Init()...

Mark Bishop
--
Student and freelance OS X & iOS developer
Quote this message in a reply
Member
Posts: 24
Joined: 2011.06
Post: #26
Awesome! got it working! Now to figure out transparency on that and getting everything else set up! Woohoo!
Quote this message in a reply
Member
Posts: 24
Joined: 2011.06
Post: #27
EDIT: Got the text working. Wasn't calling stuff right. Got it now! woo!

Update:

Can't get the variables working...I don't know what I'm doing wrong...please refer to main.c for details. where should I update the score function? It displays "Score" fine, but not the actual variable passing it. Am I passing it the right way to the ttf Render function?
Quote this message in a reply
Moderator
Posts: 697
Joined: 2002.04
Post: #28
Hmm, I'm looking at http://www.plaidapple.ithaca-ny.com/SDL/main.c but I can't see any references to score - are you sure you've uploaded the most up-to-date version of main.c?

Mark Bishop
--
Student and freelance OS X & iOS developer
Quote this message in a reply
Member
Posts: 24
Joined: 2011.06
Post: #29
I uploaded the file again. Sorry about that! Hope that helps!
Quote this message in a reply
Moderator
Posts: 697
Joined: 2002.04
Post: #30
Code:
void score (){
message = TTF_RenderText_Solid(font,"Score" + playerScore, textColor );
}

C (and C++ and Objective-C etc.) are not eg. Java; you can't just add two strings or a string and a value together and receive a string combining the two strings or the string and the value as a result – you must concatenate the two strings or convert the value to a string and then concatenate the two strings, both of which you can do using the sprintf() function; your code compiles because it is adding the address of the string "Score" to the value playerScore, eg.

Code:
#include <stdio.h>

int main( void )
{
    printf( "%s\n", "Mark" + 2 );
    // The above will print "rk"

    printf( "%s\n", "Mark" + 1983 );
    // The above will print random memory until it encounters a terminator ('\0') character.

    return 0;
}

Your code should probably look like:

Code:
void score( void )
{
    char *str = ( char* )malloc( sizeof( char ) * ( strlen( "Score " ) + ( sizeof( playerScore ) * 8 ) + 1 ));
    sprintf( str, "Score %d", playerScore );
    message = TTF_RenderText_Solid( font, str, textColor );
    free( str );
    return;
}

Mark Bishop
--
Student and freelance OS X & iOS developer
Quote this message in a reply
Post Reply