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!


fastcall22

Member Since 04 Feb 2008
Offline Last Active Today, 01:42 AM

#5101904 How do you 'learn' a programming language?

Posted by fastcall22 on 16 October 2013 - 01:25 PM

not advancing until you understand the concepts

... and completing every exercise at the end of every chapter and redoing every exercise once you learned how to build it better.


#5099591 Loading Graphics with the help of extern files

Posted by fastcall22 on 08 October 2013 - 09:26 AM

The only problem is: I still have to declare the enum.

No, you only think you do.

Try using an associative container such as map or unordered_map and refer to the textures as strings:
// load resources
map<string,texture> atlas;
for each ( line in file ) {
    atlas[line.name] = texture::load(line.uri);
}

// get resource
const texture& imgTree = atlas["TEXTURE_TREE"];



#5098560 Attaching a std::shared_ptr to a void* member

Posted by fastcall22 on 03 October 2013 - 02:02 PM

std::shared_ptr<PeerData> spPeer((PeerData*)netevent.peer->data);


When you construct a shared pointer from a raw pointer, the shared pointer assumes ownership of the pointer. See the sample code below:
#include <memory>

int main(int,char*[]) {
    typedef std::shared_ptr<int> SharedInt;
    void* ptr;
    
    SharedInt a(new int);
    /*1*/ {
        ptr = &(*a);
    }
    
    /*2*/ {
        SharedInt b( (int*)ptr );
        *b = 5;
    } // b is destroyed, ptr is deleted, and a is dangling
} // Crash once a is destroyed



#5098528 Attaching a std::shared_ptr to a void* member

Posted by fastcall22 on 03 October 2013 - 01:06 PM

The peer->data member is never sent anywhere. It is just a pointer in the peer object where the application can put data.


Why are you storing things in a packet and not sending it?

Taking a closer look, the PeerData is deleted when the case scope ends, because the shared pointer that owns it falls out of scope and there no other shared pointers referencing that data. Attempting to read from peer->data later segfaults, since the data has already been freed.


#5098511 Attaching a std::shared_ptr to a void* member

Posted by fastcall22 on 03 October 2013 - 12:32 PM

I want to be able to create a shared_ptr and set it to netevent.peer->data. When I recieve packets I can cast netevent.peer->data back to a shared_ptr and get the members out. But all the tricks i've tried so far has crashed the application.


Okay, there are several major problems here:
First, you cannot send a pointer to an object. As an example, imagine I sold an item to you. Instead of packaging the item in a box and shipping it, I mail you the location of where the item is in my house. What good does knowing where the item is in my house help you? This is essentially sending a pointer to an object. Instead, you'll need to package the object (via serialization) and send it out. If you need to refer to this object over the network later, use a unique identifier (such as an integer) to refer to the object on both machines.

Second, you cannot send a shared pointer as it is over the network, for the same reasons given above. A shared pointer is basically an integer keeping track of the number of references, and the object its pointing to. If you were to somehow send both the object and the reference count, how do the machines synchronize the reference count? Why does one machine care about the other's memory management?


#5095846 Returning a Char Array from function in C

Posted by fastcall22 on 21 September 2013 - 08:15 PM

ಠ_ಠ


#5094588 release code

Posted by fastcall22 on 16 September 2013 - 11:37 PM

Select the configuration from the drop down near the menu bar:
ReleaseDebugMode.jpg
(Random image pulled from google image search)

Then compile!




#5094469 nested for -- regex

Posted by fastcall22 on 16 September 2013 - 10:01 AM

Cannot figure it out. Any help? Thanks

#include <string>
#include <iostream>

using namespace std;

int main() {
    string str = "world";
    string expr = "Hello, (str)! Expression: (4*2./20)";

    cout << str << endl;
    cout << expr << endl;
}
Outputs:
world
Hello, (str)! Expression: (4*2./20)


And does not output:
world
Hello, world! Expression: 0.4



On the other hand, the following code:
#include <iostream>
#include <sstream>
#include <string>

using namespace std;

int main() {
    string str = "world";
    ostringstream temp;
    temp << "Hello, " << str << "! Expression: " << (4*2./20);
    string expr = temp.str();

    cout << str << endl;
    cout << expr << endl;
}
... does output:
world
Hello, world! Expression: 0.4



#5093420 Problem with C++ and visual studio 2010

Posted by fastcall22 on 11 September 2013 - 05:21 PM

'hash' : ambiguous symbol


There are two classes named "hash": std::hash, a provided by the C++11 standard which may have been included by consequence in cstdlib or string; and hash, defined in your hash.h. The Visual Studio 2010 C++ compiler introduced some support for C++11 (the newest version of C++). This would explain why it would compile for Visual Studio 2008 and not 2010.

To fix, either rename your class, or stop using a "using namespace" in a header and properly qualify cout, string, and etc with "std::".


#5092822 Simple C++ Design Question for Numerology App

Posted by fastcall22 on 09 September 2013 - 03:57 PM

-Entire birth day condensed down to one digit.

-Date of birth condensed to one digit.

-Month and date of birth condensed to one digit.

...
What does this even mean?

Perhaps a few examples might clear things up...


#5090423 c++ polymorphisam deleting a object, constructor concearn

Posted by fastcall22 on 30 August 2013 - 01:12 PM

Here a leak of one int size will occur?

No.

If so, how would the base class know to call its pointing to deconstructor?

Make the base class destructor virtual.


In C++, it is considered good practice to use a virtual destructor:
class Base {
public:
    virtual ~Base() {
    }

private:
    bool mTest;
};

class Derived : public Base {
public:
    Derived() {
        mData = new int[1000];
    }

    ~Derived() {
        delete[] mData;
    }

private:
    int* mData;
    double mOtherData;
};

int main() {
    Base* p = new Derived;
    delete p;
}
If Base::~Base were not virtual, then Derived::~Derived would not be called, and the memory for its mData would not be released. The members for Derived (the pointer mData, mOtherData, and mTest) would not leak in either case, because the members reside inside the memory block allocated for Derived. Your C++ runtime and operating system keep track of allocated blocks, so the runtime/operating system is smart enough to know the size of a block given only the pointer to its address. You can technically do something like this:
void magic( void* ptr ) {
    // Neither ~Base() or ~Derived() will be called because we won't know what type ptr is!
    // But the memory block will be freed regardless.
    delete ptr;
}

int main() {
    magic( (void*)(new Derived) );
}
Whether it is morally acceptable to do or not is another question...


#5087722 Amusing glitch gallery

Posted by fastcall22 on 20 August 2013 - 08:23 PM

This is a cube.
obichamcube2.png

The camera was inside it. I learned about the importance of the near plane that day.
It got better though.

obichamcubecolor.png

Oops, range checking fail.


(Plug) Here are more screenies of its successor:
http://imageshack.us/a/img231/9179/fastbread.png
http://imageshack.us/a/img62/8483/torus.png



EDIT:
I don't remember imageshack being so hard to use...



#5085209 VSync messing with QPC() timing?

Posted by fastcall22 on 12 August 2013 - 09:41 AM

Are you saying VSync stalls the rendering function until the display has finished drawing the previously presented frame? So the "while(true)" only fires once every 16.(6) ms?

Yes, that is what Hodgman is saying; vsync blocks until the display is ready for another frame.

I thought it would make quite a mess of things, so I've decided to use a fixed step size.

Then you might find Fix Your Timestep an interesting read.


#5084233 Duplicated Code

Posted by fastcall22 on 08 August 2013 - 02:23 PM

The correct answer is to refactor four boolean variables into a single integer indicating which form is active:

private int currentForm = 0; // "firstForm" active
private int countForm = 4;
 
public void onKeyPressed( Event e ) {
    KeyEvent k = e.getkeyCode();
    if ( k == KeyEvent.VK_RIGHT )
        currentForm = (currentForm+1)%countForm; // Modulus wraps "fourthForm" to "firstForm"
    else if ( k == KeyEvent.VK_LEFT )
        currentForm = (currentForm+countForm-1)%countForm; // Adition and modulus wraps "firstForm to "fourthForm"
}



#5065559 VC++ 2010 preprocessor directives don't seem to be updating

Posted by fastcall22 on 28 May 2013 - 12:35 PM

You're being retarded. :P

Remember that each cpp file (translation unit) is compiled separately from the rest. Therefore, only EG_DEBUG_MODE will be defined for main.cpp. You'll need to define EG_DEBUG_MODE in each cpp file, or add it to your project's preprocessor settings.




PARTNERS