Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


Khatharr

Member Since 24 Apr 2010
Offline Last Active Private

#5237167 Can't convert array of strings to char array

Posted by Khatharr on 27 June 2015 - 04:48 PM

Oh, and apparently we all forgot to mention it, but you don't have to add the trailing zero to string literals. If you use "double quotes" then it appends a trailing zero automatically. If you use 'single quotes' then it doesn't. Also, I suspect part of the bug you were running into had to do with trying to fit things like "Open\0" (a total of 6 characters) into an array of 4 chars.




#5237020 Can't convert array of strings to char array

Posted by Khatharr on 26 June 2015 - 08:05 PM

Generally you don't want to mix STL stuff with stdlib stuff. In this case there's really no reason to do what you're trying to do. If you want to get a C-style string from a std::string, you just use the .c_str() member function. If you want to stream a std::string to std::cout, you just do it. There's no conversion necessary.

 

Concerning the passing of static arrays into functions... Which then have incorrect methods attempting to determine the length of the array...

 

Firstly, when passing a complex or potentially large object into a function, pass it by reference instead of by value:

 

void doStuff(std::string& myString) { /*stuff*/ }

 

If you want to prevent it from being altered within the function then make it a const reference:

 

void doStuff(const std::string& myString) { /*stuff*/ }

 

When you pass by value, the thing you pass gets copied into the function. When you pass by reference, only the refrence gets passed in, which is much smaller, and doesn't invoke any copy behaviors.

 

Secondly, if you want to pass around arrays of variable length, then use std::vector.

 

void doStuff(const std::vector<std::string>& myVectorOfStrings) {
    for(auto& str : myVectorOfStrings) { //std::vector supports the new for-each loop type
        std::cout << str << "\n";
    }
 
    for(int i = 0; i < myVectorOfStrings.size(); i++) { //it's also aware of its own size (in terms of member count)
        std::cout << myVectorOfStrings[i] << "\n"; //and can be indexed like an array
    }
}

 

Keep at it. You'll get there.




#5230510 A whole lot of constants.... good or terrible? :/ (c++)

Posted by Khatharr on 22 May 2015 - 06:14 PM

Note that if you declare an enum or a static member within the public section of a class, you can access it externally:

class Foo {
public:
  enum Bar {
    BAZ = 12
  };
};

void someFunctionElsewhere() {
  Foo::Bar temp;
  temp = Foo::BAZ;
}

This can be useful if you want to have a public member function that takes an enum argument:

class Text {
public:
  enum HorizontalAlignment {
    HALIGN_LEFT,
    HALIGN_CENTER,
    HALIGN_RIGHT
  };
  
  void setHorizonalAlign(HorizontalAlignment hAlign);
};

void someFunctionElsewhere() {
  Text obj;
  obj.setHorizonalAlign(Text::HALIGN_CENTER);
}



#5228843 (Best Practice) Multithreading instantiation

Posted by Khatharr on 13 May 2015 - 02:32 PM

You could set up a messaging system and work with interfaces, or you could have another think about threading something that sounds like it doesn't want to be threaded.




#5228842 Make the camera follow the sprite

Posted by Khatharr on 13 May 2015 - 02:28 PM

Looking at that video, it keeps the line in screen space and simply converts the coords to world space while the line itself remains locked to the camera position.

 

If you have some differences in your implementation that make it more of a problem then just pin the camera while the mouse button is pressed.




#5228191 Help me get started with a "Hello World" game that uses a server?

Posted by Khatharr on 09 May 2015 - 11:30 PM


Secondly opening a port isn't inheritly harmful, i think you've been mislead in this regard. The problem isn't with the port being open, it is the software behind the port, if someone finds an exploit within your software that allows you to download malicious software and run it unknowingly, that is the issue. But very simple telnet like applications are going to be hard pressed to find such exploits, in addition it'd require an extremly talented hacker to not only find you, but somehow discover such a compromising bug in your server software as well.

 

Seconding this. Firewalls have become common, but they're really a sledgehammer approach to security. If you don't have an application running with a local socket bound to the port in question then incoming connections will simply fail. I've had my PC configured as my router's DMZ for a long time now and the worst that's happened is the occasional anonymous port sniff. There's nothing listening on my machine that's not been secured, so there's no real security risk. Where firewalls are actually useful is in notifying you when an application on your machine tries to bind a socket. If something is trying to listen for your connections without permission then you should take an interest in that ASAP.

 

That said, if you ever do consider going DMZ, you'll want to check for updates and security info first to make sure that your OS doesn't have any glaring security holes.

 

As for port forwarding, since you're doing active development, you'll know right away if there's a risk because a port can't be bound more than once at a time. If you have a program that you've written that's trying to listen on port 1234 and it complains that it can't bind the port then that probably means that some other program already has it bound. You'll want to check to make sure that whatever is binding the port is legitimate, and if so then just pick a different port number.




#5228190 Is it possible to declare single-bit-variables/struct?

Posted by Khatharr on 09 May 2015 - 11:09 PM

In c++ you can use bitfields http://www.tutorialspoint.com/cprogramming/c_bit_fields.htm but it is still impossible to allocate less then 1 byte and the process is still the same: processor fetches bytes and then gets access to bits.

 

std::vector<bool> is also specialized, although whether or not it actually optimizes the storage is compiler dependent.

http://www.cplusplus.com/reference/vector/vector-bool/




#5225727 What's next after knowing how to program?

Posted by Khatharr on 26 April 2015 - 05:49 PM

what are the C++ libraries that I would (eventually) need?

 
Start doing what you're going to do, and when you find a need for a library, ask about what options are available and how they compare. A lot of libs are in active development, and things can shift quickly if a new lib shows up and solves an old problem. If you sit around collecting everything then by the time you're ready to make use of it, you'll find that it's all become deprecated.




#5224119 Distance between OBB's

Posted by Khatharr on 17 April 2015 - 07:55 PM

I recently saw a video about optimal GJK. Let me see if I can dig it up for you, since you're looking at the algo...

 

Here we go. Mollyrocket in the house:

 

http://mollyrocket.com/849




#5224118 Why high level languages are slow

Posted by Khatharr on 17 April 2015 - 07:52 PM

Oh look, it's this thread again.




#5221763 Advanced unit steering (e.g minion movement in moba games)

Posted by Khatharr on 06 April 2015 - 09:03 PM

http://gamedevelopment.tutsplus.com/tutorials/understanding-steering-behaviors-collision-avoidance--gamedev-7777




#5221760 Two Questions - Willing to Trade experience on Tools and Business Side, for M...

Posted by Khatharr on 06 April 2015 - 08:56 PM

There's a series of getting-started tutorials on the official Android site.

 

https://developer.android.com/training/index.html

 

If you're using Eclipse then that's your starting point. If you want to use something like Unity I think you just build for Android from within a Unity project.




#5220844 c++ count lines in txt file and then read these lines without reopening a file

Posted by Khatharr on 01 April 2015 - 09:35 PM

std::vector<std::string> getLinesFromFile(const std::string& fname) {
  std::ifstream file(fname);
  if(!file) {throw std::runtime_error("getLinesFromFile() - Failed to open file.");}

  std::stringstream data;
  data << file.rdbuf();
  file.close();

  std::vector<std::string> lines;
  while(data) {
    lines.emplace_back();
    std::getline(data, lines.back());
  }
  lines.pop_back();

  return lines;
}



#5220809 c++ count lines in txt file and then read these lines without reopening a file

Posted by Khatharr on 01 April 2015 - 04:02 PM

std::ifstream is RAII.

 

But you should throw if the file has an error.

 

Anyway, wasn't this whole thing about performance? Why are you reading one line at a time from the disk?




#5220808 Anyone got any ideas where 4k a sec is coming from?

Posted by Khatharr on 01 April 2015 - 03:51 PM

If you're worried about leakage then download vld and test the program. Task manager is NOT an appropriate tool for measuring memory usage.






PARTNERS