Skip to content

Commit f1df8c7

Browse files
committed
tiles half works, debug segfault if key pressed many times with fps
1 parent a90d693 commit f1df8c7

8 files changed

Lines changed: 322 additions & 200 deletions

File tree

‎Makefile_many‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ O ?= 0
1414
STD ?= c11
1515
PEDANTIC ?= -pedantic-errors
1616
CFLAGS ?= -g$(G) -O$(O) -pthread -std=$(STD) -Wextra $(PEDANTIC) $(CFLAGS_EXTRA) #-pg
17-
MYCXXFLAGS ?= -g$(G) -O$(O) -pthread -std=c++11 -Wextra $(PEDANTIC) $(CXXFLAGS_EXTRA) #-pg
17+
MYCXXFLAGS ?= -g$(G) -O$(O) -pthread -std=c++14 -Wextra $(PEDANTIC) $(CXXFLAGS_EXTRA) #-pg
1818

1919
LIBS ?= -lm -lrt #-lGL -lGLU -lglut
2020

‎cpp/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
1. [Hello world](hello_world.cpp)
1010
1. [goto crosses initialization of](goto-cross-initialization.cpp)
1111
1. [const](const.cpp)
12+
1. [enum](enum.cpp)
1213
1. [RTTI](rtti.md)
1314
1. Compile time magic
1415
1. [constexpr](constexpr.cpp)

‎cpp/enum.cpp‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
Simple rule: always use C++11 enum class, never the old C-style enums.
3+
4+
- http://stackoverflow.com/questions/18335861/why-is-enum-class-preferred-over-plain-enum
5+
- http://stackoverflow.com/questions/4506750/how-do-i-use-the-enum-value-from-a-class-in-another-part-of-code
6+
- http://stackoverflow.com/questions/10869790/best-practices-for-enum-in-c
7+
*/
8+
9+
#include "common.hpp"
10+
11+
int main() {
12+
// Unlike C, already does typedef, no need to write enum all the time.
13+
{
14+
enum E {A, B, C};
15+
E e = A;
16+
}
17+
18+
#if __cplusplus >= 201103L
19+
// Set storage size.
20+
{
21+
enum E : char {A, B, C};
22+
assert(sizeof(E) == sizeof(char));
23+
}
24+
25+
// # class enum
26+
enum class ClassEnum {
27+
A,
28+
B
29+
};
30+
31+
// Error. YES, namespaces!
32+
//ClassEnum classEnum = A;
33+
34+
ClassEnum classEnum = ClassEnum::A;
35+
36+
// No conversion insanity.
37+
//int i = ClassEnum::A;
38+
#endif
39+
}

‎cpp/main.cpp‎

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -213,23 +213,6 @@ int main() {
213213
}
214214
}
215215

216-
// # enum
217-
{
218-
// Unlike C, already does typedef, no need to write enum all the time.
219-
{
220-
enum E {A, B, C};
221-
E e = A;
222-
}
223-
224-
#if __cplusplus >= 201103L
225-
// Set storage size.
226-
{
227-
enum E : char {A, B, C};
228-
assert(sizeof(E) == sizeof(char));
229-
}
230-
#endif
231-
}
232-
233216
// # for
234217
{
235218

‎cpp/reference.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ int main() {
106106
// They have the same address.
107107
{
108108
int i = 0;
109+
//int& ia = i;
109110
int& ia = i;
110111
ia = 1;
111112
assert(i == 1);

‎cpp/typeid.cpp‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ int main() {
3434
{
3535
int i, i1;
3636
int& ia = i;
37-
assert(typeid(i) == typeid(int) );
38-
assert(typeid(ia) == typeid(int&));
39-
assert(typeid(i) == typeid(i1) );
37+
assert(typeid(i) == typeid(int) );
38+
assert(typeid(ia) == typeid(int&));
39+
assert(typeid(i) == typeid(i1) );
4040
}
4141

4242
// Works differently for virtual and non virtual classes!

‎cpp/unique_ptr.cpp‎

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ Sample use case:
88
- how to prevent memory leaks?
99
1010
http://en.cppreference.com/w/cpp/memory/unique_ptr
11+
12+
unique_ptr may incur an extra dereferece cost, but it is usually well worth it.
13+
14+
In Java, everything can be though as a smart pointer (shared),
15+
so using this is still more efficient than Java, since C++ can know the exact
16+
lifetime of objects, and release them immediately when they are done.
1117
*/
1218

1319
#include "common.hpp"
@@ -52,6 +58,12 @@ void manual_ptr_test() {
5258
}
5359
}
5460

61+
// Create unique pointer dynamically,
62+
// and transfers ownershipt to caller.
63+
std::unique_ptr<Base> return_unique_ptr() {
64+
return std::unique_ptr<Base>(new Base(1));
65+
}
66+
5567
int main() {
5668
/* Basic example. */
5769
assert(Base::count == 0);
@@ -120,13 +132,46 @@ int main() {
120132
/*
121133
# unique_ptr function argments
122134
123-
- `std::move` for transfering ownership
135+
- transfering ownership TODO
136+
- use raw pointeres on the interface, and convert it to unique_ptr inside callee
137+
- if you already have an unique_ptr, release() it
138+
- this allows you to not tie down to a specific smart pointer on the function interface
139+
- use unique_ptr on interface and move on caller.
140+
- Advantage: unique_ptr on interface documents ownership transfer,
141+
and prevents callee from passing non new pointer to it by mistake.
124142
- TODO for not transfering ownership:
125-
- `const &`
143+
- `const & std::unique_ptr<T>`
126144
- `get()`. Simple and efficient. But how to use it for containers like `vector<std::unique_ptr>`?
145+
- `T&` on function, `*t` on caller. Looks good!
127146
128-
http://stackoverflow.com/questions/8114276/how-do-i-pass-a-unique-ptr-argument-to-a-constructor-or-a-functionhttp://stackoverflow.com/questions/8114276/how-do-i-pass-a-unique-ptr-argument-to-a-constructor-or-a-function
147+
- http://stackoverflow.com/questions/8114276/how-do-i-pass-a-unique-ptr-argument-to-a-constructor-or-a-functionhttp://stackoverflow.com/questions/8114276/how-do-i-pass-a-unique-ptr-argument-to-a-constructor-or-a-function
148+
- http://stackoverflow.com/questions/11277249/how-to-pass-stdunique-ptr-around
129149
*/
130150
{
131151
}
152+
153+
// Return unique_ptr from function.
154+
{
155+
{
156+
Base::count = 0;
157+
auto base = return_unique_ptr();
158+
assert(Base::count == 1);
159+
assert(base->i == 1);
160+
}
161+
assert(Base::count == 0);
162+
}
163+
164+
#if __cplusplus >= 201402L
165+
// # make_unique
166+
// Does new and puts it inside unique_ptr. Very convenient.
167+
{
168+
{
169+
Base::count = 0;
170+
auto base = std::make_unique<Base>(1);
171+
assert(Base::count == 1);
172+
assert(base->i == 1);
173+
}
174+
assert(Base::count == 0);
175+
}
176+
#endif
132177
}

0 commit comments

Comments
 (0)