PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

I'm rookie at C++ and I have simple questions

:cry:
Hello! I’m a starting programmer from Russia and I need help. I’m rookie at C++. And I have a simple questions, I am dealing with #-((
Can I load/unload pictures with Pocket Frog during the game? If yes, how can I do this (please write an example).
What is faster to load jpg or gif?
How can I reload an array to be bigger, saving the previous numbers and adding a new?
How can I load data from a *.txt file and pictures from a main memory.
Greetings from Russia.
Can I load/unload pictures with Pocket Frog during the game? If yes, how can I do this (please write an example).

Yes, of course you can. But keep in mind that loading images takes time and will slow down your game if you do this during the game loop. A better way to do it is during init or someplace where the user/client doesn't notice it. For instance, while displaying a "loading next stage" message. I don't have time to write an example right now.

What is faster to load jpg or gif?

Which ever is the smallest.

How can I reload an array to be bigger, saving the previous numbers and adding a new?

The best way to do this is NOT to use an array, but to use the std::vector container defined in the STL library. Again, I don't have time to write an example right now, but take a look at the bottom of page one at this thread where I've written something similar. http://forums.pocketmatrix.com/viewtopic.php?t=8274

How can I load data from a *.txt file and pictures from a main memory.

The answers to these questions can be found if you read through this forum. Loading files was re-posted rather recently I think.
To load images:

Code:

Surface* imgWhatever = LoadImage(GetDisplay(), IDR_WHATEVER);

Where IDR_WHATEVER is your image resource's ID. You can load from a file as well by passing a TCHAR* filename instead of a resource ID. I've never loaded from a file, so I don't know if there's any other details to doing so. You can do this at any time you want, but as adde pointed out, be careful when you do it.

Is a JPG or GIF faster to load? Theoretically a GIF should be faster just because it's a simpler format. In reality, the difference is likely to be so small that it doesn't matter. You might consider PNG's though... probably won't load any faster, but it's a generally superior file format.

As for your array question, adde is right, an array isn't the right structure to use if the size can change. A vector is a better choice, or, if you don't want to use something from STL, you can write a linked list yourself. As a VERY basic example:

Code:

// Define an element of the list
struct LLElement {
LLElement* link;
int value;
};

// Then in your main code...

// Create three elements for our list
LLElement* element1;
LLElement* element2;
LLElement* element3;
// Fill in their values and link them properly and
// remember that the last element links to NULL
element1->elementValue = 5;
element1->link = element2;
element2->value = 6;
element2->link = element3;
element3->value = 7;
element3->link = NULL;
// Iterate through our list and display the values
LLElement* nextElement = element1;
while (nextElement->link != NULL) {
cout << nextElement->value;
nextElement = nextElement->link;
}

Assuming I haven't made any really stupid errors, that should work. The basic concept is that each element of your linked list holds a pointer to the next element in the list. That does essentially the same thing as an array, but you can add elements as much as you want, as long as you update the pointers properly. One BIG different though, and this might make it useless depending on the situation, is that you can't read a specific element of the list. You must always traverse the list until you get to the element you want. For example, there's no way to do something like nextElement[5] above.

As for loading data from a file vs. memory, to load from memory do something like:

Code:

LPVOID loadFromMem(int resourceID, HMODULE exeModule) {
HGLOBAL hResD = NULL;
void *pvRes = NULL;
DWORD dwSize = NULL;
LPVOID lpMemory = NULL;
HRSRC hResInfo = NULL;
// Find the resource and load into memory
// Note: DATA is the resource group name
hResInfo = FindResource(exeModule, MAKEINTRESOURCE(resourceID), L"DATA");
hResD = LoadResource(exeModule, hResInfo);
pvRes = LockResource(hResD);
dwSize = SizeofResource(exeModule, hResInfo);
lpMemory = malloc(dwSize);
memcpy(lpMemory, pvRes, dwSize);
return lpMemory;
}

// Then from somewhere in your code:
unsigned char* pointerToMem = (unsigned char*)OASIS_loadFromMem(IDR_DATA, hInst);
// hInst is the handle to your executable module

You'll then have a pointer to a copy of your resource data that you can manipulate like any other chunk of memory. Note that if you don't copy the data, you won't be able to change it since you locked it when it was loaded. If you don't need to modify it, of course don't bother with the copy, just return pvRes instead.

As for loading from a file, on way to go:

Code:

HANDLE hFile = CreateFile(L"file.txt", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_HIDDEN, NULL);
DWORD dwFileSize = GetFileSize(hFile, NULL);
unsigned char* pFileDat = (unsigned char*)malloc(dwFileSize);
DWORD numBytesRead = 1;
int bResult = ReadFile(hFile, pFileDat , dwFileSize, &numBytesRead;, NULL);
CloseHandle(hFile);

There's more than one way to read in a file, that's just one.

I think that covers all your questions...
FZammetti wrote:Assuming I haven't made any really stupid errors, that should work.
... You must always traverse the list until you get to the element you want. For example, there's no way to do something like nextElement[5] above.


And this is exactly why one should use the STL containers. If you want to use a list, use the STL list container. If you want fast direct access to individual elements, use the STL vector container.

These containers are written for a reason. Simplicity. Unless you are a really good programmer you will always make some mistakes writing your own container. And the STL containers are not only thourougly tested and debugged, they are also optimized so they will be faster then your own code (in almost every case anyways).

So, there is no reason why one should not use the STL libraries. For instance, instead of making memory mistakes with char* strings, use the STL string container. This will also enable you to concatenate and extend strings in a way not possible for the old char* strings (also called cstrings)
adde wrote:
And this is exactly why one should use the STL containers. If you want to use a list, use the STL list container. If you want fast direct access to individual elements, [use the STL vector container.]


, use fixed arrays.




These containers are written for a reason.
...
optimized so they will be faster then your own code (in almost every case anyways).


Faster than my inept attempt to rewrite the wheel I'll give you, but not faster than a regular old array.



So, there is no reason why one should not use the STL libraries. For instance, instead of making memory mistakes with char* strings, use the STL string container. This will also enable you to concatenate and extend strings in a way not possible for the old char* strings (also called cstrings)


There are reasons for the limitations you mentioned.

But either way there is a very good valid reason to avoid the STL, speed, and memory. Both of these are in short supply on a pocket pc.
Smirk wrote:use fixed arrays

Well, the whole idea here was that he wanted an array that could grow in size. STL is the best choice.

but not faster than a regular old array.

Sigh... Isn't that pretty obvious? Anyways, an STL vector is not that much slower.

there is a very good valid reason to avoid the STL, speed, and memory.

Speed? Depends on the application. As I was saying, the STL containers are optimized. Memory? Shure, there will be a slight increase, but STL is not like MFC where your program will explode. And in a game, the slightly increased size of STL won't be your biggest memory problem anyways so.

The benefits of the STL library counters the speed and memory issues.
Sm!rk wrote:
Faster than my inept attempt to rewrite the wheel I'll give you, but not faster than a regular old array.


Indeed, A std::vector<> won't be faster to access then an array. They will have exactly the same speed.


But either way there is a very good valid reason to avoid the STL, speed, and memory. Both of these are in short supply on a pocket pc.


The standard containers and algorithms don't incur any memory or speed tradeoff over implementing these things yourself. Au contraire, they tend to produce tighter and more focused code. A big bonus is that they are already debugged and allow you to write code much faster. Saying otherwise is only demonstrating a lack of understanding of how the STL works.

Now for std::string... That's an entirely different issue.
This is the same argument that people tend to make when they are learning the concepts behind component-based coding. You have to give up a certain level on control to gain a theoretically greater benefit.

Using STL provides you with pre-written units of code that you presume are well-tested, bug-free and pretty well optimized. This, as Thierry rightly stated, saves you time (in many cases a great deal of time) in writing your code because you (a) don't have to write new code yourself and (b) spend time debugging it and (c) spend time optimizing it.

Perhaps more importantly, or at least just as important, is the simple fact that your time will be spent solving YOUR problems and implementing YOUR project, not writing what are really more mundane pieces of code and re-inventing the proverbial wheel.

It's funny, but people don't seem to mind using strcat or functions like that, but STL is a problem... Why? Your in both cases trusting that someone did their job right and did it well. Same thing when you use any of the "base" classes in Java for instance.

Your right, memory and speed are in short supply on a PocketPC, but only relative to desktop machines of today. In any case, it's worth whatever down-side there might be to use STL when you realize how much time and effort it will save in the long-run. This goes for any pre-written piece of code (assuming you have a belief that the code is well-written naturally).

I completely understand the other side of the argument though... for many years I fought against using any code in my projects that I didn't write myself. Partly because I didn't want to give up the control, partly because I believed I could always do a better job. Giving up some control is something you have to get used to, although obviously if you have the source too that's not really an issue. As far as doing a better job myself, that still might be true, but the benefit of not having to is more important.
:lol: :lol: :lol: Thanks very much for attending to my little problems!!!
Conan · 18 Mar 2003, 21:16 UTC — ok then, what can STL do ? how does it save me time ???
reading Fzammetti & Thiery's posts I just have to ask the obvious question, what can STL do for me? I don't know anything about it other than the PF connection.

How can STL save me time writing a game? What pre-written code can I use from STL and where do I find out in plain English what's possible. ( I mean something understandable not written by Unix/C++ geeks who delight in making simple things confusting )
8) At third time: Thanks for answering! 8)
STL, or Standard Template Library, is a C++ library that provides a set of easily composable C++ container classes and generic algorithms (template functions).

The container classes include vectors, lists, deques, sets, multisets, maps, multimaps, stacks, queues and priority queues.

The generic algorithms include a broad range of fundamental algorithms for the most common kinds of data manipulations, such as searching, sorting, merging, copying, and transforming.

The most important difference between STL and all other C++ container class libraries is that most STL algorithms are generic: they work on a variety of containers and even on ordinary C++ arrays. A key factor in the library design is the consistent use of iterators, which generalize C++ pointers, as intermediaries between algorithms and containers. A precise classification of iterators into five categories is the basis for determining which algorithms can be used with which containers, and is the main guide to extension of the library to include new algorithms that work with STL containers, or new containers to which many STL generic algorithms can be applied.

Take a peak at http://www.cs.rpi.edu/projects/STL/htdocs/stl.html for more in-depth information (yes, I just copied all of the above from that site!)

To boil it down simply, it's really just another library with functions and structures that tend to get used a lot in programming (maybe more so in games). The nice thing about STL is that it is designed from the ground-up to be cross-platform (I guess that's really the point actually) so your code that makes use of it's functionality should be identical across all platforms.

Thierry has certainly more experience with it than me, so hopefully he'll elaborate a bit (and correct anything I'm wrong about of course!)
Thanks to Thierry and FZammetti for backing me up and clearifying some of the STL issues. Due to my lack of indepth knowledge about the STL libraries I overestimated the speed and memory loss. I've always considered it to be the better choise and never really bothered to do a benchmark or investigate further.

To Conan (and anyone else that wants to learn about STL) take a look at the following STL tutorial. I havn't read it but I've looked at some other stuff at the Pixelate site.

Pixelate homepage:
http://echellon.hybd.net/index.php

STL tutorials:
http://echellon.hybd.net/issues/5/artic ... part1.html
http://echellon.hybd.net/issues/6/artic ... part1.html
Conan · 19 Mar 2003, 08:41 UTC — Links don't work
The links are wrong or down so I will look for the site......... Nope, no links on Google work though the tutorials are there. I will try later
All the three links are valid. I copied them directly from the browser when I wrote the post, and I just verified em by clicking.
yep, they work now :)
fzammetti wrote:
I completely understand the other side of the argument though... for many years I fought against using any code in my projects that I didn't write myself. Partly because I didn't want to give up the control, partly because I believed I could always do a better job. Giving up some control is something you have to get used to, although obviously if you have the source too that's not really an issue. As far as doing a better job myself, that still might be true, but the benefit of not having to is more important.


Actually I'm not the control freak personality, although its very common for programmers. I have no problem using it on a desktop, which I'll do any chance I get.

What the folks in this thread are ignoring is the cost of the dynamic stucture. You say vector is as fast as array, and I say you are wrong, its no where near as fast as array. The closest vector could come to the speed of an array would be to use a fixed array itself. Even then there would be a small overhead when accessing it, maybe, but with template magic I would assume it to generate the same code as a regular fixed array. However that isn't that case since it can grow dynamically. That's what you are paying for, and paying dearly (relative).

If I have some spare time later, I'll write up an app to better test this if people are pondering, although someone else could do this. What's the stl lib of choice on ppc, boost?


Edit:
Oh and the point about *needing* a dynamic array, most cases this is just flexibility that isn't necessary.
Well, Thierry indicated in a previous post that a std::vector will be the same speed as an array,

Adde thinks it's obvious that an array would be faster.

Let's assume that Thierry is actually wrong (which I tend to not believe anyway) and an array is in fact measurably faster than a std::vector.

The question, which seems obvious to me and should to everyone I think, is simply this: is the decrease in speed (a) measurable to an extent that will matter and (b) is the benefit of using a vector worth that decrease in any case?

As you said, many times a dynamic array (or vector) isn't really needed, and if that's the case than you can certainly make a good argument for not using STL. But, if you do need that flexibility, it's going to be hard to argue for NOT using STL. The benefits in terms of not having to write the code yourself and assuming it's well-optimized already overrides whatever speed decrease your likely to see.
First of all, I just want to clearify that STL is so much more than just the vector container.

Choosing the right container for your application is also something one should consider.

Since this discussion has started I got curious and used the all-knowing Google to find benchmark results for built-in array vs. STL vector and I found these two links.

http://www.cs.duke.edu/~ola/courses/cps ... e/builtin/
http://www.opencascade.org/upload/87/gc ... ean_marker

Both are tests executed on rather powerfull computers (atleast compared to a PDA) so I'm not shure how useful these results are. But they both show that regular array usage has faster access times than using the STL vector container (as I hinted before).

The second link goes to a benchmark that I won't even begin to try and explain here because the note would be loooooooooooooooong (or perhaps I should say looooooooooooooooooonger! ;).

The first link however goes to a test that got the following results:

a_int = 0.47
v_int = 0.58
a_str = 1.18
v_str = 1.36
a_strp = 0.44
v_strp = 0.58
a_cstr = 0.44
v_cstr = 0.6

where
a = built-in array allocated with new.
v = STL vector
int = integer
str = STL string
strp = STL string pointer
cstr = CString / char*

The result is the time in seconds it took to perform the test on a "Sun Ultrasparc 20 with 128 megabytes of memory"

The tests was 10 repetitions of an array of 100,000 elements that was first copied to another array and then shifted to the right so that the last element became the first. So, each test is 10*100000*4=4M array/vector element accesses (2 gets and 2 puts for each element)

Pew.. for more info I refer to the above link. Unfortunately I ran into some problems when I tried to convert the test so it could be run on a PDA platform and I don't have time for this right now (this took long enough, let me tell you).

Hope someone finds this usefull.. And perhapps someone can make a WinCE port of the test so we can test our own devices.

I still stand by my previous notes though. STL containers should be used where applicable. Arrays should be used instead of STL vectors when access time is crucial. And I mean CRUCIAL! If you take a look at the test, it says that for 4M element accesses the maximum time penalty is 0.18 seconds. These test results has not been (as far as I could understand anyways) averaged over a series of tests so the average maximum time penalty is probably a few hundreds of a second lower.

Accessing only a few hundred elements and I would assume that the time differance would be very slim (or practicly zero as Thierry said).

But, it remains to be seen how the test preforms on a PDA.

Benefits of using STL containers are:
* They are dynamic.
* They are thuroughly debugged and tested.
* You don't have to keep track of the number of elements in the container, since all containers have a size() or length() function.
* You won't run into memmory leaks since each container is allocated on the stack and will be deallocated once it goes out of scope (like any other local variable). This ofcourse asumes that you don't store any heap allocated data (i.e. with new) in the containers, because then you are responsible for deallocating it as usual (with delete).
* The containers are very similar so if you need to change to a different representation it wouldn't take that much of an effort (relatively speaking anyways).
* All the containers can be accessed through iterators, which allows you to write code independant of the chosen container which could be practical in some situations.

I'm shure there are a few more but I'm to damn tired to come up with them.

Thank you and good night =)