SDL_Rect

I’m doing a tetris clone with SDL. Here’s one function:

void putBlock(int x, int y, int color)
{
SDL_Rect *blah;
SDL_Rect *dest;
SDL_Rect *source;

source->w = 20;
source->h = 20;
source->y = 0;
source->x = color * 20;

dest->x = x*20;
dest->y = y*20;

SDL_BlitSurface(blocks, source, screen, dest);

}

It works fine, but if I change the first line to:
//SDL_Rect *blah;
the program crashes with:
Fatal signal: Segmentation Fault (SDL Parachute Deployed)

My compiler: \mingw\bin\g++ --version
2.95.3-5

What’s wrong?

You’re not actually initialising any memory for your rectangles. I’m
surprised it works at all. Try this:

SDL_Rect dest;
SDL_Rect source;

source.w = 20;
source.h = 20;
source.y = 0;
source.x = color * 20;

dest.x = x20;
dest.y = y
20;

SDL_BlitSurface(blocks, &source, screen, &dest);> ----- Original Message -----

From: mstrandm@cc.helsinki.fi (Markku Strandman)
To:
Sent: Monday, November 19, 2001 7:25 AM
Subject: [SDL] SDL_Rect

I’m doing a tetris clone with SDL. Here’s one function:

void putBlock(int x, int y, int color)
{
SDL_Rect *blah;
SDL_Rect *dest;
SDL_Rect *source;

source->w = 20;
source->h = 20;
source->y = 0;
source->x = color * 20;

dest->x = x20;
dest->y = y
20;

SDL_BlitSurface(blocks, source, screen, dest);
}

It works fine, but if I change the first line to:
//SDL_Rect *blah;
the program crashes with:
Fatal signal: Segmentation Fault (SDL Parachute Deployed)

My compiler: \mingw\bin\g++ --version
2.95.3-5

What’s wrong?


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

You’ve created pointers, but they dont point anywhere, so when you
try to write into them (with “->”), it segfaults. :slight_smile:

You can, if you want to do it a round-about way, use “malloc” to create
them. But, it just makes more sense to create actual SDL_Rect’s!

SDL_Rect dest, source;

dest.x = 0;
dest.y = 0;

(btw, you seemed to forget to set the actual dest’s “w” and “h” values!)

Then, when you call SDL_BlitSurface(), send “&dest” and “&src”

GOod luck!

-bill!On Sun, Nov 18, 2001 at 10:25:55PM +0200, Markku Strandman wrote:

SDL_Rect *blah;
SDL_Rect *dest;
SDL_Rect *source;

source->w = 20;
source->h = 20;
source->y = 0;
source->x = color * 20;

Le Dimanche 18 Novembre 2001 21:25, vous avez ?crit :

I’m doing a tetris clone with SDL. Here’s one function:

void putBlock(int x, int y, int color)
{
SDL_Rect *blah;
SDL_Rect *dest;
SDL_Rect *source;

Here, you forget to new/malloc your dest and source.

source->w = 20;
source->h = 20;
source->y = 0;
source->x = color * 20;

dest->x = x20;
dest->y = y
20;

SDL_BlitSurface(blocks, source, screen, dest);
}

It works fine,
Works, but shoudn’t :)–
Allergy
http://www.alrj.org

William Kendrick wrote:

(btw, you seemed to forget to set the actual dest’s “w” and “h” values!)

Bill, read the manual :slight_smile:

Hi !

The program below stop working when I set the X coordinates at something more
than 100. X coordinate 100 works, but not 101 !
My immage is 600 X 450 (W X H).

If I make the same test but with SDL_SetVideoMode(1024, 768, 0, 0), it works.
Is anyone have an idea of what can be the problem ?

Thanks !

Talie

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <stdio.h>
#include <stdlib.h>
#include
#include
using namespace std;

SDL_Surface *screen;
SDL_Surface *image;
SDL_Rect src, dest;
int X, Y;

main () {

X = 101;
Y = 0;

SDL_Init(SDL_INIT_VIDEO);
screen = SDL_SetVideoMode(800, 600, 0, 0);
image = IMG_Load (“de-60001.jpg”); // Image 600 X 450

src.x = 0; src.y = 0; src.w = image->w; src.h = image->h;
dest.x = X; dest.y = Y; dest.w = image->w; dest.h = image->h;

SDL_BlitSurface (image, &src, screen, &dest);
SDL_UpdateRect (screen, X, Y, X+src.w, Y+src.h);
SDL_FreeSurface (image);
SDL_Delay (5000);
}
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

? SDL_UpdateRect (screen, X, Y, X+src.w, Y+src.h);
should be
SDL_UpdateRect(screen,X,Y,dest.w,dest.h);

because SDL_UpdateRect does that addition for you internally. SDL_UpdateRect
won’t work if any of the rectangle is offscreen.

You can also change

SDL_BlitSurface (image, &src, screen, &dest);
to SDL_BlitSurface (image, NULL, screen, &dest);
then get rid of src entirely; SDL defaults to the entire source surface when
no area is given.

You should also call SDL_Quit() just before main returns, i.e. below
SDL_Delay(5000).On Sunday 08 May 2005 07:31, talie wrote: