Skip to content

Commit 8978af0

Browse files
committed
tiles: fov limited camera!
1 parent 1a01487 commit 8978af0

5 files changed

Lines changed: 259 additions & 148 deletions

File tree

‎cpp/function.cpp‎

Lines changed: 1 addition & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -2,54 +2,6 @@
22

33
#include "common.hpp"
44

5-
// TODO factor those dummy classes.
6-
class Class {};
7-
class Base {};
8-
class BaseProtected {};
9-
10-
// ERROR: no compound literals in C++
11-
//void foo (int bar[] = (int[2]){0 ,1});
12-
13-
// Function overloading
14-
15-
std::string overload(int i) { return "i"; }
16-
std::string overload(float i) { return "f"; }
17-
std::string overload(int i, int j) { return "ii"; }
18-
std::string overload(float i, float j, float k = 0.f) { return "iff"; }
19-
20-
// OK even if return type is different all is decided at compile time.
21-
22-
//int overload(int i, int j, int k){return 1;}
23-
24-
// ERROR: conflict with int.
25-
26-
//void overload(const int i){}
27-
28-
// ERROR: cannot differentiate by output since output is used to decide if other parts of code make sense.
29-
30-
//int overload(){return 0;}
31-
//float overload(){return 0.f;}
32-
33-
// ERROR: conflict with int int
34-
35-
//void overload(int i, int j=0){cout << "int int=";}
36-
37-
// BAD: compiles, but is useless to give a default,
38-
// since when calling, caller is *forced* to give a value for j
39-
// or wil get `call is ambiguous` compile time error
40-
// because compiler cannot decide between
41-
// here the default arg can be usefull for a call of type float float.
42-
43-
//void overload(float i, float j=1){cout << "float float=";}
44-
45-
// TODO: why does this compile, that is, how not to make an ambiguous call with overload(int).
46-
47-
void overloadValAddr(const int i) {}
48-
void overloadValAddr(const int& i) {}
49-
50-
void overloadBase(Base b) {}
51-
void overloadBase(BaseProtected b) {}
52-
535
// Default arguments.
546

557
void defaultArgProto(int i=0);
@@ -73,7 +25,7 @@ int def_no_argname(int, int) { return 2; }
7325
# auto arguments
7426
*/
7527

76-
/*ERROR: no you can't*/
28+
/*ERROR: no you can't*/
7729
/*
7830
int func_auto(auto a){
7931
++a;
@@ -82,58 +34,6 @@ int def_no_argname(int, int) { return 2; }
8234
*/
8335

8436
int main() {
85-
// # Overload
86-
{
87-
assert(overload(1) == "i");
88-
89-
//base type conversions
90-
{
91-
assert(overload(1.0f) == "f");
92-
93-
/*
94-
ERROR: ambiguous overload(int) overload(float)
95-
96-
Compiler does not know wether convert double to float or int.
97-
*/
98-
//overload(1.0);
99-
}
100-
101-
/*
102-
ERROR: ambiguous
103-
should compiler coverts to Base or BaseProtected? Both are possible via the default copy constructors.
104-
*/
105-
{
106-
Class cOverload;
107-
//overloadBase(cOverload);
108-
}
109-
110-
// ERROR: ambiguous
111-
//i=4;
112-
//overloadValAddr(i);
113-
114-
/*
115-
# volatile overload
116-
117-
Functions that differ by `volatile` can be overloaded:
118-
119-
- http://stackoverflow.com/questions/10242578/volatile-overloading
120-
*/
121-
{}
122-
123-
/*
124-
Function pointer: decided by the typecast.
125-
126-
http://stackoverflow.com/questions/2942426/how-to-specify-a-pointer-to-an-overloaded-function
127-
*/
128-
{
129-
// Variable.
130-
{
131-
std::string (*fi)(int) = overload;
132-
std::string (*ff)(float) = overload;
133-
}
134-
}
135-
}
136-
13737
/*
13838
In C++, unlike in C, definitions can omit argument names if they don't use those arguments!
13939

‎cpp/iterator.cpp‎

Lines changed: 78 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,22 @@ int main() {
5757
std::vector<int> v{1, 2, 0};
5858
int i = 0;
5959
int is[]{1, 2, 0};
60-
6160
for (auto it = v.begin(); it != v.end(); ++it) {
6261
assert(*it == is[i]);
6362
++i;
6463
}
6564
}
6665

66+
// begin is overloaded by method const-ness, to return either
67+
// ::iterator or ::const_iterator
68+
// http://stackoverflow.com/questions/12646998/how-does-begin-know-which-return-type-to-return-const-or-non-const
69+
{
70+
const std::vector<std::unique_ptr<int>> v;
71+
// ERROR.
72+
//std::vector<std::unique_ptr<int>>::iterator it = v.begin();
73+
std::vector<std::unique_ptr<int>>::const_iterator it = v.begin();
74+
}
75+
6776
/*
6877
# backwards iteration
6978
@@ -95,6 +104,7 @@ int main() {
95104
}
96105
}
97106

107+
#if __cplusplus >= 201103L
98108
/*
99109
# range based for loop
100110
@@ -121,9 +131,73 @@ int main() {
121131
- operator++()
122132
- operator!=()
123133
- operator*()
134+
135+
Those methods are usually trivial, except for ++ which can actually
136+
make a smart choice of what is the next value.
137+
I bet http://www.boost.org/doc/libs/1_62_0/libs/iterator/doc/index.html
138+
will make implementation easier with some defaults.
124139
*/
125140
{
126-
#if __cplusplus >= 201103L
141+
// Implement it yourself.
142+
{
143+
class Iterable {
144+
private:
145+
class Iterator {
146+
public:
147+
Iterator(int *ip) : ip(ip) {}
148+
Iterator& operator++() {
149+
this->ip++;
150+
return *this;
151+
}
152+
bool operator!=(const Iterator& other) const {
153+
return this->ip != other.ip;
154+
}
155+
// cont& could be used if there is no backing data,
156+
int& operator*() const {
157+
return *this->ip;
158+
}
159+
private:
160+
int *ip;
161+
};
162+
public:
163+
Iterable() : is{1, 2, 3} {
164+
}
165+
Iterator begin() {
166+
return Iterator(&is[0]);
167+
}
168+
Iterator end() {
169+
return Iterator(&is[3]);
170+
}
171+
private:
172+
int is[3];
173+
};
174+
175+
// Range based for loop works with that bare skelleton.
176+
// Try removing any method of the above, and watch it blow up.
177+
{
178+
int sum = 0;
179+
Iterable it;
180+
for (auto const& x : it) {
181+
sum += x;
182+
}
183+
assert(sum == 6);
184+
}
185+
186+
// Modification is also possible with this
187+
// default-ish setup and references.
188+
{
189+
int sum = 0;
190+
Iterable it;
191+
for (auto& x : it) {
192+
x++;
193+
}
194+
for (auto& x : it) {
195+
sum += x;
196+
}
197+
assert(sum == 9);
198+
}
199+
}
200+
127201
//forward
128202
{
129203
// If `int&` is used, no useless copies are made.
@@ -268,8 +342,9 @@ int main() {
268342
// Last element.
269343
*(v.end() - 1);
270344

345+
// POSSIBLE RUNTIME ERROR.
271346
// After last element no born check.
272-
*(v.end());
347+
//*(v.end());
273348

274349
// No such method.
275350
//(v.end().hasNext());

‎cpp/operator_overload.cpp‎

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
2424
Member or not
2525
26-
Certain operators can be both member functions and free functions.
26+
Certain operators can be both member functions and free overloaded functions.
2727
This includes most operators such as `+`, `=`, `+=` and others.
28-
See: <http://stackoverflow.com/a/4421729/895245> for a discussion on how to decide
28+
See: http://stackoverflow.com/a/4421729/895245 for a discussion on how to decide
2929
between them.
3030
3131
One question is that being non member improves the incapsulation, since then those
@@ -112,12 +112,13 @@ class OperatorOverload {
112112

113113

114114
// Prefix. Should return reference to match primitives.
115-
const OperatorOverload& operator++() {
115+
OperatorOverload& operator++() {
116116
++(this->i);
117117
return *this;
118118
}
119119

120120
// Postfix. Should not return reference to match primitives.
121+
// Yes, this weird `int` dummy argument differentiates signature from prefix.
121122
const OperatorOverload operator++(int) {
122123
OperatorOverload old(*this);
123124
++(*this);
@@ -329,16 +330,36 @@ int main() {
329330
{
330331
// Prefix
331332
{
332-
OperatorOverload i(1);
333-
assert(++i == OperatorOverload(2));
334-
assert(i == OperatorOverload(2));
333+
{
334+
OperatorOverload i(1);
335+
assert(++i == OperatorOverload(2));
336+
assert(i == OperatorOverload(2));
337+
}
338+
339+
// The non-const reference return allows to chain ++.
340+
{
341+
OperatorOverload i(1);
342+
assert(++(++i) == OperatorOverload(3));
343+
assert(i == OperatorOverload(3));
344+
345+
// Just like for primitives.
346+
{
347+
int i = 0;
348+
assert(++(++i) == 2);
349+
assert(i == 2);
350+
}
351+
}
335352
}
336353

337354
// Postfix. TODO
338355
{
339356
OperatorOverload i(1);
340357
assert(i++ == OperatorOverload(1));
341358
assert(i == OperatorOverload(2));
359+
360+
// ERROR. Cannot chain postfix, just like for primitives.
361+
// It must return a new value.
362+
//(i++)++
342363
}
343364
}
344365

0 commit comments

Comments
 (0)