This topic is now archived and is closed to further replies.


SDL and Images

Recommended Posts

Well, there is a little explanation in Linux game programming, but seems like it''s not working good, else with SDL, there is some doc in HTML, try to check it ^__^

Good luck, I''m also trying, but doesn''t work (compilation errors),


Life is a game, sometimes you win, sometimes you lose but in the end everyone loses

Share this post

Link to post
Share on other sites
I think you do it like you would do it on directx, you create a surface, you lock it, you read the image data from a file (pcx,gif,png,tiff,etc) with your own algorithm or using one of the libs to do so, plot the pixels on the surface, unlock the surface, and then you blit as you want, anyway if you decide not to use any helper and do it yourself, here is some code from the Book "Programming Linux Games" by Loki Software, hope this helps:

// Example of direct pixel access with SDL.

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

Uint16 CreateHicolorPixel(SDL_PixelFormat * fmt, Uint8 red, Uint8 green,
Uint8 blue)
Uint16 value;

// This series of bit shifts uses the information from the SDL_Format

// structure to correctly compose a 16-bit pixel value from 8-bit red,

// green, and blue data.

value = ((red >> fmt->Rloss) << fmt->Rshift) +
((green >> fmt->Gloss) << fmt->Gshift) +
((blue >> fmt->Bloss) << fmt->Bshift);

return value;

int main()
SDL_Surface *screen;
Uint16 *raw_pixels;
int x, y;

// Initialize SDL's video system and check for errors.

if (SDL_Init(SDL_INIT_VIDEO) != 0) {
printf("Unable to initialize SDL: %s\n", SDL_GetError());
return 1;

// Make sure SDL_Quit gets called when the program exits!


// Attempt to set a 256x256 hicolor (16-bit) video mode.

// This will set some type of 16-bit mode, but we won't

// know which particular pixel format ahead of time. If

// the video card can't handle hicolor modes, SDL will

// emulate it.

screen = SDL_SetVideoMode(256, 256, 16, 0);
if (screen == NULL) {
printf("Unable to set video mode: %s\n", SDL_GetError());
return 1;

// Video memory can be strange, and it's sometimes necessary to

// "lock" it before it can be modified. SDL abstracts this with

// the SDL_LockSurface function.


// Get a pointer to the video surface's memory.

raw_pixels = (Uint16 *) screen->pixels;

// We can now safely write to the video surface. We'll draw a nice

// gradient pattern by varying our red and blue components along

// the X and Y axes. Notice the formula used to calculate the

// offset into the framebuffer for each pixel.

// (The pitch is the number of bytes per scanline in memory.)

for (x = 0; x < 256; x++) {
for (y = 0; y < 256; y++) {
Uint16 pixel_color;
int offset;
pixel_color = CreateHicolorPixel(screen->format, x, 0, y);
offset = (screen->pitch / 2 * y + x);
raw_pixels[offset] = pixel_color;

// We're finished drawing, so unlock the surface.


// Inform SDL that the screen has been changed. This is necessary

// because SDL's screen surface is not always the real framebuffer;

// it is sometimes emulated behind the scenes.

SDL_UpdateRect(screen, 0, 0, 0, 0);

// Pause for a few seconds as the viewer gasps in awe.

return 0;

Edited by - kwizatz on August 30, 2001 11:06:58 AM

Share this post

Link to post
Share on other sites
Think very carefully before you answer that question

After careful deliberation, I have come to the conclusion that Nazrix is not cool. I am sorry for any inconvienience my previous mistake may have caused. We now return you to the original programming

Share this post

Link to post
Share on other sites