Jump to content
  • Advertisement


  • Content Count

  • Joined

  • Last visited

Community Reputation

165 Neutral

About formalproof

  • Rank
  1. I recently managed to get a cheap copy of Charles Petzold's Progamming Windows, 5th Edition (1998). I haven't really kept up with times all that well. So my question is: is this book, and the Win32 API in general, still relevant and useful?   I'm pretty sure that programs using the Win32 API described in the book are still compatible with modern versions of Windows. But has the API changed much since 1998? Will I get suboptimal performance or miss some important operating system features if I use the API calls described in the book?   The reason for my interest in Win32 API is that I'd like to be able to program with the minimal amount of abstraction layers (such MFC, .NET or the standard C/C++ IO libraries). That way, I get to know what my program really is doing under the hood. I am also involved in some research projects where speed and efficiency are critical, and I suspect I may have to write a very high-performance Windows program in the future. For this purpose, I suppose C and Win32 are still the way to go?
  2. Ok, seems I made an embarassing mistake in understanding what little endian means So in a little endian system, val = 0xAABBCCDD is stored in memory as: 0 1 2 4 DD CC BB AA That would explain why vec[0] == 0xDD, etc. Thanks!
  3. I'm working on a project where I need to convert the bit pattern in an integer type to an array of chars (bytes), and vice versa. I'm wondering why this conversion always seems to "reverse" the order of the bytes. For example, when converting val = 0xAABBCCDD to an array of chars, the first element of the array will be 0xDD, the second 0xCC, etc., although I'm using a little endian system. For me, the logical order would be 0xAA, 0xBB, 0xCC, 0xDD, because that is the order in which the bytes of val are stored in memory, isn't it? Here is the code: #include <iostream> #include <vector> using namespace std; int main() { int one = 0x1; cout << "The system is " << (((one << 1) == 0)? "big endian" : "little endian") << endl; // This will output "little endian" on my system vector<char> vec; vec.push_back(0xAA); vec.push_back(0xBB); vec.push_back(0xCC); vec.push_back(0xDD); unsigned long* ul = reinterpret_cast<unsigned long*>(&vec[0]); cout << hex << *ul << endl; // Why does this output 0xDDCCBBAA? unsigned long n = 0xAABBCCDD; vector<char> w; w.assign( reinterpret_cast<char*>(&n), reinterpret_cast<char*>(&n) + sizeof(unsigned long) ); for(int i = 0; i < w.size(); i++) { cout << hex << (int)(unsigned char)w; // this cast is because we want to print the bit pattern of w, not the character } cout << endl; // This will output in the "reverse" order as well: 0xDDCCBBAA0000 } So I'm asking whether someone can explain to me why the order of bytes after conversion is 0xDDCCBBAA and not 0xAABBCCDD? Btw, if you know a better way to convert between an integer type and a vector of chars, or notice any other stupidities in my code, please let me know.
  4. Quote:Original post by Sneftel Not sure what you mean by "overkill". overkill = not much additional benefit compared to simpler system
  5. Thanks to both of you. Quote:Original post by Sneftel By the way, if you're doing events, and you're already using boost, take a look at boost.signals2, a signal/slot system which is already integrated with shared_ptr/weak_ptr for that sort of connection management. This would have been my second question: would you recommend using the boost signal library instead of my own notification system? I'm concerned it could a little overkill. Which approach would you take yourself?
  6. In my single player RPG (C++/Boost/Allegro), whenever a character dies, everyone who was targeting this character must be notified so that the AI can change its behaviour. I'm thinking of giving each character a list of other characters who are targeting it (a list "targeters"), and then broadcasting a message to each of them when the character dies. All my pointers are boost::shared_ptrs, and all characters are created on the heap. class Character { //... // List of characters who target this character std::vector< boost::shared_ptr<Character> > targeters; // Iterates over targeters and tells everyone this character died void broadcastDeath(); //... } // Called when this starts fighting target void Character::startFighting( boost::shared_ptr<Character> target ) { this->fightingTarget = target; target->addTargeter( this ); // here's the problem this->pushAiState(ais_fight); } Since I cannot directly get a shared pointer from this, would this be a valid situation to use boost::enable_shared_from_this? I could also make the targeters vector save raw pointers, but I don't like this idea. There may be other situations where I need take a member from the targeters vector and pass it to a function which takes a shared_ptr as an argument. Also, I'm not sure if it's safe to have a vector which saves a raw pointer to X and at the same time have other vectors which save shared_ptr to X (all the other "list of characters" vectors in my game save shared_ptrs). What would you do in this situation?
  7. Quote:Original post by ApochPiQ First of all do a clean rebuild of the project and make sure that you don't just have some corrupted files floating around in the build cache. This is generally the first port of call when dealing with mystery bugs, because you want to rule out the possibility that it isn't in fact your fault [wink] Thanks a lot -- doing a clean rebuild solved the first kind of problem. I'm now trying to see if there is a regular bug (not memory corruption) causing the other errors; I'll come back to you later when I know more. Just one question at this point: since Visual C++ obviously made a mistake and had a corrupted file in cache, does this mean it is likely to do it again, and could there be a specific reason for it, or does the corruption just happen at random?
  8. I'm writing a single-threaded, single-player game with Visual C++, Allegro and Boost. Some time ago I started to get some really weird access violations. I've tried to track back the changes I have made but I have found nothing which could cause them. For example, although I have not changed executable lines 1 to n-1 at all, I now get an error on line n (I did, however, change some of the #includes and the other files). I always encapsulate memory allocations inside shared_ptr's, like this: boost::shared_ptr<Sprite> sprite = boost::shared_ptr<Sprite>( new Sprite(32, 32) ); This is where one of the errors occurs. The Sprite constructor finishes, but the shared_ptr constructor gives an error: "0xC0000005: Access violation writing location 0xcdcdcdd1." Before now, everything worked perfectly, and I did not change any line which is executed before this one. The other errors I'm getting are weirder. For example (the following code also worked perfectly before I started getting the memory errors): bool Spell::isOnTarget() { // targetCharacter is of type boost::shared_ptr<Character> // x, y and targetCharacter are member variables of Spell if(( x == targetCharacter->x ) && (y == targetCharacter->y )) return true; else return false; } This never returns true, even when the coordinates are equal; I've tested situations when the spell should definitely be on target. When debugging with VC++, I can even check from the local variables window that x and targetCharacter->x, etc. are equal, yet, the condition still evaluates to true. If I change the code to this: bool Spell::isOnTarget() { int tempX = targetCharacter->x; int tempY = targetCharacter->y; if(( x == tempX ) && (y == tempY )) return true; else return false; } then it works. All this doesn't seem to make any sense. Probably the heap gets corrupted somehow, but I don't have any experience debugging memory corruptions, so I don't know what to do. All calls to the new operator are encapsulated in boost::shared_ptr constructors, so they shouldn't be any memory leaks. I've tried rebooting Windows XP, but it didn't help either. My game runs in a single thread, although I think some Allegro timer routines run in a separate thread. Which steps should I take to debug this? [Edited by - formalproof on July 7, 2010 4:17:46 PM]
  9. formalproof

    Losing the big picture while coding

    Quote:Original post by rip-off With practise, it is easier to remember the structure of programs. One skill that has come in useful time and time again is reading foreign code, as in code you didn't write. I honed that skill over many years on these forums - inferring architectural details from a beginners post is often required when important details are omitted by the beginner. If you can dive into anyone elses code, of varying quality, it becomes quite easy to navigate your own quickly. I'd love to learn to read code efficiently. I have tried to look at a few open-source RPG projects (which tend to be large, and not well documented), and I've found it extremely difficult to grasp the overall architecture. Especially since today complexity is divided into small parts, which means that you have to read and cross-reference N functions / source files to understand one algorithm.
  10. formalproof

    Losing the big picture while coding

    Quote:Original post by Telastyn Not in my experience. They quickly go out of date, don't show actual interaction between portions of code, and generally aren't accurate or useful abstractions. Let's say you have a large project, and you have not been developing it for several months. You have forgotten much of its architecture and how it works. What is the first thing you do in this situation? Do you simply read through all the relevant code files? Isn't that too many files to read? How many files do you have to read in average to get a good grasp? Do you use any additional tools at all to help refresh your memory?
  11. formalproof

    Losing the big picture while coding

    Quote:Original post by owl Ideally, you would have the entire design document of the application you are coding. The complete description of every functionality required. And the development divided in stages that are done when you code, test and debug the last routine in each one. That way you will always know where you are, what to do and how much is left to be done. I have found that designing everything in advance is a waste of time in my situation. Perhaps it works for professionals who have done everything already a million times, and know what works and what is doable. But for someone who is still learning, it is impossible to find a good design before seeing how the program develops. Especially in a game project, where it's very hard to predict what is going to be fun and what is not.
  12. formalproof

    Losing the big picture while coding

    Quote:Original post by Telastyn Quote: How do you maintain the big picture of your code structure while coding? I don't. Keeping details in your head gets harder as you get older, and even mildly complex programs are too complex to hold all the details in mind. Abstraction is your friend. While coding, you should only need to be aware of the things directly relevant to that code. Having a good design helps. Having good intellisense helps a lot. Having consistent naming and coding patterns help. Of course, in an ideal world your code is written so clearly that you don't need diagrams, or even comments. But we do not live in an ideal world. Like you say, abstraction is important. Aren't diagrams just that - useful abstractions? Since I'm writing a relatively large game in my free time (with long breaks between), what I need most is a quick way to refresh my memory about my program's architecture, interactions, sequences, etc. Doing this by reading my code is a pain, since it's a lot to read, and admittedly the code is often unclear and hard to grasp. (In an ideal world, I would refactor it and make it clear, but I have found that perfectionistic refactoring is rarely a good idea, since it takes way too much time. Besides, when I add new features, the new code will again be bad, so I there will always be unclear code in my project anyway.) Diagrams would be the perfect solutions for this, since they allow to understand the structure at one glance. But until now my attempts at creating useful diagrams have been unsuccesful, and I'm not sure how I can learn this. Watching how professionals do this would be very educational, but I don't have this opportunity in real life. Are there some online material or books available about this?
  13. I have the problem that I often lose the big picture of my program structure while coding. The best solution would naturally be to draw diagrams. I believe especially sequence diagrams would be useful, since my programs (usually games) tend to have complicated sequences and subsystem interactions. Until now, I've simply left some notes about the structure as comments in the code itself, but these are not practical, since you need to browse through the code to find them. However, when I try to draw such diagrams, it takes way too much time, and it's too time consuming to change the diagrams when the code changes. My question is, how do you solve this problem? How do you maintain the big picture of your code structure while coding? Which tools do you use to draw diagrams? Which diagram types do you use? How do you keep all your diagrams organized so that you can cross-reference them easily? How do you keep your diagrams up to date when the code changes?
  14. formalproof

    RPG damage computation math

    Sorry about frasing my question in an unclear fashion. I forgot to say that I do not know which intervals weapon, strength and dexterity are from. (i.e. I want my solution to be general and not dependent on these intervals). Quote:Original post by schattenmaennlein You want the damage to be 0-20%*Strength + 0-20%*Dextery + 0-60%*Weapon? Damage = Rf(0,0.2) * Strength + Rf(0,0.2) * Dextery + Rf(0,0.6) * Weapon; Because of the different ranges, this is not correct. For example, if we have the following ranges: weapon : [1,100] str: [1,20] dex: [1,20] And for example weapon=70, str=20 and dex=9. (i.e. str is maximal) And let's say the RNG Rf(0,0.2) returns 0.1, Rf(0,0.2) returns 0.1 and Rf(0,0.6) returns 0.3 Then damage is = 0.1*20 + 0.1*9 + 0.3*70 = 2 + 0.9 + 49 = 51.9 See the problem? The strength component is only 2, which is just about 4% of the total, although strength was at maximum! And the dice wasn't even especially bad (it was average). However, I was able to find a satisfying solution to the problem. Let's say the possible range of values in the game is: wpn: [1, wpn_max] str: [1, str_max] dex: [1, dex_max] i.e. str_max is the maximum strength possible in the game. And the attacking character has the following values: wpn = w str = s dex = d Then we define three random variables with uniform distribution: X_1 = {1,...,w} X_2 = {1,...,s} X_3 = {1,...,d} In the whole game, the maximum for the sum these random variables is: max = wpn_max + str_max + dex_max However, the maximum for our specific character is: total = w + s + d Our task was to construct a damage function such that "weapon contributes at most 60% of the damage, strength and dexterity both at most 20%, and the damage must be a value from [1, d_max]." The solution is: (assuming X_1 = x1, X_2 = x2, and X_3 = x3) char_damage = (x1)/(w)*0.6*total + (x2)/(s)*0.2*total + (x3)/(e)*0.2*total final_damage = (char_damage / max) * dmg_max The idea is not to look at absolute values, but at the "goodness" of each value, i.e. how far away it is from its maximum. First we compute how good the value is for this character, and then how good it is for all possible characters. It took me several hours to come up with this (I know, I'm stupid), but I did a few tests and it seems to work. I can make it even more general if I replace the percent factors (0.6 etc.) with variables. This is not only useful for damage computation, but for all kinds of RPG character ability checks. The main advantage is that you can formulate RPG rules in an intuitive fashion "lock pick skill determines 80% of the success, and dexterity 20%", and by setting a few variables you already have a complete rule. Do you think my approach is correct? Can you see any mistakes or problems with this?
  15. formalproof

    RPG damage computation math

    I'd like to state some rules of my RPG in the following way: "Each time a character lands a hit, the weapon modifier should determine up to 60% of the damage, strength should determine up to 20% and dexterity up to 20%. The damage must always be an integer from [dmg_low, dmg_high]." Is there a general formula for doing this computation? I know, my formulation of the goal isn't probably the best, and has several different interpretations. For example, where does the random element come in? (We don't want the game to be deterministic). One possible way is to define 3 random variables: X1 = {0,...,weapon} X2 = {0,...,str} X3 = {0,...,dex} (weapon is the character's current weapon modifier, etc.) With every value being equally possible, i.e. P( X1=i ) = (1/weapon) (in other words, X1 is simply a fair (weapon)-sided dice.) So the best possible result is that every random variable gives the maximum value: max = weapon + str + dex And now I want to create the damage function D, such that X1 contributes 60% to the value of D, X2 contributes 20% and X3 also 20%. Additionally, D needs to be an integer from [dmg_low, dmg_high]. D(X1,X2,X3) = ? How can I do this? It sounds simple enough, but I'm out of ideas.
  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!