Jump to content
  • Advertisement
Sign in to follow this  
Kristen

SDL images handle

This topic is 4344 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

The problem is this: I'd been testing some thing on SDL, but I can't make that a simple bmp file loads on the console window, the code is the next one
#ifdef WIN32
#pragma comment(lib, "SDL.lib")
#pragma comment(lib, "SDLmain.lib")
#endif

#include <SDL.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>

#define SCREEN_WIDTH 640     //Defines that make it easy for us to change the screen size without even changing the functions.
#define SCREEN_HEIGHT 480
#define COLORKEY 255, 0, 255 //The colorkey that will not be included in sprites when they are blitted onto the screen.
#define endl "\n"			 //Used later for making line breaks when reporting errors. 

void EstablecerVideo();
void PintarPixel(SDL_Surface *screen, Uint8 R, Uint8 G, Uint8 B);
bool iniSDL();
void mainloop();
void drawimage(SDL_Surface *srcimg, int sx, int sy, int sw, int sh, SDL_Surface *dstimg, int dx, int dy, int alpha);
SDL_Surface* CargarBMP(char *file);

SDL_Surface *screen;
SDL_Surface *kristen;

int main(int argc, char **argv)
{
	if ( SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0 ) 
	{
        fprintf(stderr, "No se puede iniciar SDL: %s\n", SDL_GetError());
        exit(1);
    }
	atexit(SDL_Quit);
    screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 8, SDL_SWSURFACE);
    if ( screen == NULL ) 
    {
        fprintf(stderr, "No se puede establecer el modo de video 640x480: %s\n", SDL_GetError());
		getchar();
        exit(1);
    }
	kristen = CargarBMP("art/kristen.bmp");
	if (kristen == NULL) 
	{
		printf("No se cargo ni madres");
		getchar();
		return true;
	}           
	drawimage(kristen, 10, 10, SCREEN_WIDTH, SCREEN_HEIGHT, screen, 0, 0, 255);
	getchar();
	return false;
}
bool iniSDL()
{
		if ( SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0 ) 
	{
        fprintf(stderr, "No se puede iniciar SDL: %s\n", SDL_GetError());
        exit(1);
    }
	atexit(SDL_Quit);

    screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 32, SDL_SWSURFACE);

    if ( screen == NULL ) 
    {
        fprintf(stderr, "No se puede establecer el modo                 de video 640x480: %s\n", SDL_GetError());
		getchar();
        exit(1);
    }

	kristen = CargarBMP("art/kristen.bmp");
	if (kristen == NULL) 
	{
		printf("No se cargo ni madres");
		getchar();
		return true;     
	}           
	printf("Se cargo correctamente");
	getchar();
	return false;
}
SDL_Surface* CargarBMP(char *file)
{
  SDL_Surface *tmp;                        
  tmp = SDL_LoadBMP(file);             //Load the requested BMP into the temporary surface.

  if (tmp == NULL) {                   //If the loading failed, then report the error and return null so the code that called this can deal with the error.
    printf("Error: File could not be opened:", file, SDL_GetError(), "\n");
    return NULL;
  } 
  else {                               //Set the colorkey for the surface we are loading so we can have transparent pixels where we want to.
    if(SDL_SetColorKey(tmp, SDL_SRCCOLORKEY | SDL_RLEACCEL, SDL_MapRGB(tmp->format, COLORKEY)) == -1)
    printf("Warning: Colorkey will not be used, reason: \n", SDL_GetError(), "\n");
  }
  return tmp;
}



void drawimage(SDL_Surface *srcimg, int sx, int sy, int sw, int sh, SDL_Surface *dstimg, int dx, int dy, int alpha = 255)
{
  if ((!srcimg) || (alpha == 0)) return; //If there's no image, or its 100% transparent.
  SDL_Rect src, dst;                     //The two rectangles are filled with the information passed to the function. 
  
  src.x = sx;  src.y = sy;  src.w = sw;  src.h = sh;
  dst.x = dx;  dst.y = dy;  dst.w = src.w;  dst.h = src.h;
  if (alpha != 255) SDL_SetAlpha(srcimg, SDL_SRCALPHA, alpha); //Make SDL aware of the desired level of Alpha transparency in the source image.
  SDL_BlitSurface(srcimg, &src, dstimg, &dst);                 //Finally Blit the source on to the destination surface.
}

As you can see, I load the image into a file with the name of Kristen, and send it to a function wich as I can see, has to load it into the screen, but it doesn't do the trick, and i can't figure out why isn't loading

Share this post


Link to post
Share on other sites
Advertisement
What exactly isn't working? You have to say what you see and what kind of output you get. Anyway, the picture wouldn't appear on the console window, it would appear on a separate window. Try SDL_Flip or SDL_UpdateRect.

Share this post


Link to post
Share on other sites
You've blitted the image, but you havent updated the screen. After you call SDL_BlitSurface, make a call to SDL_UpdateRect. Note that you'll need to call
SDL_Flip instead of SDL_UpdateRect if you ever implement double buffering.

One other thing: you may want to implement an event handler instead of relying on getchar().

Share this post


Link to post
Share on other sites
Wooojoooooo, finally, thank you very much

Seems like I didn't make myself clear, but you got where i was having trouble.

Really, thank you very much, hope i can count on you next time I have trouble, but 'll make sure not being too soon

Next step, make an animation on the console, xDDDDDD

Really, thanks!!!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!