Problems with Sprite class

Started by
4 comments, last by Gaiiden 19 years, 6 months ago
I have written a sprite class and tried to test it. The code compiles, but it does not display anything. I have no idea where the problem lies in my code. Anyway, here is my code: sdlmain.cpp

#include <stdio.h>
#include <stdlib.h>
#include <sdl.h>
#include "Sprite.h"

int main(int argc, char **argv)
{
    //local variables
	SDL_Surface* screen;
    bool isRunning;
    SDL_Event event;

    SDL_Init(SDL_INIT_VIDEO);

	SDL_WM_SetCaption("Dreiver", 0);

    atexit(SDL_Quit);

    screen = SDL_SetVideoMode(800, 600, 0, SDL_ANYFORMAT);

    isRunning = true;

	Sprite* test = new Sprite(0, 100, 100, "test");
	test->source.x = 0;
	test->source.y = 0;
	test->source.w = 100;
	test->source.h = 100;
	SDL_Rect dest;
	dest.x = 0;
	dest.y = 0;
	dest.w = 100;
	dest.h = 100;
	


    while(isRunning == true)
    {
        SDL_PollEvent(&event);

        if (event.type == SDL_QUIT)
            isRunning = false;
		SDL_BlitSurface(test->image, &test->source , screen, &dest); 

    }

    return 0;
}

Sprite.h

#pragma once
#include <stdlib.h>
#include <stdio.h>
#include <sdl.h>

class Vector
{
public:
	int x, y;
};


class Sprite
{
public:
	int flags;
	SDL_Surface* image;
	Vector position;
	Vector velocity;
	Vector size;
	SDL_Rect source;

	Sprite(int flags, int sx, int sy, char* filename);
	~Sprite();

	void set_pos(int px, int py);

	void set_vel(int vx, int vy);

	void process(void);

	bool collision(Sprite *spr);
};

Sprite::Sprite(int f, int sx, int sy, char* filename)
{
	flags  = f;
	image = SDL_LoadBMP(filename);
	position.x  = 0;
	position.y  = 0;
	velocity.x  = 0;
	velocity.y  = 0;
	size.x = sx;
	size.y = sy;
	source.x = position.x;
	source.y = position.y;
	source.w = size.x;
	source.h = size.y;
}

void Sprite::set_pos(int px, int py)
{
	position.x = px;
	position.y = py;
}
void Sprite::set_vel(int vx, int vy)
{
	velocity.x = vx;
	velocity.y = vy;
}
void Sprite::process(void)
{
	position.x += velocity.x;
	position.y += velocity.y;
}
Sprite::~Sprite()
{
	SDL_FreeSurface(image);
}

Thanks in advance for your help!
Advertisement
while(isRunning == true)
{
SDL_PollEvent(&event);

if (event.type == SDL_QUIT)
isRunning = false;
SDL_BlitSurface(test->image, &test->source , screen, &dest);
SDL_Flip(screen);

}
No go. I entered that code. I have a feeling that it's something wrong with my size-related members of the Sprite class, but I can't find it.
Well, you ARE going to need an SDL_Flip call at the end of the drawing or else nothing at all will show up whether your code is valid or not.
while(isRunning){        while(SDL_PollEvent(&event)        {            if (event.type == SDL_QUIT)               isRunning = false;        }    SDL_BlitSurface(test->image, &test->source , screen, &dest);   SDL_Flip(screen);}


first, while(isRunning) is the same as while(isRunning == true), since isRunning is a boolean variable, it will evaluate to a boolean expression (as if there was a comparison there). so the '== true' is not needed.

second, you need to poll for events in a seperate loop, not in the same loop as the game. what happends if there is more then one event? in fact, you dont even check if SDL_PollEvent() returns true.

last, like EG said, you need to flip the screen. its hard to debug someone else's code, but:

make sure the image is loading. i believe that the loadimage call will return a NULL pointer. check for that.

secondly, try changing the blitsurface call to this:
SDL_BlitSurface(test->image,NULL, screen, &dest);

its been a while since i used SDL for rendering, but, why do you want to only draw a piece of your image, anyway?
FTA, my 2D futuristic action MMORPG
Quote:Original post by graveyard filla
its been a while since i used SDL for rendering, but, why do you want to only draw a piece of your image, anyway?

Because he's making a sprite class using templated images, no doubt [smile]

Besides that, I agree with your solution.

Drew Sikora
Executive Producer
GameDev.net

This topic is closed to new replies.

Advertisement