SDL sprite clipping problem

Started by
9 comments, last by way2lazy2care 12 years, 9 months ago
Hello,

What i did is the following:

I took the code to create a button from Lazy Foo's Lesson 9 Mouse Events. I then used that buttonclass in another tutorial project from SDLTutorials(tictactoe).
I tried to integrate the buttonclass in that project, but got some problems in doing so. it's driving me nuts :)

basicly when i try to compile what i got so far i receive : "\SDLTutorialBasics\CApp_OnRender.cpp `resetButton' undeclared (first use this function) " <=from Dev-C

resetButton is an instance of the buttonclass, this is the function where i use resetButton, its in CApp_OnRender.cpp : CSurface::OnDraw(Surf_Display, Surf_Button, 690, 455, &resetButton.a_rect_clips[ 1 ]);

Now what i dont understand is why i get this undeclared error, i made everything public in my button class, just to test.

If i use "CSurface::OnDraw(Surf_Display, Surf_Button, 690, 455,0,0,120 ,90);" to render the button sprite then its ok

Forgive me if the answer is something very simple, i'm an absolute beginner in c++.

I attached my project, think that will help
Advertisement
Are you using precompiled headers to declare functions, classes and structs? For each button.cpp, you should have a button.h

The headers should just contain function prototypes (single line declarations) and then the cpp files should contain block code.

ie

//in button.h
class button
{
public:
int this_function(int thisint);
};

//in button.cpp
int button::this_function(int thisint)
{
//do some stuff
return(anotherint);
}


Any questions just ask.
-A1P4A 0M3GA
Lead script writer on Scutum [http://www.gamedev.n...-entertainment/]
Team Member of [size=2]Forcas Entertainment
Amateur programmer with C++ and SDL knowledge
Game Enthusiast
I do use .h and .cpp files. I think maybe the problem is related to bad "includes"
for example, i got a " one undeclared" error when compiling this:

CApp_OnRender.cpp

#include "CApp.h"
void CApp::OnRender() {
double grades;
grades = one.obtuse_angle(); // one is an instance of triangle class
}

So actually my problem is that i dont know how to access an instance of an object from inside another class.
The function obtuse_angle(); is a public function inside the triangle class so that should be accessible.
The triangle class instance "one" is not known inside CApp_OnRender.cpp,
Even if i add (#include "triangle.h") i still receive the same error when compiling: CApp_OnRender.cpp: `one' undeclared (first use this function)
I also tried to add (#include "triangle.h") to CApp.h, same error when compiling

I can also say that the object instance "one" is created before i get inside CApp::OnRender()

This is the code from CApp.h

//==============================================================================
// SDL Tutorial 1
//==============================================================================
#ifndef _CAPP_H_
#define _CAPP_H_

#include <SDL/SDL.h>
#include "SDL/SDL_gfxPrimitives.h"

#include "CEvent.h"
#include "CSurface.h"
#include "SDL/SDL_ttf.h"

//==============================================================================
class CApp : public CEvent {
private:
bool Running;

SDL_Surface* Surf_Display;

SDL_Surface* Surf_Test;

SDL_Surface* Surf_Message;

//SDL_Surface* Surf_Message2;

TTF_Font* font ;

public:
CApp();

int OnExecute();

public:
bool OnInit();

void OnEvent(SDL_Event* Event);

void OnExit();

void OnLoop();

void OnRender();

void OnCleanup();
};

//==============================================================================

#endif



Well, what i tried now : i redeclared "one":

triangle one;

in CApp_OnRender.cpp and guess what, it worked.

Is this the way i should proceed, just redeclare an instance where you want to use it? thats if you're using it outside its own class
No, these instances will be independent of one another. If you changes the values somewhere, these changes will not be reflected elsewhere.

The correct way to handle this is to pass a copy or reference to the data to the function that requires it. Depending on the nature of the data, it can be preferable to pass the copy/reference to the constructor of the class instead.

A "quick fix" is to making the variable a global, by using the extern keyword in the declarations and giving it exactly one definition. This is a poor solution, because it doesn't scale well when you have more and more data that needs to be shared. Also, globals make it very difficult to reason about the state of the program.

No, these instances will be independent of one another. If you changes the values somewhere, these changes will not be reflected elsewhere.


Ok, so in fact what happens is that a copy of "one" is created, that copy got the same values as the existing (primary)version of "one", and what you mean is that if i change any value i will not change the value of the primary instance of "one". Ok so as long as you dont need to change anything, that's not really a problem.

A better solution is to pass a reference to the constructor of the class, can you give me a simple example how i can do that
No, no copy is created. You've created an independent triangle object with the same name. The member values are likely the same, unless you have a non-deterministic constructor (e.g. calling rand(), accessing the filesystem or the clock, etc). Remember that the names of the variables are irrelevant here, it just so happens you've given two variables in different scopes the same identifier.

To get a true copy, you need to pass the variable by value to the function or class involved.


A better solution is to pass a reference to the constructor of the class, can you give me a simple example how i can do that
[/quote]
You'd write a function like this:

// CApp would have a Triangle (value or instance) member named "one"
CApp::CApp(/* const ? */ Triangle &triangle)
:
one(triangle)
{
}

void CApp::OnRender()
{
double grades = one.obtuse_angle();
// ...
}

// Or

void CApp::OnRender(const Triangle &triangle)
{
double grades = one.obtuse_angle();
// ...
}

Which is correct depends heavily on what Triangle is supposed to do, and the overall design of your program. The design seems poor to begin with (an Application is an Event?), though this is probably related to the tutorial you are following.
call me stupid but i just can't figure it out:

To make it simple for myself i made the following model, i made 4 files. In
cars::carmax() i have "return loco.trainmax()", when i compile i get : `loco' undeclared (first use this function)
Can you modify my code with your reference methode so loco.trainmax() would be known inside cars::carmax()

That would really help me alot to learn to understand that stuff .

cars.h

class cars {

public:
cars();
int maxpassengerscar;
int carmax();
};


cars.cpp

#include "trains.h"
#include "cars.h"
#include <iostream>
using namespace std;
cars::cars() {
maxpassengerscar = 5;
}

int cars::carmax() {
return loco.trainmax()
}

int main() {

cars fiat;
trains loco;

cout << loco.trainmax(); // should cout 150
return 0;
}


trains.h

class trains {
public:
trains();
int maxpassengers;
int trainmax();
};


trains.cpp

#include "trains.h"
trains::trains() {
maxpassengers = 150;
}

int trains::trainmax() {
return maxpassengers;
}
You need to learn about declarations, and scope. A variable must be declared before the point it is referenced. A variable also exists in a certain scope.

In your program, "loco" is locally declared in main(). If you want to use its value elsewhere, you must pass it to these functions. So you would say:

int cars::carmax(const trains &loco) {
return loco.trainmax(); // trains::trainmax would need to be declared 'const'
}

However, in this case it doesn't make sense. Why is the maximum capacity of a car related to the maximum capacity of a particular train? Instead:

int cars::carmax() {
return maxpassengerscar;
}

However, in this case it doesn't make sense. Why is the maximum capacity of a car related to the maximum capacity of a particular train?


You're right, its not related at all, but it was only something to show what i wanted to ask :)

Big thanks for your patience.

This topic is closed to new replies.

Advertisement