How to make a game

Pages: 12
well like you said animations might need drawing and tht other thing i mentioned is movement interpolation. Lets say an enemy moves very quickly, lets say it has a speed of 10 so each update loop it moves 10 spaces in one direction. Because of this leap in position the movement will look very choppy, but if we redraw as often as we can we can draw the enemy at positions in between its start and end position from one update to the next, and make the movement look much more smooth. But sorry for calling you out in the first place as it was me who missed your sleep call =P for a simple game with no animations your loop will work fine
Yes, its just a super simple example on what a game loop should include. Sorry, I didn't see your post about movement interpolation. That would certainly be a valid situation to render more than you update.
why not add openGL and directX to the libraries, seeing as though two are the most widely used amongst proffesionals?
Just did :)
How about sample code for a resource manager? This is one of the most important system functions of a game, in my opinion.

It could a template class or use a base class for the resource type. I have some code which I can share if necessary.
Last edited on
Could you please post some code then? I'd like to see how you do it because my methods aren't the best, hehe. I won't have time to update the article for a while though.
I'm sure mine aren't either. Here is my code - it was written recently so there may still be a few bugs in it. Also, feel free to let me know if you think it's too long for this thread - in that case I could just put the class declaration.

I can also provide usage examples if you want :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/***************************************************************
 ResourceManagerB.hpp - Generic template resource manager				
									
 (C) Alexander Thorne (SFML Coder) 2011	
 http://sfmlcoder.wordpress.com/	
		
 Manages loading and unloading of a resource type specified by a
 template argument.

****************************************************************/

#pragma once
#ifndef AVS_ICANOS_SYSTEM_RESOURCEMANAGERB_HPP
#define AVS_ICANOS_SYSTEM_RESOURCEMANAGERB_HPP

#include <map>
#include <string>
#include <exception>

typedef const std::string URI;

// exceptions
namespace Exceptions {

	// thrown if user requests a resource URI not present in the manager's list
	class URINotFound : public std::runtime_error 
	{ 
	public: 
		URINotFound(const std::string& Message = "The specified URI was not found in the resource index.")
			: runtime_error(Message) { } 
	};

	// thrown if a resource allocation fails
	class BadResourceAllocation : public std::runtime_error {
	public: 
		BadResourceAllocation(const std::string& Message = "Failed to allocate memory for resource.")
			: runtime_error(Message) {}
	};
}

template <class Resource> class ResourceManagerB {
	typedef std::pair<URI, Resource*> ResourcePair;
	typedef std::map<URI, Resource*> ResourceList;

	// the list of the manager's resources
	ResourceList Resources;
public:
	~ResourceManagerB() { UnloadAll(); }

	// Load a resource with the specified URI
	// the URI could represent, e.g, a filename
	URI& Load(URI& Uri);
	// unload a resource with the specified URI
	void Unload(URI& Uri);
	// unload all resources
	void UnloadAll();

	// get a pointer to a resource
	Resource* GetPtr(URI& Uri);
	// get a reference to a resource
	Resource& Get(URI& Uri);
};

template <class Resource>
URI& ResourceManagerB<Resource>::Load(URI& Uri)
{
	// check if resource URI is already in list
	// and if it is, we do no more
	if (Resources.find(Uri) == Resources.end())
	{
		// try to allocate the resource
		// NB: if the Resource template argument does not have a
		// constructor accepting a const std::std::string, then this
		// line will cause a compiler error
		Resource* temp = new (std::nothrow) Resource(Uri);
		// check if the resource failed to be allocated
		// std::nothrow means that if allocation failed
		// temp will be 0
		if (!temp)
			throw Exceptions::BadResourceAllocation();
		// add the resource and it's URI to the manager's list
		Resources.insert(ResourcePair(Uri, temp));
	}
	return Uri;
}

template <class Resource>
void ResourceManagerB<Resource>::Unload(URI& Uri)
{
	// try to find the specified URI in the list
	ResourceList::const_iterator itr = Resources.find(Uri);
	// if it is found...
	if (itr != Resources.end())
	{
		// ... deallocate it
		delete itr->second;
		// then remove it from the list
		Resources.erase(Uri);
	}
}

template <class Resource>
void ResourceManagerB<Resource>::UnloadAll()
{
	// iterate through every element of the resource list
	ResourceList::iterator itr;
	for (itr = Resources.begin(); itr != Resources.end(); itr++)
		// delete each resource
		delete itr->second;
	// finally, clear the list
	Resources.clear();
}

template <class Resource>
Resource* ResourceManagerB<Resource>::GetPtr(URI& Uri)
{
	// find the specified URI in the list
	ResourceList::const_iterator itr;
	// if it is there...
	if ((itr = Resources.find(Uri)) != Resources.end())
		// ... return a pointer to the corresponding resource
		return itr->second;
	// ... else return 0
	return 0;
}

template <class Resource>
Resource& ResourceManagerB<Resource>::Get(URI& Uri)
{
	// get a pointer to the resource
	Resource* temp = GetPtr(Uri);
	// if the resource was found...
	if (temp)
		// ... dereference the pointer to return a reference
		// to the resource
		return *temp;
	else
		// ... else throw an exception to notify the caller that
		// the resource was not found
		throw Exceptions::URINotFound();
}
#endif 
Last edited on
Thanks! I don't have enough room left in the article for your example, so I will just tell people where to find it :)
In hindsight, you probably should have reserved the first three posts or so.
We could do with a forum section for "draft articles". That way people could pool ideas and code and then it could be assembled into a coherent article at the end.

Of course we could just make draft articles in the articles section, but it would make rather a mess...
I shouldve, but i didnt think of it back when I posted this.
just remake it hmm?
We used to have a section for "proper" articles outside of the forum. In that section you could post much longer articles and people couldn't comment on them (unless they sent you a PM), and you could use HTML tags. I don't know why, but now it is gone and the link just redirects to the forum one. There were some good articles there... :(
I suppose I'll remake then, but I won't have time for a few days. Too bad about that articles section :(
Remade the article with room to spare. Find it here: http://cplusplus.com/forum/articles/43175/
Topic archived. No new replies allowed.
Pages: 12