[solved] SDL Tutorial (Coordinates & Blitting) fails to yield expected results.

What (immediately) follows are background details that may or may not be relevant.

Ctrl+F <TheMeat> if you want to get to the meat of my problem. I say this to save you time. You are all people with schedules, I understand.

I started off yesterday before work doing this SDL tutorial:

http://www.sdltutorials.com/sdl-tutorial-basics

I followed it line-for-line, stopping to think about what it was I was doing, looking into the SDL headers to see the contexts of various functions I had never used before, etc. etc.

In short, I took it slow. This first tutorial did not yield the expected results. At first, I thought it was because I might've installed SDL wrong.

LOW PRIORITY SIDE QUEST(ION): I found a file in SDL's lib directory that seemed to have 2 file extensions instead of just one (LibSDL.dll.a), but apparently this file is standard-issue with everyone else's SDL library. Not sure why it has two file extensions (or rather, appears to), but I figured it was beyond the scope of my current knowledge. However, if someone could elaborate why this is, that'd be peachy.</lowpriority>

Once I realized it wasn't LibSDL.dll.a that was causing my problems, I diddled around a bit more until I discovered that I used SDL_QUIT (which is a pointer to SDL_Quit, I heard one person say in the comments, or somewhere else, correct me if I'm wrong) where I should've used SDL_Quit, and vice-versa. I also used SDL_FLip at one point, and was absolutely baffled that it took me an hour to realize that the 'L' was improperly capitalized. That's what Stroustrup calls a "silly" or "low on sleep" error.

I eventually get it all to run. My chosen image file is rendered, albeit with an extra console window accompanying my surface window (my compiler is code::blocks). Yippie.

The image window and its pretty little console escort stays up until I personally choose to close it. I said "Let there be image" and there was image, and I saw that it was good. Same old story I'm sure you all went through at some point.


Day 2: <TheMeat>

http://www.sdltutorials.com/sdl-coordinates-and-blitting

Now I'm doing this tutorial. I follow it perfectly, as far as I can tell, I write a second interface/declaration for CSurface::OnDraw in CSurface.cpp, and a second implementation/definition as well. I check my scope delimiters, my semicolons, my spelling, my capitalization, my function names, my file names, etc. etc. etc.

I even save the tileset image (A PNG on the site) as a .bmp image. For the first tutorial I just used the standard code::blocks logo that SDL gives me in my project directory when I use a template. That was successful. I overwrite that "cb.bmp" (which I use for the string arg in the CApp_OnInit, by the way, my only deviation from the tutorial is my chosen image name)

Everything seems to be following the tutorial 100%. However, when I build at the end of this tutorial, instead of showing me the cropped tileset, I just get a console window, and a very briefly-flashing surface window that, as far as I can tell from the millisecond(s) I get to see it, does not have an image in it.

What I've written so far:

CApp_OnInit.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "CApp.h"

bool CApp::OnInit()
{
    if(SDL_Init(SDL_INIT_EVERYTHING) < 0)
    {
    return false;
    }

    if((Surf_Display = SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE | SDL_DOUBLEBUF)) ==NULL)
    {
        return false;
    }

    if((Surf_Test = CSurface::OnLoad("cb.bmp")) == NULL)
    {
        return false;
    }

    return true;
}


CApp_OnEvent.cpp
1
2
3
4
5
6
7
8
9
#include "CApp.h"

void CApp::OnEvent(SDL_Event* Event)
{
    if(Event->type == SDL_QUIT)
    {
        Running = false;
    }
}


CApp_OnLoop.cpp
1
2
3
4
5
6
#include "CApp.h"

void CApp::OnLoop()
{

}


CApp_OnRender.cpp
1
2
3
4
5
6
7
8
9
#include "CApp.h"

void CApp::OnRender()
{
    CSurface::OnDraw(Surf_Display, Surf_Test, 0, 0);
    CSurface::OnDraw(Surf_Display, Surf_Test, 100, 100, 0, 0, 50, 50);

    SDL_Flip(Surf_Display);
}


CApp_OnCleanup.cpp
1
2
3
4
5
6
7
8
#include "CApp.h"

void CApp::OnCleanup()
{
    SDL_FreeSurface(Surf_Test);
    SDL_FreeSurface(Surf_Display);
    SDL_Quit();
}


CApp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include "CApp.h"

CApp::CApp() //constructor def
{
    Surf_Test = NULL;
    Surf_Display = NULL;

    Running = true;
}

int CApp::OnExecute()
{
    if(OnInit() == false)
    {
        return -1;
    }

    SDL_Event Event;

    while(Running)
    {
        while(SDL_PollEvent(&Event))
        {
            OnEvent(&Event);
        }

        OnLoop();
        OnRender();
    }

    OnCleanup();
    return 0;
}

int main(int argc, char* argv[])
{
    CApp theApp;
    return theApp.OnExecute();
}


CSurface.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include "CSurface.h"

CSurface::CSurface()
{

}

SDL_Surface* CSurface::OnLoad(char* File)
{
    SDL_Surface* Surf_Temp = NULL;
    SDL_Surface* Surf_Return = NULL;

    if((Surf_Temp = SDL_LoadBMP(File)) == NULL)
    {
        return NULL;
    }

    Surf_Return = SDL_DisplayFormat(Surf_Temp);
    SDL_FreeSurface(Surf_Temp);

    return Surf_Return;
}

bool CSurface::OnDraw(SDL_Surface* Surf_Dest, SDL_Surface* Surf_Src, int X, int Y)
{
    if(Surf_Dest == NULL || Surf_Src == NULL)
    {
        return false;
    }

    SDL_Rect DestR;

    DestR.x = X;
    DestR.y = Y;

    SDL_BlitSurface(Surf_Src, NULL, Surf_Dest, &DestR);

    return true;


}

bool CSurface::OnDraw(SDL_Surface* Surf_Dest, SDL_Surface* Surf_Src, int X, int Y, int X2, int Y2, int W, int H)
{
    if(Surf_Dest == NULL || Surf_Src == NULL)
    {
        return false;
    }

    SDL_Rect DestR;

    DestR.x = X;
    DestR.y = Y;

    SDL_Rect SrcR;

    SrcR.x = X2;
    SrcR.y = Y2;
    SrcR.w = W;
    SrcR.h = H;

    SDL_BlitSurface(Surf_Src, &SrcR, Surf_Dest, &DestR);

    return true;
}


CApp.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#ifndef _CAPP_H_
    #define _CAPP_H_

#include <SDL.h>

#include "CSurface.h"

class CApp
{
    private:
        bool Running;
        SDL_Surface* Surf_Display;
        SDL_Surface* Surf_Test;

    public:
        CApp();
        int OnExecute();

    public:
        bool OnInit();
        void OnEvent(SDL_Event* Event);
        void OnLoop();
        void OnRender();
        void OnCleanup();
};

#endif 


CSurface.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef _CSURFACE_H_
    #define _CSURFACE_H_

#include <SDL.h>

class CSurface
{
    public:
        CSurface();

    public:
        static SDL_Surface* OnLoad(char* File);

        static bool OnDraw(SDL_Surface* Surf_Dest, SDL_Surface* Surf_Src, int X, int Y);

        static bool OnDraw(SDL_Surface* Surf_Dest, SDL_Surface* Surf_Src, int X, int Y, int X2, int Y2, int W, int H);
};

#endif 


Thanks in advance for your boundless patience and concern.
Last edited on
Sorry if this is considered annoying on this forum, but...

"Bump."
LibSDL.dll.a

The extension is .a.

The .dll part is to let you know it is an import library for the SDL dll and not a static library.
Look and see what the return code in the console window is. If it's -1, you know that an error happened in the function OnInit(). If it's zero, then something happened afterwards.

Always good to narrow it down a bit.
The extension is .a.

The .dll part is to let you know it is an import library for the SDL dll and not a static library.


Thanks, Vin.

Always good to narrow it down a bit.


Sorry, I completely forgot to mention. The process returns 0. It's probably a runtime error.
If it is returning 0, then something must be setting Running back to false otherwise it would never make it out of your while loop as far as I can tell. The only thing that is set to change Running to false is when SDL gets SDL_QUIT event, so try to comment that out and see if it runs fine then.
I tried commenting out both SDL_Quit/QUIT events, one at a time, running = false; statements, and various statements that modified the surface. None of it changed the behavior of the execution in any way except minor fluctuations in execution time.

EDIT: Solved part of my problem!

Even though cb.bmp was saved as a bmp off the website, when I opened it in MS paint and tried to save it again, it had a png file extension, not on top of its bmp, it just had it for seemingly no reason.

I might've converted it by simply backspacing over the file extension instead of opening it and resaving it. Now the image shows up in the program, but isn't being cropped properly. At this point it'll just be a matter of messing with parameters of OnDraw.

EDIT 2: Sorry for the wild goose chase, guys. Took care of it. Marked as solved.
Last edited on
Topic archived. No new replies allowed.