• Content count

  • Joined

  • Last visited

Community Reputation

113 Neutral

About DanBerliner

  • Rank
  1. All a bit...Overwhelming. Where do I start?

    What is your goal? Are you simply looking to make a game or are you looking to know how to program? A graphical game will be well above the capabilities of a beginner programmer; there's just too much involved. If all you want to do is make a game use a tool like Game Maker. If you really want to do it with a programming language you are going to have the spend time getting better at general programming before doing something as ambitious as this. 
  2. 3 weeks until moving day. Gonna be strange living somewhere else.
  3. C++ arbitrarily sorting a struct

    [quote name='SiCrane' timestamp='1355716029' post='5011503'] [quote name='wqking' timestamp='1355711372' post='5011470'] My code returns true only whan a's all fields are less than b's [/quote] This isn't a legal comparison for use with std::set. A valid comparison needs to be a [url="http://en.wikipedia.org/wiki/Strict_weak_ordering"]strict weak ordering[/url]. One of the properties of a strict weak ordering is that if you have three objects A, B and C if A and B are equivalent and B and C are equivalent then A and C must also be equivalent. However, with this kind of comparison this property doesn't hold. One way to do this is to use a lexicographical comparison. Ex: [code] if (a.dir < b.dir) return true; if (a.dir > b.dir) return false; if (a.startx < b.startx) return true; if (a.startx > b.startx) return false; return a.starty < b.starty; [/code] [/quote] Interesting, thanks for sharing.
  4. C++ arbitrarily sorting a struct

    That's roughly the same idea, I like your approach better. Thanks. The exact order it sorts in is unimportant, it just needs some way to do so.
  5. I'm currently making a scrabble like game, the AI is the topic of this week. Basically the computer goes though and finds every possible place it can make a move. I handle this data with the below struct [source lang="cpp"]struct Move{ int startx,starty, score, dir; std::string word; }; [/source] While the string and score are there for later reasons, any object with the same starting points and direction must be the same word. My algorithm for finding possible moves takes on duplicates so I'm using std::set to store it. To do this, however, I need a way to compare one Move from another. Dir ranges from 0-1, startx and starty both range from 0 to 15 (in theory they can be expanded, unlikely ever more than 100 though). Here is what I am currently using [source lang="cpp"]bool operator<(const Move &a, const Move &b){ return (a.dir + a.startx*10 + a.starty*1000) < (b.dir + b.startx*10 + b.starty*1000); }[/source] As long as the starting X position is under 100 I don't see any way to produce false-duplicates. Is there a better way to do this?
  6. I'm glad this happened when it did, a few days later and I would have bought a commercial license.
  7. [quote name='SiCrane' timestamp='1302306587' post='4796148'] Remember that the MSVC debug and release settings aren't magic; they're both just a combination of presets of compiler and linker options. Modern versions of MSVC allow debugging of optimized release builds as well as standard debug builds. If you want you can create a debug mode that disables things like _SECURE_SCL and _HAS_ITERATOR_DEBUGGING so they run faster or a release mode that disables optimizations so it's easier to debug. [/quote] Interestingly enough if I hit the "debug now" button while on the release configuration it sees no performance hit at all.
  8. @rip-off I was using debug and that is the root of my problem. Doing it off debug improves the code from taking nearly 600ms to a mere 3ms. A 1080p vector only takes 15ms. This project is going to be fun without being able to use debug... @alvaro, it takes me around 1.2 seconds to do it on Visual Studio off debug and around 3 seconds on GCC, its also a bad idea to use vectors with simple loops; iterators are much more reliable.
  9. [quote name='alvaro' timestamp='1302266330' post='4795932'] [quote name='Dan Berliner' timestamp='1302219094' post='4795740'] I've done that, but just to iterate though an 800*600 (480,000) element vector takes my computer about 570-590ms.[/quote] That comes out to about 3000 cycles per pixel. Something is not right... It should be at least two orders of magnitude faster. Perhaps you can post some short code that shows the slowness? [/quote] I unfortunately don't have the exact code anymore but it was very simple. It was a vector<bool> that was 800*600 in length (it was one dimensional). I then used a simple iterator to go though it in a for loop. Inside the for loop I incremented an int by 1, I didn't even draw a pixel.
  10. [quote name='SiCrane' timestamp='1302222371' post='4795759'] Which compiler did you use for that benchmark? Some compilers create extra code even in release mode when using standard library templates. For example some versions of MSVC will enable _SECURE_SCL in release, which can significantly slow things down. [/quote] I'm using MSVC, I'll look into optimizing on that compiler.
  11. [quote name='alvaro' timestamp='1302143152' post='4795315'] [quote name='Dan Berliner' timestamp='1302137979' post='4795293'] What about multi dimensional vectors? Would the same advantage apply to a multi dimensional vector? The math gets a little more annoying if it is one dimensional going over a two dimensional screen. [/quote] The simplest way to deal with multi-dimensional arrays is to use an underlying one-dimensional array and translate your multiple indices into a single index. [code] std::vector<bool> image(false, 1920*1080); image[x+y*1080] = true;[/code] [/quote] I've done that, but just to iterate though an 800*600 (480,000) element vector takes my computer about 570-590ms. I have a pretty high end machine so that's unacceptably slow. It's just a simple iteration loop where it adds one to a int variable; the actual program will have to do significantly more than that per loop. When I was using an array of bool for the same sized screen it took about 47ms while drawing. Unless there is some way to optimize this considerably I dont see vectors as being a possible solution even if they can do the one bit thing.
  12. What about multi dimensional vectors? Would the same advantage apply to a multi dimensional vector? The math gets a little more annoying if it is one dimensional going over a two dimensional screen.
  13. [quote name='alvaro' timestamp='1302123405' post='4795210'] [quote name='Dan Berliner' timestamp='1302120771' post='4795189'] I recall reading somewhere that bool was longer than 1 bit, after googling it again I see that I was incorrect. Thanks.[/quote] bool does indeed take more than one bit. At the very least it takes one byte, and it may take more. However, std::vector<bool> is implemented in some clever way and will store bits compactly, just as you want. [/quote] Great, so now I cant read. After looking another time I see it is 1 byte, not one bit. I will go with std::vector<bool>, I currently have a proof of concept written up with simple bools in an array and it is seeing some performance issues on the slow computer running it.
  14. [quote name='Nanoha' timestamp='1302120800' post='4795190'] Its usually easier to use a seperate variable for each component and combining them, than remembering huge numbers for each colour. unsigned int red = 128; unsigned int green = 0; unsigned int blue = 255; unsigned int alpha = 255; You can put them all together into an int with some bitwise operators unsigned int colour = 0; colour |= (red << 24); colour |= (green << 16); colour |= (blue << 8); colour |= |= alpha; You can do the opposite to get each individua l component back red = 0xff000000 & colour; // only want the red bits, so mask the others out red = red >> 24; green = 0x00ff0000 & colour; green = green >> 16; I'm slightly rusty so it may have mistakes. Your just packing each different component into one int, most significant bytes being red in what I've done. Of course it all depends on what bytes are considered to be what, I've gone for RGBA but you see lots of combination (BRGA etc). No need to deal with individual bits (unless you have to). You can do individual bits in a similar way. Take a look at bitwise operators [url="http://www.cprogramming.com/tutorial/bitwise_operators.html"]http://www.cprogramm..._operators.html[/url] . [/quote] The program don't allow for more than black and transparent so a 1 bit value per pixel is all that is needed. Thanks for the explanation though.
  15. [quote name='Antheus' timestamp='1302120255' post='4795183'] int is 32 bits. char is 8 bits. short is 16. [/quote] My mistake there [quote name='Antheus' timestamp='1302120255' post='4795183'] See std::vector<bool>. It should be specialized to use one bit per value, while working like a regular array. [/quote] I recall reading somewhere that bool was longer than 1 bit, after googling it again I see that I was incorrect. Thanks. For people who may come here looking for the original requested solution (perhaps for a different issue), the following function will tell you if a bit is on or off in an int data type (you can modify it to use any fundamental data type: [code] bool isBitOn(int place, int &subject) { return ((subject & 1<<place)==0 ? false : true); } [/code]