Tille's SiteThis is the source code for the exercise from the third chapter.
/* Program name: testscreen5.c */
/* SDL screen test display image */
# include <SDL.h>
# include <stdlib.h>
# include <unistd.h>
/* Used for the endian-dependent 24 bpp mode in DrawPixel. */
# include <SDL_endian.h>
/* Loading an image. */
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(file);
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;
SDL_BlitSurface(image, NULL, screen, &dest);
/* Update the changed portion of the screen */
SDL_UpdateRects(screen, 1, &dest);
SDL_FreeSurface(image);
}
/* End loading an image. */
/* Drawing a pixel. */
void DrawPixel(SDL_Surface *screen, int x, int y,
Uint8 R, Uint8 G,
Uint8 B)
{
/* So as the program doesn't segfault when drawing something outside the screen.*/
if (x>=640) return;
if (y>=480) return;
Uint32 color = SDL_MapRGB(screen->format, R, G, B);
if ( SDL_MUSTLOCK(screen) ) {
if ( SDL_LockSurface(screen) < 0 ) {
return;
}
}
switch (screen->format->BytesPerPixel) {
case 1: { /* Assuming 8-bpp */
Uint8 *bufp;
bufp = (Uint8 *)screen->pixels + y*screen->pitch + x;
*bufp = color;
}
break;
case 2: { /* Probably 15-bpp or 16-bpp */
Uint16 *bufp;
bufp = (Uint16 *)screen->pixels + y*screen->pitch/2 + x;
*bufp = color;
}
break;
case 3: { /* Slow 24-bpp mode, usually not used */
Uint8 *bufp;
bufp = (Uint8 *)screen->pixels + y*screen->pitch + x * 3;
if(SDL_BYTEORDER == SDL_LIL_ENDIAN) {
bufp[0] = color;
bufp[1] = color >> 8;
bufp[2] = color >> 16;
} else {
bufp[2] = color;
bufp[1] = color >> 8;
bufp[0] = color >> 16;
}
}
break;
case 4: { /* Probably 32-bpp */
Uint32 *bufp;
bufp = (Uint32 *)screen->pixels + y*screen->pitch/4 + x;
*bufp = color;
}
break;
}
if ( SDL_MUSTLOCK(screen) ) {
SDL_UnlockSurface(screen);
}
SDL_UpdateRect(screen, x, y, 1, 1);
}
/* End draw pixel. */
int main(int argc, char *argv[])
{
if (argc != 2) {
printf("Number of arguments:%d\n", argc);
printf("Usage: testscreen5 <file>\n");
exit(1);
}
SDL_Surface *screen;
if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
exit(1);
}
atexit(SDL_Quit);
screen = SDL_SetVideoMode(640, 480, 16, SDL_SWSURFACE);
if ( screen == NULL ) {
fprintf(stderr, "Unable to set 640x480 video: %s\n", SDL_GetError());
exit(1);
}
printf("Set window of 640x480 at %d bits per pixel mode\n",
screen->format->BitsPerPixel);
ShowBMP(argv[1], screen, 0, 0);
int i;
int j=0;
for (i=80; i<560; i++) {
DrawPixel(screen, i, j, 255, 0, 0);
j++;
}
j=480;
for (i=80; i<560; i++) {
DrawPixel(screen, i, j, 255, 0, 0);
j--;
}
sleep(5);
printf("Quitting SDL.\n");
return(0);
}
And this is the makefile:
CC = gcc -Wall
BIN = testscreen5
CFLAGS=`sdl-config --cflags`
LDFLAGS=`sdl-config --libs`
all: $(BIN)
$(BIN): testscreen5.o
$(CC) testscreen5.o \
-o $(BIN) $(LDFLAGS)
clean:
@rm -f *.o
@rm $(BIN)
When you run the program, it should display the image specified on the command line and draw an X over it in red, and exit after a couple of seconds.
To the next chapter.
| Home |