Advertisement Jump to content
  • Advertisement


  • Content Count

  • Joined

  • Last visited

Community Reputation

429 Neutral

About Lith

  • Rank

Recent Profile Visitors

The recent visitors block is disabled and is not being shown to other users.

  1. Gah, that was stupid. Thanks.    Thanks for the tip! I'll upgrade my casts.
  2. Eh, this may be kinda stupid, but I have the traditional setup: class tile { // ... }; class tilePlayer : public tile { // ... }; At one point in my code I had a pointer to a tile instance and I wanted to access it as a tilePlayer instance. So I tried something like this: // map is an array of pointers to tile instances (tilePlayer*)map[ index ]->PlayerSpecificFunction(); This didn't compile ( "PlayerSpecifcFunction not member of tile" ) so I tried this and it worked: tilePlayer* p = (tilePlayer*)map[ index ]; p->PlayerSpecificFunction(); Isn't the latter just the same as the former but with more steps? Or am I messing up the syntax?
  3. To give some context, I'm 17 and I have been programming for a few years.   I was doing a code challenge on, (similar to project euler), that involved converting to and from negabinary numbers (base negative 2). I had never heard of, nor even considered numbers with a negative base. But I managed to figure out some patterns in the tables the problem gave me and wrote a nice converter from negabinary to decimal. But the other way around, that was a different story. I tried for hours to figure out a way of converting decimal to negabinary but to no avail. It got reasonably late so I slept on it. The next day I tried for hours again but still couldn't figure out an algorithm. After this I googled it, found a wikipedia page with an algorithm all ready and waiting for me.   I had solved it, albeit with a little "research"...   I want to be the best programmer I can, and I think I'm on the right track by attempting these coding challenges. But I'm a little lost on what I should do when I'm faced with a situation like this. Almost all the problems I'll solve (at least early on) will have been already solved brilliantly by someone else. I'm sure If I was working, my boss wouldn't be impressed if he found out I'd spend the best part of 2 days trying to solve a problem that I could have solved in 2 minutes If I had googled it. But googling it feels like cheating, and I don't get to practice my problem solving abilities.   It seems like the best programmers are able to take problems that no one has solved before and develop solutions on their own.   I guess what I'm asking is how do I get really fucking good at problem solving? By spending massive ammounts of time thinking through problems for myself or by looking at as many past solutions as possible?
  4.   Ah my bad. Thanks for pointing that out Frob.   I guess what I'm trying to do is redesign without breaking anything.
  5. Thanks dmatter, good info.   This seems like a very useful skill to learn. I'm going to try and challenge myself to keep the project working as it was previously at every commit, while still slowly changing things around and refactoring.
  6. I'm going to preface this topic with the fact that I've only every worked alone on projects.   In the project I'm working on, I have got to a stage where I've finally decided to refactor one of the systems that a lot of the total code depends on. Refactoring this would probably mean refactoring a lot of other code afterwards. Now I realise that I've got to this scenario through a horrible combination of bad design choices and lack of forethought, but my question is what to do when I find myself in this situation.   Because I'm working alone on this project, it doesn't really matter if I just make a new branch, completely refactor the system and then all of the other code afterwards. This could mean that the project won't actually build for days. Again, not too much of a problem considering that I'm working by my self.   What would be your advice if I encounter this when I'm working with a team of programmers? Assuming that the refactoring work HAS to be done, how can I approach this problem so that I cause minimum disruption to the other programmers?   Thanks for your time.
  7. Thanks for your speedy reply Samith, lots of good info. This in particular was very enlightening. I'll use a byte order marking to check weather to swap all other numbers read.   It's nice to know that pretty much all conventional PCs use little endain. But I did a bit of searching and found that vc++ has some built in functions for swapping byte order (I think they're quite well optimised as well). With all this info I found that it really wasn't much effort to support big and little endian (unless I've overlooked something) even if it's not likely to be necessary. Good practice I guess.   This is what I managed to come up with so far: It still needs to be cleaned up, but I think I've not overlooked anything important (other than a load function..) bool save_as_image( std::string argFilename, std::vector<ggCell>& argCells ) { // saves a vector of cells as an image. // could pack more into each pixel but it's not necessary // calculate the image size needed unsigned int dimWidth = 128; unsigned int hlpBytesNeeded = GG_BYTES_PER_CELL*argCells.size(); unsigned int dimHeight = 17 + (hlpBytesNeeded/4)/128; out("height is " + uint_to_string(dimHeight)); // load template header sf::Image imgHeader; if ( !imgHeader.loadFromFile("data/pattern_header.png") ) { return false; } // create new image with header sf::Image imgPattern; imgPattern.create( dimWidth, dimHeight, sf::Color(0,0,0)); imgPattern.copy( imgHeader, 0, 0 ); // write all pixels unsigned int x = 0; unsigned int y = 16; for ( unsigned int index=0; index<argCells.size(); ++index ) { // gen colours sf::Color colX, colY, colType; colX = int_to_pixel( argCells[index].x ); colY = int_to_pixel( argCells[index].y ); colType = int_to_pixel( argCells[index].type ); // write to image imgPattern.setPixel( x, y, colX ); increment(x,y); imgPattern.setPixel( x, y, colY ); increment(x,y); imgPattern.setPixel( x, y, colType ); increment(x,y); // output colours used out("[" + std::to_string(index) + "] "); out("X: ( " + std::to_string(colX.r) + ", " + std::to_string(colX.g) + ", " + std::to_string(colX.b) + ", " + std::to_string(colX.a) + " )\n"); out("[" + std::to_string(index) + "] "); out("X: ( " + std::to_string(colY.r) + ", " + std::to_string(colY.g) + ", " + std::to_string(colY.b) + ", " + std::to_string(colY.a) + " )\n"); out("[" + std::to_string(index) + "] "); out("X: ( " + std::to_string(colType.r) + ", " + std::to_string(colType.g) + ", " + std::to_string(colType.b) + ", " + std::to_string(colType.a) + " )\n"); } // save image imgPattern.saveToFile( argFilename ); return true; } sf::Color int_to_pixel( int number ) { // code number to look better as an image number += GG_INT_CODE; // convert to pixel int r_single = number & 0xff000000; int g_single = number & 0x00ff0000; int b_single = number & 0x0000ff00; int a_single = number & 0x000000ff; int r = r_single >> 24; int g = g_single >> 16; int b = b_single >> 8; int a = a_single; sf::Color colour((unsigned char)r,(unsigned char)g,(unsigned char)b,(unsigned char)a); return colour; } int pixel_to_int( sf::Color colour, bool swap ) { int r = (int) colour.r; int g = (int) colour.g; int b = (int) colour.b; int a = (int) colour.a; int number = r << 24; number = number | g << 16; number = number | b << 8; number = number | a; if ( swap ) { number = (int)_byteswap_ulong( number ); } // decode number -= GG_INT_CODE; return number; } void increment( unsigned int& x, unsigned int& y ) { ++x; if ( x >= 128 ) { x = 0; ++y; } }
  8. I'm trying to write a function that takes an std::vector of game objects and saves them all as an image.   The game I'm making revolves around placing game objects in a specific way and I wanted users to be able to share different placements easily. Since there won't be much data, I decided that for every game object, each of it's applicable data members would be converted to a colour, and saved in the image as a pixel. To load game states my game would open the image and read each pixel from left to right, top to bottom and re-construct each game object.   The bit that I'm a little stuck on is how to convert a number to a colour, and back again. For example the x coordinate of a specific game object could be 42, I need a way to take 42, and convert it to 4 individual bytes (r,g,b,a).   I've realised that this, by itself, isn't that difficult. But I was wondering If I need to take the platform's integer size and endianness into consideration when I'm building this? And If so what would be a good way to deal with all that?   Thanks.
  9.   Yep, you're right. Include guards are one of the features I hadn't got around to implementing yet.   I kinda knew this was a bad idea, hence me posting this in coding horrors  Still, I've learned some good little bits from the posts here, thanks everyone.   I've decided to finish this and use it for the project that I made it for. Just for the fun of it. Who knows, I might learn something more..  But yeah never again -.-
  10. Oh well I had fun...   EDIT: And I forgot to close the file afterwards -.-
  11. So I was learning and messing around with javascript and html5 canvas when I found out that javascript has no include or import. you either have to include extra files through the html or do some funky asynchronous loading or get Jquery to do it for you.   So a few hours later I produce this: # # command line utility that takes a javascript file (any text file really) # as input, goes though it and replaces #include with # the appropriate js files. Also minimises the resulting # large file and saves it. # minimising and saving not yet implemented # usage: file.js # options: # -m minimised the resulting file. # -o specify output filename # NOTE - none of these implemented yet import sys import argparse # handle arguments parser = argparse.ArgumentParser( description="update this later pls" ) parser.add_argument( "filename" ) parser.add_argument( "-m", "--minimise", help="minimises the resultant file", action="store_true" ) parser.add_argument( "-o", "--output", help="filename of the resultant file" ) args = parser.parse_args() # set options optInputfile = args.filename optMinimise = True if args.minimise else False optOutputfile = args.output if args.output else "COMPILED_" + optInputfile # extracts the next filename in double or single quotes from a position def extract_filename ( string, position ): curPos = position curString = "" done = False inName = False while not done: if not inName: if string[curPos] == '"' or string[curPos] == "'": inName = True else: if string[curPos] != '"' and string[curPos] != "'": curString += string[curPos] else: done = True curPos += 1 if curPos-position >= 40: # probably a missing " or ' print( "COMPILE ERROR: #include filename too long. You've probably missed a ' or \"" ) break return curString # replaces everything with spaces in 'string' from 'position' until 2 ' or " are found def replace_include_line ( string, position ): curPos = position done = False quoteCount = 0 curLen = 0 while not done: curLen += 1 if string[curPos] == '"' or string[curPos] == "'": quoteCount += 1 if quoteCount >= 2: done = True curPos += 1 newString = string[:position-1] + string[position+curLen:] return newString # inserts string 'b' into string 'a' at position 'pos' def string_insert ( a, b, pos ): return a[:pos] + b + a[pos:] # recursive file parsing function def parse_file ( filename ): file = open( filename, 'r' ) fileStr = count = 0 pos = 0 while True: # find the next include pos = fileStr.find( "#include", pos ) if pos == -1: break # read the filename includeFilename = extract_filename( fileStr, pos ) # remove the #include from file fileStr = replace_include_line( fileStr, pos ) # insert the included file fileStr = string_insert( fileStr, parse_file( includeFilename ), pos ) # woah here's where the magic happens return fileStr print( parse_file( optInputfile ) ) It's a command line utility written in Python that parses text files for #include statements and inserts the appropriate files. It's far from elegant and most of the extra features aren't done yet, at the moment it just prints out the resulting superfile. But the basic functionality of recursively replacing #include with the contents of a file is done.   So in any file that's parsed you can write: #include "file.ext" Thoughts? Am I crazy? Wasting my time? Has this been done/solved before?
  12.   I agree with you but there are some alarm bells going off, I fear your advice may be misunderstood.   Notice what I highlighted. If the person is learning game programming, then yeah, I completely agree. But it seems to me like this advice already assumes the person knows a language already. If a person wants to learn a new language, say C++, then I feel that they should get some console/terminal experience before they move on.   Better learn how to #include and cout before you try linking graphics libraries and allocating memory.
  13.   All clear now! Thank you very much.
  14. Quick question, if I declare this pointer to a vector: std::vector<int>* myVector = &someOtherVector; Why does this not work? *myVector[0] = 42; And why does this work? (*myVector)[0] = 42; Thanks.
  15.   It builds! (And runs without any problems first try)   Thanks for all your help. I'll try to commit all this to memory to get out of similar situations in the future.
  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. 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!