Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 28 Oct 2012
Offline Last Active Mar 08 2014 06:52 PM

#5106673 Template content manager in c++

Posted by on 03 November 2013 - 07:27 AM

This can surely be done in a simple manner.

// Base class for all resources
class Resource

    virtual void Load(const std::string & sourcePath);
    virtual void Unload();

// Resource manager
class ResourceManager
    std::map< std::string, Resource * > m_Resources;

    template< class T >
    T * GetResource(const std::string & sourcePath)
        if(m_Resources[sourcePath] != 0)
            return dynamic_cast< T * >(m_Resources[sourcePath]);

        T * newT = new T();

        m_Resources[sourcePath] = newT;
        return newT;

So what happens is when you use the resource manager to get a resource.
It will first check to see if we have already loaded the resource and try to return that one.
If we havent loaded it, load it and store it so we later on can get it.

void Engine::Init()
    ResourceManager * resourceManager = GetResourceManager();
    TextureResource * textureResource = resourceManager->GetResource< TextureResource  >( "path/to/texture.png" );

As you can see above, its very straight forward how to use it smile.png

What is also worth mentioning is that in this example im using std::map to store the resources. You should really experiment with different types of storage to find whats best for your needs. 


That dynamic cast is unnecessary IMO. If you know the type, just use a static_cast, no need to check if it's valid. If it's not then you'll get a runtime error and it's the programmer's fault, not the user's. Also you may want to consider using a std::shared_ptr rather than a raw pointer, or returning by reference and having a Release method.

#5099301 Some pointers please (Entity component systems).

Posted by on 07 October 2013 - 03:39 AM

Pro tip: If you want more people to look at your code, host it on GitHub or BitBucket. That way they can browse it online instead of downloading it completely. I recommend you version control all your projects regardless.


You should really put #pragma once at the top of your code, that way it doesn't get in the way and you can read the necessary code. This is more of a convention, but still. Also, I believe #ifndef/#define/#endif is more portable, so if you want portability use that instead of #pragma once. Although I think most modern compilers support the use of #pragma once.


In your InputComponent, I personally think boolean(*handleInput)(Entity* entity) should be typedef'd, as if you change it, you'll have to change it twice (this can cause annoying compiler errors) and plus you get more readability if you typedef it. e.g. typedef boolean(*HandleInputFn)(Entity* entity). Also what the f*ck is boolean? Did you typedef bool or is this a C++ .NET primitive? Just use bool.


Use initializer lists, don't assign in the constructor. e.g. in your PositionComponent

struct PositionComponent
     int X;
     int Y;
     PositionComponent(int x,int y) : X(x), Y(y) { /* this should be empty */}

And god dammit, use smart pointers. There is more than likely going to be some leaks in your program (I'm not going to check if there is, because I can't be bothered looking for any). Raw pointers should only be used for non-owning objects (i.e. objects that you don't allocate with new/delete).


Also, try to use the stack more often, this isn't Java. In Main.cpp you're allocating objects with new for no goddamn reason. A stack allocation could suffice. For example, your WorldState and GameNode objects are allocated with new when they don't need to be. WHY? You don't even delete them (good practice to, even known they will be deleted on exit of the program, but still), which brings me to the previous point I made: USE SMART POINTERS. 

#5099298 C++ Libraries - Where to find a list ?

Posted by on 07 October 2013 - 03:16 AM

The first place to start is the Standard C++ Library, sometimes (erroneously) referred to as the STL*. This contains lots of libraries relating to containers (lists, arrays, vectors, etc), strings (not a part of the language, but a library) and various other stuff (input/output streams, algorithms, etc).

+1 for linking to en.cppreference instead of cplusplus.com. @OP Don't use cplusplus.com for a reference on the standard library (for one, it's not official documentation).


Also, OP, if you're looking for a particular library for C++ that does something the standard library (or even boost) doesn't offer, e.g. a rendering library, then google it. Google is your friend, you could also try to look on GitHub or BitBucket if you can't find it on google or another search engine.

#5097774 Component based system

Posted by on 30 September 2013 - 02:17 AM

You can check out my library too if you please. https://github.com/miguelishawt/anax

Check the wiki for a basic tutorial on how to use it.

#5094174 CS Degree - Is it worth it?

Posted by on 15 September 2013 - 02:25 AM

Okay, well this blew up. I'm definitely going to do a degree, especially with all the points made.

#5092250 CS Degree - Is it worth it?

Posted by on 07 September 2013 - 03:41 AM

I'm curious to know if I should spend 3 years of my life to get a CS degree. I'm 17 years old, turning 18, in my last year of high school (grade 12) and willing to apply to a university. I already know how to program, in various languages (here's my GitHub and BitBucket if you want to look at some of my code [I haven't finished/update some projects as of yet]). Obviously, I don't know everything about programming*, but I would say I know quite a lot. Would I be "twiddling my thumbs" for the first couple of years whilst doing a CS degree (i.e. be bored, not learn anything new)?


I'm considering between choosing CS/CS(Advanced), Bachelor of Engineering (Electrical and Electronic) with Bachelor of Mathematical and Computer Sciences, and Bachelor of Engineering (Computer Systems) with Bachelor of Mathematical and Computer Sciences. I'm not sure if the last two degrees would have all the content as a CS degree (it's a double degree; yet it has three degrees(?)).


Can I get opinions on why getting a CS degree would be beneficial to me? The only reasons I can think of is: (1) More knowledge, (2) Good environment for learning at uni (topics I'm unsure/inexperienced in or don't know about*) and (3) Looks good on a resume. Also, could I get a job (programming related) whilst studying for my CS degree?

*Here's the topics I do not currently know (there is more than likely more, but here's the main ones I think I should learn and seem interesting):

  • Assembly programming

  • Operating Systems

Here's the languages I know:

  • C

  • C++

  • C#

  • Java

  • JavaScript

  • Anything really C-like

  • VB (and TI-Basic)

  • Had some little experience with Lua

  • HTML/CSS (Not sure if these really count, but I still know them)

#5074055 Where to go next?

Posted by on 29 June 2013 - 08:59 PM

Reading/grasping the idea isn't the whole picture, you need experience. Make little toy programs in C++ first, just using console i/o. Once you have experience and you're quite comfortable with programming, then I recommend you to move on.


Jumping straight into OpenGL wouldn't be so smart. It'd be easier if you have somewhat of a computer graphics background (e.g. you know Maya/Cinema4D/Blender/3DS Max, or even After Effects). I'd start at learning Allegro, SDL or SFML first and then moving from there. Unless of course you want to really learn OpenGL and not concentrate on making games. After that you could learn OpenGL, or even jump to a rendering engine, such as Ogre3D. It's all up to you.

#5074054 SDL Key/Mouse Simplified Intput Class

Posted by on 29 June 2013 - 08:53 PM

Couple of quick tips:

//different types of possible key states
#define KEY_NONE -1
#define KEY_PRESSED 0
#define KEY_RELEASED 1
#define KEY_HOLDING 2

Why are you using #define? #define is evil and should be avoided. Use an enum or const, e.g.:

enum KeyState
    KEY_NONE = -1,
    KEY_PRESSED = 0,

I believe this:

#include "stdafx.h"
Makes your code less portable, although a person could delete it/comment it out, but yeah.
	return true;
	return false;

Could simply be:


Also, KEYS_STATE2 and KEY_STATES are not not a very good names IMO. Perhaps make them more descriptive? KEYS_PRESSED_STATES and KEYS_HELD_STATES? BTW, you could probably use a struct/array to make it into one variable.


There's also a lot of some-what duplicate code, the only thing that's different is the variables, this could be avoided.

#5024636 sizeof() giving strange results...

Posted by on 23 January 2013 - 02:28 AM

Shouldn't it be:

It's rather trivial in this case; nothing will be harmed by assigning three floats to themselves. Past attempting to maintain the invariants of the class, there is little other reason to check to see if you are assigning to yourself for a class that contains nothing but primitive types.




If you don't need to explicitly test for self-assignment, for example, if your code works correctly (even if slowly) in the case of self-assignment, then do not put an if test in your assignment operator just to make the self-assignment case fast. The reason is simple: self-assignment is almost always rare, so it merely needs to be correct - it does not need to be efficient. Adding the unnecessary if statement would make a rare case faster by adding an extra conditional-branch to the normal case, punishing the many to benefit the few.

In other words, don't stick one in just to do it. Do it for a provable reason that isn't speed.


Ah, my bad.

#5024617 sizeof() giving strange results...

Posted by on 23 January 2013 - 12:54 AM

The compiler is probably getting confused if you inline it and it isn't available to all translation units (i.e. you need to define it in the class declaration or in the header)?

haha yes that solved it... stupid me for not seeing it myself
i just removed the 'inline' keyword. took me some time googling to fully understand whats going on, but i get it now smile.png

this can be closed i guess

thanks a plenty to everyone again, <3 this forum



EDIT: i also moved to returning a reference for assignments:

Vector3 &Vector3::operator=(const Vector3 &v)
	x = v.x;
	y = v.y; 
	z = v.z;
	return *this;

i assume this the proper way to do this?

Shouldn't it be:



Vector3& Vector3::operator=(const Vector3& v)
       if(this != &v)
             x = v.x;
             y = v.y;
             z = v.z;
       return *this;


Oh and why not use a pre-made math library such as GLM?

#5023743 Fullscreen functionality

Posted by on 20 January 2013 - 09:05 PM

When you're calling al_set_new_display_flags (assuming you are), are you specifying the ALLEGRO_FULLSCREEN flag?



al_set_new_display_flags(someFlags  | ALLEGRO_FULLSCREEN);

Where someFlags is whatever flags you want other than fullscreen, such as ALLEGRO_OPENGL, ALLEGRO_DIRECT3D, or ALLEGRO_OPENGL_3_0.


By the way... google and documentation is your friend smile.png.



I found this question on allegro 5 and creating a fullscreen resolution, which has the same resolution as the user's monitor, which you might be interested in.

#5022778 Losing interest in game development...

Posted by on 17 January 2013 - 10:59 PM

We have all experienced feelings such as yours before. At this very moment, I am angered with the level of obfuscation that seems to have arisen in my networking codebase. So, I am removing it. From the ground up. A good point was raised earlier that one should try and refactor wherever possible. Yet, coming at this from the other direction, sometimes it is best to start again. I just purchased a book on software design. Normally, I would not have considered such a step, yet what limits my creativity the most is my constant fear of implementing something the "wrong" way. But, in many cases there isn't a wrong way. Get something working, learn from it, and if needs be redesign it with the newer functionality in mind. The point about a "buddy". I think it is one of, if not the MOST important point.

It's so hard to find someone.




I have an artist (which hopefully wont bail)

Ah yes one of my biggest problems.


When I get bored I write DOS games. They may seem like a horrible waste of time being so out of date and all that. However when you write one of them you learn a lot about the hardware you're using and there great fun. You don't have to worry about your code getting to complex because a really complicated program wont run on most dos machines. They are usually quick and almost every line is important and you learn a ton about optimization. So next time you feel like this I would get started on one of those and like me you will wish modern programming was like this.

Well hopefully he wont, but I dunno. Lately I've been frustrated in the way I'm doing things in my current game. I've got all boiler plate code, and a screen showing up currently. Nothing else has been done really. Hopefully I'll have something on the screen by the end of the week.

#5022170 Losing interest in game development...

Posted by on 16 January 2013 - 07:50 AM

Do you have any programming buddies?


Actually, EmployeeNumber8 makes an awesome point. I'm noticing that my productivity and motivation stay at much higher average levels from working in a team - I also have a bunch of other gamedevs on twitter and we keep each other on track. You can lone-wolf it if you desire, but you'll be thankful for at least a little bit of a support network. Helps to wave off the depression...

Ah I wish I had a programming buddy. But, I'm currently making a game at the moment and I have an artist (which hopefully wont bail).

#5021709 Losing interest in game development...

Posted by on 15 January 2013 - 01:21 AM

This feeling is normal. Complete something to be proud of yourself, this just happens when you see your projects working.

You are young, don't think you are failling. The great things come with time.



You have to remember that code is never perfect. All code sucks. You can't let that stop you from writing it. This is what programming is.



Seconded.  As well, I'd like to add my favorite concept, which keeps me motivated (and productive): "First make it work, then you can make it work better."

That first time it works, even if the algorithm is ugly, brute-force garbage, and the framerate is an ungodly 5 fps, is better than se- actually, no, I'd still rather have...anyway: it's an addictive high.  It keeps you motivated, instead of almost completing it 5 times and never seeing anything for your work.

Great advice actually, makes me want to code, plan or something. But... gotta do the homework. 


I know I'm leaving half of the stuff out that I could put it in and that annoys me, e.g. texture options (whether to clamp or repeat the texture) or abstracting OpenGL specific stuff (such as Texture classes w/o GLuint directly in them, more data oriented of a design). I REALISE that I could use another engine, like Ogre3D, or whatever, but whenever I do try to use it, I just hate the way that it enforces you to do things their way (plus I find it somewhat awkward programming with someone else's code, unless I've studied it and frameworks like Ogre3d takes awhile to study). But anyway, my game is 2D so I thought why not use OpenGL by itself (probably going to use legacy code because I haven't really learnt about shaders). Another thing, I've made my own entity system, which I know I can really improve but it just gets all out of hand when I try to.
The above indicates that you might be succumbing to the dark side. Don't succumb to the dark side.

Succumbing to the dark side involves slowly losing grip on software projects you are heavily emotionally invested in and leaving them in a constant unfinished state because you are too concerned with how much better your code could have been if only you had made this one decision at the very beginning, or if only you add this one clever feature to your low-level architecture and then refactor your entire codebase to use it.

You have to remember that code is never perfect. All code sucks. You can't let that stop you from writing it. This is what programming is.

Yeah I realise code is never perfect, but I just want to make my code perfect. 

#5021273 Losing interest in game development...

Posted by on 13 January 2013 - 10:45 PM

This is normal. I could have said the same thing at one point. I still stick to my comfort zone. I switched from C/C++ to java only because of direct PNG support. and I couldn't for the life of me figure out how to load a png with c++.


I never learned 3d and cannot really do the math for it so I am stuck in 2d. OpenGL DX? Pft. Guy I been programming for fun for longer than you've been alive and I still don't know that junk.

I know OpenGL (the Fixed Function Pipeline; i.e. legacy code). I know most things for OpenGL except: lighting, shaders, bump mapping, multi-texturing and perhaps a few more things here and there. Other than that, I'm comfortable with OpenGL, just I think I try to abstract OpenGL too much than what I should... :l