|
This thread is locked; no one can reply to it. |
1
2
|
[OpenLayer] Bitmaps and Iterators |
Matthew Dalrymple
Member #7,922
October 2006
|
I'm trying to get this to work... here is the pastebin if you'd rather look at it through that http://pastebin.bafserv.com/699 Anyway, when using an iterator to call the Draw() method nothing actually draws. Pressing space in the example will draw them as Draw() is being called directly.
What am I doing wrong/how can I correct this? Hehe don't ask why I used gnolam's avatar. EDIT: Here is the fix for this.. is there anyway to do this with the above example?
=-----===-----===-----= |
CGamesPlay
Member #2,559
July 2002
|
If Bitmap doesn't have a proper copy constructor, it won't be copied, and it won't be displayed. Maybe checking IsValid inside the Draw method will tell you something? When you push the a, b, c, and d objects into the vector, you push the actual objects, so you actually make a copy of them. When this happens, OpenLayer has to send a copy of the Bitmap to the graphics card. I don't know if it handles this properly. One possible fix is to use a Bitmap* inside the class, and continue to use a vector of objects, but the pointer method is superior for most cases in terms of performance. -- Ryan Patterson - <http://cgamesplay.com/> |
Matthew Dalrymple
Member #7,922
October 2006
|
FYI: CGamesPlay was a major contributor to this being fixed :-D. Another error that I'm having that CGamesPlay said was the right syntax and I thought so myself is this line: ... vector < Avatars* > CurrentList, AvatarList; // Avatar List gets filled so don't worry there is stuff in it ... CurrentList.reserve(MAX_AVATARS_ON_SCREEN); for(int i = 0; i < MAX_AVATARS_ON_SCREEN; i++) CurrentList.push_back(AvatarList.at(rand() % AvatarList.size())); *.at() should return a pointer and so it compiles correctly. The problem is that it stops things from being drawn and causes my CPU to be 100% and lags like crazy. I comment that line out and "bam" it's fixed. Is their a correct way to do this or is their a workaround like maybe setting a temp Avatar instance setting it equal to *.at(...) then pushing it to the back of CurrentList? I would rather have a one line simple thing. =-----===-----===-----= |
CGamesPlay
Member #2,559
July 2002
|
What is the value of MAX_AVATARS_ON_SCREEN? Like I said: that line of code is fine. Show the part where you draw, for instance. -- Ryan Patterson - <http://cgamesplay.com/> |
Matthew Dalrymple
Member #7,922
October 2006
|
MAX_AVATARS_ON_SCREEN is set to 15 for now. This is how they're drawn: bool Game::DrawAvatars() { vector< Avatar* >::iterator curAvatar; for(curAvatar = CurrentList.begin(); curAvatar != CurrentList.end(); curAvatar++) (*curAvatar)->Draw(); //Log::log("CurrentList.size() = %i", CurrentList.size()); return true; } This is the Avatar's Draw method: bool Avatar::Draw(void) { this->m_Avatar.Blit(10, 10); return true; } EDIT: I hate having difficult questions that no one knows the exact answer to or just ignore me =-----===-----===-----= |
CGamesPlay
Member #2,559
July 2002
|
I wonder if you've found the answer yet? -- Ryan Patterson - <http://cgamesplay.com/> |
ImLeftFooted
Member #3,935
October 2003
|
Donno why its hanging there, likely a copy paste error or something. CurrentList.reserve(MAX_AVATARS_ON_SCREEN); for(int i = 0; i < MAX_AVATARS_ON_SCREEN; i++) CurrentList.push_back(AvatarList.at(rand() % AvatarList.size())); Should be: #include <algorithm> CurrentList.resize(AvatarList.size()); random_sample(AvatarList.begin(), AvatarList.end(), CurrentList.begin(), CurrentList.end());
Quote: Here is the fix for this.. is there anyway to do this with the above example? Its not pretty:
|
Matthew Dalrymple
Member #7,922
October 2006
|
Quote: I wonder if you've found the answer yet? Haha nope, well now that DD has posted I might have. To DD (Or anyone else): I'm reading about random_sample() from cppreference.com and it seems that it confuses me haha. =-----===-----===-----= |
CGamesPlay
Member #2,559
July 2002
|
random_sample(AvatarList.begin(), AvatarList.end(), CurrentList.begin() + dead_avatar_index, CurrentList.begin() + dead_avatar_index + 1);That will make it replace 1. I have no idea why you wouldn't just use your code. -- Ryan Patterson - <http://cgamesplay.com/> |
Matthew Dalrymple
Member #7,922
October 2006
|
I would use my code because it doesn't work. It may be logically correct but its causing things to not work, I mean I can rar it up again and show you how it doesn't work Edit: I keep getting the error: random_sample().. first use of this function. And I've included algorithm >.< Rebuilt all... Baaaah. =-----===-----===-----= |
Jakub Wasilewski
Member #3,653
June 2003
|
Quote:
Edit: I keep getting the error: random_sample().. first use of this function. And I've included algorithm >.< Rebuilt all... Baaaah. Err, maybe std::random_sample or "using namespace std;" would help? These seem like errors you get when you forget about a namespace . This and the fact that <algorithm> probably only wraps <algo.h> in a namespace . --------------------------- |
ImLeftFooted
Member #3,935
October 2003
|
Quote: When one dies a random avatar from AvatarList will be popped onto CurrentList. [edit]Ah I see. Yes your code is better suited for this task. Just don't use reserve. Its generally useless unless you really know what you're doing. And even when you do, it pretty much will only apply to one compiler. |
Matthew Dalrymple
Member #7,922
October 2006
|
Jakub Wasilewski said: Err, maybe std::random_sample or "using namespace std;" would help? Tried that and I am already using the standard namespace. Dustin Dettmer said: Just don't use reserve. Then how do I set the size that CurrentList is going to be filled up to? =-----===-----===-----= |
CGamesPlay
Member #2,559
July 2002
|
vec.resize(newsize); -- Ryan Patterson - <http://cgamesplay.com/> |
Matthew Dalrymple
Member #7,922
October 2006
|
Bah I never saw resize() on cppreference.com, I always overlook things. That's totally what I wanted to use in my other projects too not reserve(). Umm where can I get an update for all the standard libraries because no matter what I try I can't get random_sample() to work with <algorithm>. =-----===-----===-----= |
ImLeftFooted
Member #3,935
October 2003
|
What compiler are you using? |
Matthew Dalrymple
Member #7,922
October 2006
|
MinGW (the version that came with Dev-C++ beta 5 [4.9.9.2 or whatever]) =-----===-----===-----= |
ImLeftFooted
Member #3,935
October 2003
|
Should support it just fine. Paste some code. |
Matthew Dalrymple
Member #7,922
October 2006
|
Quote: Paste some code.
#define MAX_AVATARS_ON_SCREEN 15 ... #include <algorithm> ... CurrentList.resize(MAX_AVATARS_ON_SCREEN); random_sample(AvatarList.begin(), AvatarList.end(), CurrentList.begin(), CurrentList.end());
When I use #include <algo.h> Quote: Â is not valid for all of my Avatars in CurrentList. I changed CurrentList and AvatarList from vector < Avatar* > to vector < Avatar > and I got their names instead of that weird A but they were still not valid >.<. I'm thinking about trying this with an array instead of a vector. Or maybe try and STL::List (but lists and vectors are so similar I wouldn't see how that would effect things. =-----===-----===-----= |
CGamesPlay
Member #2,559
July 2002
|
Could you show the code that wrote the "Â is not valid", as well as the full code for the Avatar class? -- Ryan Patterson - <http://cgamesplay.com/> |
Matthew Dalrymple
Member #7,922
October 2006
|
Avatar.hpp
Avatar.cpp
Ok to get the log file to produce this:
This is what I did
=-----===-----===-----= |
CGamesPlay
Member #2,559
July 2002
|
Add a copy constructor to your object: Avatar::Avatar(const Avatar& other) { Log::error("Object not allowed to be copied"); }
-- Ryan Patterson - <http://cgamesplay.com/> |
Matthew Dalrymple
Member #7,922
October 2006
|
Same log file. The object isn't being copied. Is that good or bad? =-----===-----===-----= |
CGamesPlay
Member #2,559
July 2002
|
Good Unfortunately, I'm at a loss. Start dumping your objects at very frequent intervals and see what causes them to screw up. Make sure they are loaded correctly. -- Ryan Patterson - <http://cgamesplay.com/> |
Matthew Dalrymple
Member #7,922
October 2006
|
Baaaaah, this is what always defers me away from programming sometimes. Everything always looks right, seems like it should work... BAM! RUN TIME ERRORS that are hard as hell to solve. =-----===-----===-----= |
|
1
2
|