You are not logged in.

#1 2012-05-24 17:17:49

Allok
Member
Registered: 2012-04-30
Posts: 20

Problem with showing a picture by SDL

Hello, everyone!

I've got a following code:

#include <iostream>
#include <stdlib.h>
#include "SDL/SDL.h"

using namespace std;

SDL_Surface* load_image(string file)
{
    SDL_Surface *li, *oi;
    li = SDL_LoadBMP(file.c_str());
    if (li != NULL)
    {
        oi = SDL_DisplayFormat(li);
        SDL_FreeSurface(li);
    }
    else
    {
        cerr << "Error of loading  \"" << file << "\"" << endl;
        return NULL;
    }
    return oi;
}

void apply_surface(int x, int y, SDL_Surface *s, SDL_Surface *d)
{
    SDL_Rect pos;
    pos.x = x;
    pos.y = y;
    SDL_BlitSurface(s, NULL, d, &pos);
}

int main(int argc, char **argv)
{
    int SCREEN_WIDTH = 640, SCREEN_HEIGHT = 480, SCREEN_BPP = 32;
    if (argc > 1)
    {
        SCREEN_WIDTH = atoi(argv[1]);
    }
    if (argc > 2)
    {
        SCREEN_HEIGHT = atoi(argv[2]);
    }
    if (argc > 3)
    {
        SCREEN_BPP = atoi(argv[3]);
    }
    //Start SDL
    SDL_Init(SDL_INIT_EVERYTHING);

    SDL_Surface *screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE);
    SDL_WM_SetCaption("Draft", NULL);
    SDL_Surface *pict = load_image("hello.bmp");
    apply_surface(10, 10, pict, screen);

    SDL_Flip(screen);
    SDL_Delay(3000);
    SDL_FreeSurface(pict);

    //Quit SDL
    SDL_Quit();
    return 0;
}

It compiles good, links as well, but the problem starts with imaging hello.bmp. Only some parts of it are shown and the others are black (as the whole background). Anyhow it works properly on other computers with other OSs. Could anyone give me a hand and explain where is the problem hiding?

Thanks in advance.

Offline

#2 2012-05-25 14:34:27

Allok
Member
Registered: 2012-04-30
Posts: 20

Re: Problem with showing a picture by SDL

Nobody knows??? Nobody has the same trouble?

Offline

#3 2012-05-25 16:14:38

drcouzelis
Member
From: Connecticut, USA
Registered: 2009-11-09
Posts: 4,092
Website

Re: Problem with showing a picture by SDL

I haven't tested it because I'm not home, but this part looks strange:

SDL_Flip(screen);
SDL_Delay(3000);
SDL_FreeSurface(pict);

I've never seen screen drawing like this before. Instead, it's always in a loop. Here's my guess as to why it doesn't look correct:

You try to update the screen (SDL_Flip). Before the hardware finishes updating you tell the thread to pause (SDL_Delay). As soon as it's done pausing, the screen is destroyed and the application quits.

Do other sample SDL applications show the same incorrect behaviour as your code?

I'll try your code on Arch Linux tonight, if I remember. tongue

Offline

#4 2012-05-25 16:26:27

jac
Member
From: /home/jac
Registered: 2009-05-19
Posts: 431
Website

Re: Problem with showing a picture by SDL

It works fine here. Can you upload the specific bmp you're using somewhere?

Edit: Nothing looks wrong to me in the source. Though, why are you including stdlib? Also, since the SDL header should be in the system header location, you should use angle brackets for it.

Last edited by jac (2012-05-25 16:35:04)

Offline

#5 2012-05-25 16:46:20

Allok
Member
Registered: 2012-04-30
Posts: 20

Re: Problem with showing a picture by SDL

drcouzelis,
The following program works properly:

#include <iostream>
#include "SDL/SDL.h"

using namespace std;

void SLock(SDL_Surface *screen)
{
    if (SDL_MUSTLOCK(screen))
    {
	SDL_LockSurface(screen);
    }
}

void SUnlock(SDL_Surface *screen)
{
    if (SDL_MUSTLOCK(screen))
    {
	SDL_UnlockSurface(screen);
    }
}

void DrawPixel(SDL_Surface *screen, int x, int y, Uint8 R, Uint8 G, Uint8 B)
{
    Uint32 color = SDL_MapRGB(screen->format, R, G, B);
    switch(screen->format->BytesPerPixel)
    {
	case 1:
	{
	    printf("1\n");
	    Uint8 *new_c;
	    new_c = (Uint8 *) screen->pixels + y * screen->pitch + x;
	    *new_c = color;
	}break;
	case 2:
	{
	    printf("2\n");
	    Uint16 *new_c;
	    new_c = (Uint16 *) screen->pixels + y * screen->pitch/2 + x;
	    *new_c = color;
	}break;
	case 3:
	{
	    printf("3\n");
	    Uint8 *new_c; 
	    new_c = (Uint8 *)screen->pixels + y*screen->pitch + x * 3; 
	    if(SDL_BYTEORDER == SDL_LIL_ENDIAN)
	    {
		new_c[0] = color; 
		new_c[1] = color >> 8; 
		new_c[2] = color >> 16; 
	    }
	    else
	    {
		new_c[2] = color; 
		new_c[1] = color >> 8; 
		new_c[0] = color >> 16;
	    }
	}break;
	case 4:
	{
	    //printf("4\n");
	    Uint32 *new_c;
	    new_c = (Uint32 *) screen->pixels + y * screen->pitch/4 + x;
	    *new_c = color;
	}break;
    }
}

void DrawScene(SDL_Surface *screen)
{
    SLock(screen);
    for (int i = 0; i < 640; i++)
    {
	for (int j = 0; j < 480; j++)
	{
	    DrawPixel(screen, i, j, j / 2, j / 2, (i + 0) / 3);
	}
    }
    SUnlock(screen);
    SDL_Flip(screen);
}

int main()
{
    if (SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0)
    {
	printf("I`m sorry. I can`t initialize SDL\n %s\n", SDL_GetError());
	exit(-1);
    }
    SDL_Surface *screen;
    screen = SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE|SDL_DOUBLEBUF);
    if (screen == NULL)
    {
	printf("Can`t create a window 640x480\n");
	exit(-2);
    }
    SDL_WM_SetCaption("My first expereince with SDL", "");
    bool done = 0;
    while (done == 0)
    {
	SDL_Event ev;
	while (SDL_PollEvent(&ev))
	{
	    if (ev.type == SDL_QUIT)
	    {
		done = 0;
	    }
	    if (ev.type == SDL_KEYDOWN)
	    {
		if (ev.key.keysym.sym == SDLK_ESCAPE)
		{
		    done = 1;
		}
	    }
	}
	DrawScene(screen);
    }
    atexit(SDL_Quit);
    return 0;
}

But it doesn't use a BMP file. Anyhow, I've got this code from here

I would appreciate if you don't forget wink

Offline

#6 2012-05-25 17:10:53

Allok
Member
Registered: 2012-04-30
Posts: 20

Re: Problem with showing a picture by SDL

jac,
I don't know where to upload BMP file. Only that

I agree, there's no sense in stdlib.h.

Offline

#7 2012-05-25 17:35:10

drcouzelis
Member
From: Connecticut, USA
Registered: 2009-11-09
Posts: 4,092
Website

Re: Problem with showing a picture by SDL

Ok. What IDE and compiler are you using?

Offline

#8 2012-05-25 17:45:29

jac
Member
From: /home/jac
Registered: 2009-05-19
Posts: 431
Website

Re: Problem with showing a picture by SDL

Allok, you should try http://ompldr.org/. It is fairly popular here on the boards and hopefully won't modify the image. The other image you linked is a png wink

Offline

#9 2012-05-25 18:43:53

Allok
Member
Registered: 2012-04-30
Posts: 20

Re: Problem with showing a picture by SDL

drcouzelis,
IDE - console
compiler - g++ (GCC) 4.7.0 20120505

Offline

#10 2012-05-25 18:45:06

Allok
Member
Registered: 2012-04-30
Posts: 20

Re: Problem with showing a picture by SDL

jac,

Ok'ey. Here it is

Offline

#11 2012-05-26 00:08:07

drcouzelis
Member
From: Connecticut, USA
Registered: 2009-11-09
Posts: 4,092
Website

Re: Problem with showing a picture by SDL

It works and looks fine on my computer.

My recommendation is to ignore it and continue learning and programming SDL. You'll probably never have a finished application that draws to the screen like this anyway. tongue

Offline

#12 2012-05-26 17:12:27

Allok
Member
Registered: 2012-04-30
Posts: 20

Re: Problem with showing a picture by SDL

drcouzelis,

I've replaced the function SDL_LoadBMP by IMG_Load. But it doesn't change anything. If it's not the true way,  what way did you mean, when you said about strange realisation of showing images?

Offline

#13 2012-05-27 12:25:34

jac
Member
From: /home/jac
Registered: 2009-05-19
Posts: 431
Website

Re: Problem with showing a picture by SDL

Even with the image it seems to be working fine. I'm going to have to say it's probably a weird hardware issue? I think drcouzelis was just suggesting you ignore the error. Once you move on, it may go away simply because you'll be rendering in a loop.

Though, here are some things you could try: Change the screen to use SDL_HWSURFACE instead of SDL_SWSURFACE. Also check out SDL_DOUBLEBUF. Actually, try playing around with the other flags too: a list of flags. To pass multiple flags, you use logical or. Example:

SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_HWSURFACE | SDL_DOUBLEBUF);

Offline

#14 2012-05-28 16:54:29

Allok
Member
Registered: 2012-04-30
Posts: 20

Re: Problem with showing a picture by SDL

Well, ok. Thanks everybody for replying. I'll try to move onin learning SDL. It's believed that it's just a casual error.

PS. Jac, it doesn't help)

Offline

Board footer

Powered by FluxBB