Sign in to follow this  
CodeTitan

Problems with Sprite class

Recommended Posts

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;
}

[/i]

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!

Share this post


Link to post
Share on other sites
while(isRunning == true)
{
SDL_PollEvent(&event);

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

}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this