├── LICENSE
├── README.md
├── asteroids
├── asteroids.c
├── asteroids.h
├── main.c
├── player.c
├── player.h
├── renderer.c
├── renderer.h
├── vector.c
└── vector.h
└── screenshot.png
/LICENSE:
--------------------------------------------------------------------------------
1 | GNU GENERAL PUBLIC LICENSE
2 | Version 2, June 1991
3 |
4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc.,
5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
6 | Everyone is permitted to copy and distribute verbatim copies
7 | of this license document, but changing it is not allowed.
8 |
9 | Preamble
10 |
11 | The licenses for most software are designed to take away your
12 | freedom to share and change it. By contrast, the GNU General Public
13 | License is intended to guarantee your freedom to share and change free
14 | software--to make sure the software is free for all its users. This
15 | General Public License applies to most of the Free Software
16 | Foundation's software and to any other program whose authors commit to
17 | using it. (Some other Free Software Foundation software is covered by
18 | the GNU Lesser General Public License instead.) You can apply it to
19 | your programs, too.
20 |
21 | When we speak of free software, we are referring to freedom, not
22 | price. Our General Public Licenses are designed to make sure that you
23 | have the freedom to distribute copies of free software (and charge for
24 | this service if you wish), that you receive source code or can get it
25 | if you want it, that you can change the software or use pieces of it
26 | in new free programs; and that you know you can do these things.
27 |
28 | To protect your rights, we need to make restrictions that forbid
29 | anyone to deny you these rights or to ask you to surrender the rights.
30 | These restrictions translate to certain responsibilities for you if you
31 | distribute copies of the software, or if you modify it.
32 |
33 | For example, if you distribute copies of such a program, whether
34 | gratis or for a fee, you must give the recipients all the rights that
35 | you have. You must make sure that they, too, receive or can get the
36 | source code. And you must show them these terms so they know their
37 | rights.
38 |
39 | We protect your rights with two steps: (1) copyright the software, and
40 | (2) offer you this license which gives you legal permission to copy,
41 | distribute and/or modify the software.
42 |
43 | Also, for each author's protection and ours, we want to make certain
44 | that everyone understands that there is no warranty for this free
45 | software. If the software is modified by someone else and passed on, we
46 | want its recipients to know that what they have is not the original, so
47 | that any problems introduced by others will not reflect on the original
48 | authors' reputations.
49 |
50 | Finally, any free program is threatened constantly by software
51 | patents. We wish to avoid the danger that redistributors of a free
52 | program will individually obtain patent licenses, in effect making the
53 | program proprietary. To prevent this, we have made it clear that any
54 | patent must be licensed for everyone's free use or not licensed at all.
55 |
56 | The precise terms and conditions for copying, distribution and
57 | modification follow.
58 |
59 | GNU GENERAL PUBLIC LICENSE
60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
61 |
62 | 0. This License applies to any program or other work which contains
63 | a notice placed by the copyright holder saying it may be distributed
64 | under the terms of this General Public License. The "Program", below,
65 | refers to any such program or work, and a "work based on the Program"
66 | means either the Program or any derivative work under copyright law:
67 | that is to say, a work containing the Program or a portion of it,
68 | either verbatim or with modifications and/or translated into another
69 | language. (Hereinafter, translation is included without limitation in
70 | the term "modification".) Each licensee is addressed as "you".
71 |
72 | Activities other than copying, distribution and modification are not
73 | covered by this License; they are outside its scope. The act of
74 | running the Program is not restricted, and the output from the Program
75 | is covered only if its contents constitute a work based on the
76 | Program (independent of having been made by running the Program).
77 | Whether that is true depends on what the Program does.
78 |
79 | 1. You may copy and distribute verbatim copies of the Program's
80 | source code as you receive it, in any medium, provided that you
81 | conspicuously and appropriately publish on each copy an appropriate
82 | copyright notice and disclaimer of warranty; keep intact all the
83 | notices that refer to this License and to the absence of any warranty;
84 | and give any other recipients of the Program a copy of this License
85 | along with the Program.
86 |
87 | You may charge a fee for the physical act of transferring a copy, and
88 | you may at your option offer warranty protection in exchange for a fee.
89 |
90 | 2. You may modify your copy or copies of the Program or any portion
91 | of it, thus forming a work based on the Program, and copy and
92 | distribute such modifications or work under the terms of Section 1
93 | above, provided that you also meet all of these conditions:
94 |
95 | a) You must cause the modified files to carry prominent notices
96 | stating that you changed the files and the date of any change.
97 |
98 | b) You must cause any work that you distribute or publish, that in
99 | whole or in part contains or is derived from the Program or any
100 | part thereof, to be licensed as a whole at no charge to all third
101 | parties under the terms of this License.
102 |
103 | c) If the modified program normally reads commands interactively
104 | when run, you must cause it, when started running for such
105 | interactive use in the most ordinary way, to print or display an
106 | announcement including an appropriate copyright notice and a
107 | notice that there is no warranty (or else, saying that you provide
108 | a warranty) and that users may redistribute the program under
109 | these conditions, and telling the user how to view a copy of this
110 | License. (Exception: if the Program itself is interactive but
111 | does not normally print such an announcement, your work based on
112 | the Program is not required to print an announcement.)
113 |
114 | These requirements apply to the modified work as a whole. If
115 | identifiable sections of that work are not derived from the Program,
116 | and can be reasonably considered independent and separate works in
117 | themselves, then this License, and its terms, do not apply to those
118 | sections when you distribute them as separate works. But when you
119 | distribute the same sections as part of a whole which is a work based
120 | on the Program, the distribution of the whole must be on the terms of
121 | this License, whose permissions for other licensees extend to the
122 | entire whole, and thus to each and every part regardless of who wrote it.
123 |
124 | Thus, it is not the intent of this section to claim rights or contest
125 | your rights to work written entirely by you; rather, the intent is to
126 | exercise the right to control the distribution of derivative or
127 | collective works based on the Program.
128 |
129 | In addition, mere aggregation of another work not based on the Program
130 | with the Program (or with a work based on the Program) on a volume of
131 | a storage or distribution medium does not bring the other work under
132 | the scope of this License.
133 |
134 | 3. You may copy and distribute the Program (or a work based on it,
135 | under Section 2) in object code or executable form under the terms of
136 | Sections 1 and 2 above provided that you also do one of the following:
137 |
138 | a) Accompany it with the complete corresponding machine-readable
139 | source code, which must be distributed under the terms of Sections
140 | 1 and 2 above on a medium customarily used for software interchange; or,
141 |
142 | b) Accompany it with a written offer, valid for at least three
143 | years, to give any third party, for a charge no more than your
144 | cost of physically performing source distribution, a complete
145 | machine-readable copy of the corresponding source code, to be
146 | distributed under the terms of Sections 1 and 2 above on a medium
147 | customarily used for software interchange; or,
148 |
149 | c) Accompany it with the information you received as to the offer
150 | to distribute corresponding source code. (This alternative is
151 | allowed only for noncommercial distribution and only if you
152 | received the program in object code or executable form with such
153 | an offer, in accord with Subsection b above.)
154 |
155 | The source code for a work means the preferred form of the work for
156 | making modifications to it. For an executable work, complete source
157 | code means all the source code for all modules it contains, plus any
158 | associated interface definition files, plus the scripts used to
159 | control compilation and installation of the executable. However, as a
160 | special exception, the source code distributed need not include
161 | anything that is normally distributed (in either source or binary
162 | form) with the major components (compiler, kernel, and so on) of the
163 | operating system on which the executable runs, unless that component
164 | itself accompanies the executable.
165 |
166 | If distribution of executable or object code is made by offering
167 | access to copy from a designated place, then offering equivalent
168 | access to copy the source code from the same place counts as
169 | distribution of the source code, even though third parties are not
170 | compelled to copy the source along with the object code.
171 |
172 | 4. You may not copy, modify, sublicense, or distribute the Program
173 | except as expressly provided under this License. Any attempt
174 | otherwise to copy, modify, sublicense or distribute the Program is
175 | void, and will automatically terminate your rights under this License.
176 | However, parties who have received copies, or rights, from you under
177 | this License will not have their licenses terminated so long as such
178 | parties remain in full compliance.
179 |
180 | 5. You are not required to accept this License, since you have not
181 | signed it. However, nothing else grants you permission to modify or
182 | distribute the Program or its derivative works. These actions are
183 | prohibited by law if you do not accept this License. Therefore, by
184 | modifying or distributing the Program (or any work based on the
185 | Program), you indicate your acceptance of this License to do so, and
186 | all its terms and conditions for copying, distributing or modifying
187 | the Program or works based on it.
188 |
189 | 6. Each time you redistribute the Program (or any work based on the
190 | Program), the recipient automatically receives a license from the
191 | original licensor to copy, distribute or modify the Program subject to
192 | these terms and conditions. You may not impose any further
193 | restrictions on the recipients' exercise of the rights granted herein.
194 | You are not responsible for enforcing compliance by third parties to
195 | this License.
196 |
197 | 7. If, as a consequence of a court judgment or allegation of patent
198 | infringement or for any other reason (not limited to patent issues),
199 | conditions are imposed on you (whether by court order, agreement or
200 | otherwise) that contradict the conditions of this License, they do not
201 | excuse you from the conditions of this License. If you cannot
202 | distribute so as to satisfy simultaneously your obligations under this
203 | License and any other pertinent obligations, then as a consequence you
204 | may not distribute the Program at all. For example, if a patent
205 | license would not permit royalty-free redistribution of the Program by
206 | all those who receive copies directly or indirectly through you, then
207 | the only way you could satisfy both it and this License would be to
208 | refrain entirely from distribution of the Program.
209 |
210 | If any portion of this section is held invalid or unenforceable under
211 | any particular circumstance, the balance of the section is intended to
212 | apply and the section as a whole is intended to apply in other
213 | circumstances.
214 |
215 | It is not the purpose of this section to induce you to infringe any
216 | patents or other property right claims or to contest validity of any
217 | such claims; this section has the sole purpose of protecting the
218 | integrity of the free software distribution system, which is
219 | implemented by public license practices. Many people have made
220 | generous contributions to the wide range of software distributed
221 | through that system in reliance on consistent application of that
222 | system; it is up to the author/donor to decide if he or she is willing
223 | to distribute software through any other system and a licensee cannot
224 | impose that choice.
225 |
226 | This section is intended to make thoroughly clear what is believed to
227 | be a consequence of the rest of this License.
228 |
229 | 8. If the distribution and/or use of the Program is restricted in
230 | certain countries either by patents or by copyrighted interfaces, the
231 | original copyright holder who places the Program under this License
232 | may add an explicit geographical distribution limitation excluding
233 | those countries, so that distribution is permitted only in or among
234 | countries not thus excluded. In such case, this License incorporates
235 | the limitation as if written in the body of this License.
236 |
237 | 9. The Free Software Foundation may publish revised and/or new versions
238 | of the General Public License from time to time. Such new versions will
239 | be similar in spirit to the present version, but may differ in detail to
240 | address new problems or concerns.
241 |
242 | Each version is given a distinguishing version number. If the Program
243 | specifies a version number of this License which applies to it and "any
244 | later version", you have the option of following the terms and conditions
245 | either of that version or of any later version published by the Free
246 | Software Foundation. If the Program does not specify a version number of
247 | this License, you may choose any version ever published by the Free Software
248 | Foundation.
249 |
250 | 10. If you wish to incorporate parts of the Program into other free
251 | programs whose distribution conditions are different, write to the author
252 | to ask for permission. For software which is copyrighted by the Free
253 | Software Foundation, write to the Free Software Foundation; we sometimes
254 | make exceptions for this. Our decision will be guided by the two goals
255 | of preserving the free status of all derivatives of our free software and
256 | of promoting the sharing and reuse of software generally.
257 |
258 | NO WARRANTY
259 |
260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
268 | REPAIR OR CORRECTION.
269 |
270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
278 | POSSIBILITY OF SUCH DAMAGES.
279 |
280 | END OF TERMS AND CONDITIONS
281 |
282 | How to Apply These Terms to Your New Programs
283 |
284 | If you develop a new program, and you want it to be of the greatest
285 | possible use to the public, the best way to achieve this is to make it
286 | free software which everyone can redistribute and change under these terms.
287 |
288 | To do so, attach the following notices to the program. It is safest
289 | to attach them to the start of each source file to most effectively
290 | convey the exclusion of warranty; and each file should have at least
291 | the "copyright" line and a pointer to where the full notice is found.
292 |
293 | {description}
294 | Copyright (C) {year} {fullname}
295 |
296 | This program is free software; you can redistribute it and/or modify
297 | it under the terms of the GNU General Public License as published by
298 | the Free Software Foundation; either version 2 of the License, or
299 | (at your option) any later version.
300 |
301 | This program is distributed in the hope that it will be useful,
302 | but WITHOUT ANY WARRANTY; without even the implied warranty of
303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
304 | GNU General Public License for more details.
305 |
306 | You should have received a copy of the GNU General Public License along
307 | with this program; if not, write to the Free Software Foundation, Inc.,
308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
309 |
310 | Also add information on how to contact you by electronic and paper mail.
311 |
312 | If the program is interactive, make it output a short notice like this
313 | when it starts in an interactive mode:
314 |
315 | Gnomovision version 69, Copyright (C) year name of author
316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
317 | This is free software, and you are welcome to redistribute it
318 | under certain conditions; type `show c' for details.
319 |
320 | The hypothetical commands `show w' and `show c' should show the appropriate
321 | parts of the General Public License. Of course, the commands you use may
322 | be called something other than `show w' and `show c'; they could even be
323 | mouse-clicks or menu items--whatever suits your program.
324 |
325 | You should also get your employer (if you work as a programmer) or your
326 | school, if any, to sign a "copyright disclaimer" for the program, if
327 | necessary. Here is a sample; alter the names:
328 |
329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program
330 | `Gnomovision' (which makes passes at compilers) written by James Hacker.
331 |
332 | {signature of Ty Coon}, 1 April 1989
333 | Ty Coon, President of Vice
334 |
335 | This General Public License does not permit incorporating your program into
336 | proprietary programs. If your program is a subroutine library, you may
337 | consider it more useful to permit linking proprietary applications with the
338 | library. If this is what you want to do, use the GNU Lesser General
339 | Public License instead of this License.
340 |
341 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Asteroids
2 |
3 | This is a WebAssembly port of an [Asteroids game written in C](https://github.com/flightcrank/asteroids):
4 |
5 | **[Live Demo](http://www.levelupwasm.com/apps/asteroids/index.html)** | **[WebAssembly Tutorial](https://medium.com/@robaboukhalil/porting-games-to-the-web-with-webassembly-70d598e1a3ec?source=friends_link&sk=20c835664031227eae5690b8a12514f0)**
6 |
7 | 
8 |
9 | If you're looking for a practical guide to learning WebAssembly, check out my book [Level up with WebAssembly](http://www.levelupwasm.com/).
10 |
11 | ## Build
12 |
13 | Fetch Emscripten from DockerHub:
14 |
15 | ```bash
16 | docker pull robertaboukhalil/emsdk:1.38.26
17 | ```
18 |
19 | Create a container from that image:
20 |
21 | ```bash
22 | # Create container from that image
23 | docker run -dt --name wasm robertaboukhalil/emsdk:1.38.26
24 |
25 | # Enter the container
26 | docker exec -it wasm bash
27 | ```
28 |
29 | Within the container, fetch the code:
30 |
31 | ```bash
32 | git clone "https://github.com/robertaboukhalil/wasm-asteroids.git"
33 | cd wasm-asteroids
34 | ```
35 |
36 | And compile it to WebAssembly:
37 |
38 | ```bash
39 | emcc \
40 | -o app.html asteroids/*.c \
41 | -Wall -g -lm \
42 | -s USE_SDL=2
43 | ```
44 |
--------------------------------------------------------------------------------
/asteroids/asteroids.c:
--------------------------------------------------------------------------------
1 |
2 | //asteroids.c
3 |
4 | #include
5 | #include
6 | #include
7 | #include "renderer.h"
8 | #include "asteroids.h"
9 |
10 | void init_asteroids(struct asteroid asteroids[], int size) {
11 |
12 | int i = 0;
13 | int j = 0;
14 | struct vector2d translation = {SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2};
15 |
16 | for (i = 0; i < size; i++) {
17 |
18 | int sign_x = rand() % 100;
19 | int sign_y = rand() % 100;
20 |
21 | //start asteroid in random location
22 | int lx = rand() % SCREEN_WIDTH / 2;
23 | int ly = rand() % SCREEN_HEIGHT / 2;
24 |
25 | //give asteroid random velocity
26 | float vx = (float) (rand() % 500) / 1000;
27 | float vy = (float) (rand() % 500) / 1000;
28 |
29 | float degrees = (float) (rand() % 100 + 1000) / 1000;
30 |
31 | //50% chance the sign of the variable will be switched to negative
32 | if (sign_x >= 50) {
33 |
34 | vx = -vx;
35 | lx = -lx;
36 | degrees = -degrees;
37 | }
38 |
39 | //50% chance the sign of the variable will be switched to negative
40 | if (sign_y >= 50) {
41 |
42 | vy = -vy;
43 | ly = -ly;
44 | }
45 |
46 | //firest 3 asteroids are alive
47 | if (i < 3) {
48 |
49 | asteroids[i].alive = 1;
50 |
51 | } else {
52 |
53 | asteroids[i].alive = 0;
54 | }
55 |
56 | asteroids[i].size = LARGE;
57 | asteroids[i].hit_radius = 35;
58 | asteroids[i].rotation = degrees;
59 | asteroids[i].location.x = lx;
60 | asteroids[i].location.y = ly;
61 | asteroids[i].velocity.x = vx;
62 | asteroids[i].velocity.y = vy;
63 | asteroids[i].obj_vert[0].x = .0;
64 | asteroids[i].obj_vert[0].y = .4;
65 | asteroids[i].obj_vert[1].x = .2;
66 | asteroids[i].obj_vert[1].y = .3;
67 | asteroids[i].obj_vert[2].x = .2;
68 | asteroids[i].obj_vert[2].y = .1;
69 | asteroids[i].obj_vert[3].x = .4;
70 | asteroids[i].obj_vert[3].y = .0;
71 | asteroids[i].obj_vert[4].x = .3;
72 | asteroids[i].obj_vert[4].y = -.2;
73 | asteroids[i].obj_vert[5].x = .1;
74 | asteroids[i].obj_vert[5].y = -.2;
75 | asteroids[i].obj_vert[6].x = .0;
76 | asteroids[i].obj_vert[6].y = -.3;
77 | asteroids[i].obj_vert[7].x = -.2;
78 | asteroids[i].obj_vert[7].y = -.2;
79 | asteroids[i].obj_vert[8].x = -.4;
80 | asteroids[i].obj_vert[8].y = 0;
81 | asteroids[i].obj_vert[9].x = -.3;
82 | asteroids[i].obj_vert[9].y = .3;
83 |
84 | for (j = 0; j < VERTS; j++) {
85 |
86 | //coverts verts from obj space to world space and traslate world space to screen space
87 | multiply_vector(&asteroids[i].obj_vert[j], 88);
88 | add_vector(&asteroids[i].world_vert[j], &asteroids[i].obj_vert[j]);
89 | add_vector(&asteroids[i].world_vert[j], &translation);
90 | }
91 | }
92 | }
93 |
94 | void update_asteroids(struct asteroid asteroids[], int size) {
95 |
96 | int i = 0;
97 | int j = 0;
98 | struct vector2d translation = {SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2};
99 |
100 | for (i = 0; i < size; i++) {
101 |
102 | //updates each asteroids location based off its velicity vector
103 | add_vector(&asteroids[i].location, &asteroids[i].velocity);
104 |
105 | //update each vert of the asteroid to reflect the changes made to the asteroids location vector
106 | //and rotation amount, then translate the new vert location to screen space
107 | for (j = 0; j < VERTS; j++) {
108 |
109 | asteroids[i].world_vert[j] = add_vector_new(&asteroids[i].obj_vert[j], &asteroids[i].location);
110 | add_vector(&asteroids[i].world_vert[j], &translation);
111 | rotate_vector(&asteroids[i].obj_vert[j], asteroids[i].rotation);
112 | }
113 | }
114 | }
115 |
116 | void draw_asteroids(uint32_t* pixel_buffer, struct asteroid asteroids[], int size) {
117 |
118 | int i = 0;
119 |
120 | for (i = 0; i < size; i++) {
121 |
122 | if (asteroids[i].alive) {
123 |
124 | draw_line(pixel_buffer, asteroids[i].world_vert[0].x, asteroids[i].world_vert[0].y, asteroids[i].world_vert[1].x, asteroids[i].world_vert[1].y, 0xffffffff);
125 | draw_line(pixel_buffer, asteroids[i].world_vert[1].x, asteroids[i].world_vert[1].y, asteroids[i].world_vert[2].x, asteroids[i].world_vert[2].y, 0xffffffff);
126 | draw_line(pixel_buffer, asteroids[i].world_vert[2].x, asteroids[i].world_vert[2].y, asteroids[i].world_vert[3].x, asteroids[i].world_vert[3].y, 0xffffffff);
127 | draw_line(pixel_buffer, asteroids[i].world_vert[3].x, asteroids[i].world_vert[3].y, asteroids[i].world_vert[4].x, asteroids[i].world_vert[4].y, 0xffffffff);
128 | draw_line(pixel_buffer, asteroids[i].world_vert[4].x, asteroids[i].world_vert[4].y, asteroids[i].world_vert[5].x, asteroids[i].world_vert[5].y, 0xffffffff);
129 | draw_line(pixel_buffer, asteroids[i].world_vert[5].x, asteroids[i].world_vert[5].y, asteroids[i].world_vert[6].x, asteroids[i].world_vert[6].y, 0xffffffff);
130 | draw_line(pixel_buffer, asteroids[i].world_vert[6].x, asteroids[i].world_vert[6].y, asteroids[i].world_vert[7].x, asteroids[i].world_vert[7].y, 0xffffffff);
131 | draw_line(pixel_buffer, asteroids[i].world_vert[7].x, asteroids[i].world_vert[7].y, asteroids[i].world_vert[8].x, asteroids[i].world_vert[8].y, 0xffffffff);
132 | draw_line(pixel_buffer, asteroids[i].world_vert[8].x, asteroids[i].world_vert[8].y, asteroids[i].world_vert[9].x, asteroids[i].world_vert[9].y, 0xffffffff);
133 | draw_line(pixel_buffer, asteroids[i].world_vert[9].x, asteroids[i].world_vert[9].y, asteroids[i].world_vert[0].x, asteroids[i].world_vert[0].y, 0xffffffff);
134 | }
135 |
136 | /*
137 | //draw vert representing ships location
138 | struct vector2d cpy = {asteroids[i].location.x, asteroids[i].location.y};
139 | struct vector2d translation = {SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2};
140 | add_vector(&cpy, &translation);
141 |
142 | draw_pixel(pixel_buffer, cpy.x, cpy.y, 0x00ff00ff);
143 | */
144 | }
145 | }
146 |
147 | int shrink_asteroid(struct asteroid* a, int size) {
148 |
149 | a->obj_vert[0].x = .0;
150 | a->obj_vert[0].y = .4;
151 | a->obj_vert[1].x = .2;
152 | a->obj_vert[1].y = .3;
153 | a->obj_vert[2].x = .2;
154 | a->obj_vert[2].y = .1;
155 | a->obj_vert[3].x = .4;
156 | a->obj_vert[3].y = .0;
157 | a->obj_vert[4].x = .3;
158 | a->obj_vert[4].y = -.2;
159 | a->obj_vert[5].x = .1;
160 | a->obj_vert[5].y = -.2;
161 | a->obj_vert[6].x = .0;
162 | a->obj_vert[6].y = -.3;
163 | a->obj_vert[7].x = -.2;
164 | a->obj_vert[7].y = -.2;
165 | a->obj_vert[8].x = -.4;
166 | a->obj_vert[8].y = 0;
167 | a->obj_vert[9].x = -.3;
168 | a->obj_vert[9].y = .3;
169 |
170 | int i = 0;
171 |
172 | for (i = 0; i < VERTS; i++) {
173 |
174 | multiply_vector(&a->obj_vert[i], 88);
175 | }
176 |
177 | if (size == LARGE) {
178 |
179 | //shrink asteroid
180 | for (i = 0; i < VERTS; i++) {
181 |
182 | divide_vector(&a->obj_vert[i], 2);
183 | }
184 |
185 | a->size = MEDIUM;
186 |
187 | return 0;
188 | }
189 |
190 | if (size == MEDIUM) {
191 |
192 | //shrink asteroid
193 | for (i = 0; i < VERTS; i++) {
194 |
195 | divide_vector(&a->obj_vert[i], 4);
196 | }
197 |
198 | a->size = SMALL;
199 |
200 | return 0;
201 | }
202 |
203 | if (size == SMALL) {
204 |
205 | //shrink asteroid
206 | for (i = 0; i < VERTS; i++) {
207 |
208 | divide_vector(&a->obj_vert[i], 8);
209 | }
210 |
211 | return 0;
212 | }
213 |
214 | return 1;
215 | }
216 |
217 | void spawn_asteroids(struct asteroid a[], int length, int size, struct vector2d v) {
218 |
219 | int i = 0;
220 | int count = 0;
221 |
222 | for (i = 0; i < length; i++) {
223 |
224 | if(a[i].alive == 0) {
225 |
226 | if (count == 3) {
227 |
228 | break;
229 | }
230 |
231 | a[i].location = v;
232 | a[i].hit_radius /= 2;
233 | a[i].alive = 1;
234 | count++;
235 | shrink_asteroid(&a[i], size);
236 | }
237 | }
238 | }
239 |
240 | void bounds_asteroids(struct asteroid asteroids[], int size) {
241 |
242 | int i = 0;
243 |
244 | for (i = 0 ; i < size; i++) {
245 |
246 | if (asteroids[i].location.x < -SCREEN_WIDTH / 2) {
247 |
248 | asteroids[i].location.x = SCREEN_WIDTH / 2;
249 | }
250 |
251 | if (asteroids[i].location.x > SCREEN_WIDTH / 2) {
252 |
253 | asteroids[i].location.x = -SCREEN_WIDTH / 2;
254 | }
255 |
256 | if (asteroids[i].location.y < -SCREEN_HEIGHT / 2) {
257 |
258 | asteroids[i].location.y = SCREEN_HEIGHT / 2;
259 | }
260 |
261 | if (asteroids[i].location.y > SCREEN_HEIGHT / 2) {
262 |
263 | asteroids[i].location.y = -SCREEN_HEIGHT / 2;
264 | }
265 | }
266 | }
267 |
268 |
269 | int collision_asteroids(struct asteroid asteroids[], int size, struct vector2d* v, float radius) {
270 |
271 |
272 | int i = 0;
273 |
274 |
275 | for (i = 0; i < size; i++) {
276 |
277 | //only check for collions for asteroids that are shown onscreen
278 | if (asteroids[i].alive == 1) {
279 |
280 | float sum = asteroids[i].hit_radius + radius;
281 | float a = pow(asteroids[i].location.x - v->x, 2);
282 | float b = pow(asteroids[i].location.y - v->y, 2);
283 | float distance = sqrt(a + b);
284 |
285 | if (distance < sum) {
286 |
287 | return i;
288 | }
289 | }
290 | }
291 |
292 | return -1;
293 | }
294 |
--------------------------------------------------------------------------------
/asteroids/asteroids.h:
--------------------------------------------------------------------------------
1 |
2 | //asteroids.h
3 | #include "vector.h"
4 | #define VERTS 10
5 |
6 | enum sizes {SMALL, MEDIUM, LARGE};
7 |
8 | struct asteroid {
9 |
10 | int alive;
11 | enum sizes size;
12 | float hit_radius;
13 | float rotation;
14 | struct vector2d location;
15 | struct vector2d velocity;
16 | struct vector2d obj_vert[VERTS];
17 | struct vector2d world_vert[VERTS];
18 | };
19 |
20 | void init_asteroids(struct asteroid asteroids[], int size);
21 | void update_asteroids(struct asteroid asteroids[], int size);
22 | void draw_asteroids(uint32_t* pixel_buffer, struct asteroid asteroids[], int size);
23 | int shrink_asteroid(struct asteroid* a, int size);
24 | void spawn_asteroids(struct asteroid a[], int length, int size, struct vector2d v);
25 | void bounds_asteroids(struct asteroid asteroids[], int size);
26 | int collision_asteroids(struct asteroid asteroids[], int size, struct vector2d* v, float radius);
27 |
--------------------------------------------------------------------------------
/asteroids/main.c:
--------------------------------------------------------------------------------
1 |
2 | //main.c
3 |
4 | //Using SDL and standard IO
5 | #include
6 | #include
7 | #include
8 | #include "renderer.h"
9 | #include "player.h"
10 | #include "asteroids.h"
11 |
12 | // Import "emscripten_set_main_loop()" and "emscripten_cancel_main_loop()"
13 | #include "emscripten.h"
14 |
15 | #define ASTEROIDS 27
16 | #define LIVES 3
17 |
18 | int init(int width, int height);
19 |
20 | SDL_Window* window = NULL; //The window we'll be rendering to
21 | SDL_Renderer *renderer; //The renderer SDL will use to draw to the screen
22 | SDL_Texture *screen; //The texture representing the screen
23 | uint32_t* pixels = NULL; //The pixel buffer to draw to
24 | struct asteroid asteroids[ASTEROIDS]; //The asteroids
25 | struct player p; //The player
26 | struct player lives[LIVES]; //Player lives left
27 |
28 |
29 | // Main game loop
30 | void mainloop()
31 | {
32 | // Moved these variables
33 | int sleep = 0;
34 | int quit = 0;
35 | SDL_Event event;
36 | Uint32 next_game_tick = SDL_GetTicks();
37 |
38 | // Main loop code
39 | //check for new events every frame
40 | SDL_PumpEvents();
41 |
42 | const Uint8 *state = SDL_GetKeyboardState(NULL);
43 |
44 | if (state[SDL_SCANCODE_ESCAPE]) {
45 | quit = 1;
46 | }
47 |
48 | if (state[SDL_SCANCODE_UP]) {
49 | struct vector2d thrust = get_direction(&p);
50 | multiply_vector(&thrust, .06);
51 | apply_force(&p.velocity, thrust);
52 | }
53 |
54 | if (state[SDL_SCANCODE_LEFT]) {
55 | rotate_player(&p, -4);
56 | }
57 |
58 | if (state[SDL_SCANCODE_RIGHT]) {
59 | rotate_player(&p, 4);
60 | }
61 |
62 | while (SDL_PollEvent(&event)) {
63 | switch(event.type) {
64 | case SDL_KEYDOWN:
65 | switch( event.key.keysym.sym ) {
66 | case SDLK_SPACE:
67 | if (p.lives > 0) {
68 | shoot_bullet(&p);
69 | }
70 | break;
71 | }
72 | }
73 | }
74 |
75 | //draw to the pixel buffer
76 | clear_pixels(pixels, 0x00000000);
77 | draw_asteroids(pixels, asteroids, ASTEROIDS);
78 | update_player(&p);
79 | bounds_player(&p);
80 | bounds_asteroids(asteroids, ASTEROIDS);
81 |
82 | int res = collision_asteroids(asteroids, ASTEROIDS, &p.location, p.hit_radius);
83 |
84 | if (res != -1) {
85 | p.lives--;
86 | p.location.x = 0;
87 | p.location.y = 0;
88 | p.velocity.x = 0;
89 | p.velocity.y = 0;
90 |
91 | int i = LIVES - 1;
92 | for ( i = LIVES; i >= 0; i--) {
93 | if(lives[i].lives > 0) {
94 | lives[i].lives = 0;
95 | break;
96 | }
97 | }
98 | }
99 | draw_player(pixels, &p);
100 | draw_player(pixels, &lives[0]);
101 | draw_player(pixels, &lives[1]);
102 | draw_player(pixels, &lives[2]);
103 |
104 | int i = 0;
105 | struct vector2d translation = {-SCREEN_WIDTH / 2, -SCREEN_HEIGHT / 2};
106 |
107 | for (i = 0; i < BULLETS; i++) {
108 | //only check for collision for bullets that are shown on screen
109 | if (p.bullets[i].alive == TRUE) {
110 | //convert bullet screen space location to world space to compare
111 | //with asteroids world space to detect a collision
112 | struct vector2d world = add_vector_new(&p.bullets[i].location, &translation);
113 | int index = collision_asteroids(asteroids, ASTEROIDS, &world, 1);
114 |
115 | //collision occured
116 | if (index != -1) {
117 | asteroids[index].alive = 0;
118 | p.bullets[i].alive = FALSE;
119 |
120 | if (asteroids[index].size != SMALL) {
121 | spawn_asteroids(asteroids, ASTEROIDS, asteroids[index].size, asteroids[index].location);
122 | }
123 | }
124 | }
125 | }
126 |
127 | update_asteroids(asteroids, ASTEROIDS);
128 |
129 | //draw buffer to the texture representing the screen
130 | SDL_UpdateTexture(screen, NULL, pixels, SCREEN_WIDTH * sizeof (Uint32));
131 |
132 | //draw to the screen
133 | SDL_RenderClear(renderer);
134 | SDL_RenderCopy(renderer, screen, NULL, NULL);
135 | SDL_RenderPresent(renderer);
136 |
137 | if(quit == 1) {
138 | emscripten_cancel_main_loop();
139 |
140 | //free the screen buffer
141 | free(pixels);
142 |
143 | //Destroy window
144 | SDL_DestroyWindow(window);
145 |
146 | //Quit SDL subsystems
147 | SDL_Quit();
148 | }
149 | }
150 |
151 | int main (int argc, char* args[]) {
152 |
153 | //SDL Window setup
154 | if (init(SCREEN_WIDTH, SCREEN_HEIGHT) == 1) {
155 |
156 | return 0;
157 | }
158 |
159 | int i = 0;
160 | int j = 0;
161 | int offset = 0;
162 | struct vector2d translation = {-SCREEN_WIDTH / 2, -SCREEN_HEIGHT / 2};
163 |
164 | //set up icons used to represent player lives
165 | for (i = 0; i < LIVES; i++) {
166 |
167 | init_player(&lives[i]);
168 | lives[i].lives = 1;
169 |
170 | //shrink lives
171 | for (j = 0; j < P_VERTS; j++) {
172 |
173 | divide_vector(&lives[i].obj_vert[j], 2);
174 | }
175 |
176 | //convert screen space vector into world space
177 | struct vector2d top_left = {20 + offset, 20};
178 | add_vector(&top_left, &translation);
179 | lives[i].location = top_left;
180 | update_player(&lives[i]);
181 | offset += 20;
182 | }
183 |
184 | //set up player and asteroids in world space
185 | init_player(&p);
186 | init_asteroids(asteroids, ASTEROIDS);
187 |
188 | emscripten_set_main_loop(mainloop, 0, 1);
189 |
190 | return 0;
191 | }
192 |
193 | int init(int width, int height) {
194 |
195 | //Initialize SDL
196 | if (SDL_Init(SDL_INIT_VIDEO) < 0) {
197 |
198 | printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
199 |
200 | return 1;
201 | }
202 |
203 | //Create window
204 | SDL_CreateWindowAndRenderer(SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN, &window, &renderer);
205 |
206 | //set up screen texture
207 | screen = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STREAMING, SCREEN_WIDTH, SCREEN_HEIGHT);
208 |
209 | //allocate pixel buffer
210 | pixels = (Uint32*) malloc((SCREEN_WIDTH * SCREEN_HEIGHT) * sizeof(Uint32));
211 |
212 |
213 | if (window == NULL) {
214 |
215 | printf ("Window could not be created! SDL_Error: %s\n", SDL_GetError());
216 |
217 | return 1;
218 | }
219 |
220 | if (screen == NULL) {
221 |
222 | printf ("Texture could not be created! SDL_Error: %s\n", SDL_GetError());
223 |
224 | return 1;
225 | }
226 |
227 | if (pixels == NULL) {
228 |
229 | printf ("Error allocating pixel buffer");
230 |
231 | return 1;
232 | }
233 |
234 | return 0;
235 | }
236 |
--------------------------------------------------------------------------------
/asteroids/player.c:
--------------------------------------------------------------------------------
1 |
2 | //player.c
3 |
4 | #include
5 | #include "renderer.h"
6 | #include "player.h"
7 |
8 | void init_player(struct player* p) {
9 |
10 | int i = 0;
11 | struct vector2d translation = {SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2};
12 |
13 | p->hit_radius = 15;
14 | p->lives = 3;
15 | p->location.x = 0;
16 | p->location.y = 0;
17 | p->velocity.x = 0;
18 | p->velocity.y = 0;
19 | p->obj_vert[0].x = 0;
20 | p->obj_vert[0].y = 1.5;
21 | p->obj_vert[1].x = -1;
22 | p->obj_vert[1].y = -1;
23 | p->obj_vert[2].x = 1;
24 | p->obj_vert[2].y = -1;
25 |
26 | //convert player verts from object space to world space
27 | for(i = 0; i < P_VERTS; i++) {
28 |
29 | multiply_vector(&p->obj_vert[i], -1);
30 | multiply_vector(&p->obj_vert[i], 12);
31 | add_vector(&p->world_vert[i], &p->obj_vert[i]);
32 | add_vector(&p->world_vert[i], &translation);
33 | }
34 |
35 | for(i = 0; i < BULLETS; i++) {
36 |
37 | p->bullets[i].alive = FALSE;
38 | }
39 | }
40 |
41 | void apply_force(struct vector2d* velocity, struct vector2d v) {
42 |
43 | add_vector(velocity, &v);
44 | }
45 |
46 | struct vector2d get_direction(struct player* p) {
47 |
48 | struct vector2d direction = p->obj_vert[0];
49 | normalise_vector(&direction);
50 |
51 | return direction;
52 | }
53 |
54 | void shoot_bullet(struct player* p) {
55 |
56 | int i = 0;
57 |
58 | for (i = 0; i < BULLETS; i++) {
59 |
60 | if (p->bullets[i].alive == FALSE) {
61 |
62 | p->bullets[i].alive = TRUE;
63 | p->bullets[i].location = p->world_vert[0];
64 | p->bullets[i].velocity = get_direction(p);
65 | multiply_vector(&p->bullets[i].velocity, 4.1);
66 | break;
67 | }
68 | }
69 | }
70 |
71 | void draw_player(uint32_t* pixel_buffer, struct player* p) {
72 |
73 | int i = 0;
74 |
75 | if (p->lives > 0) {
76 |
77 | draw_line(pixel_buffer, p->world_vert[0].x, p->world_vert[0].y, p->world_vert[1].x, p->world_vert[1].y, 0xffffffff);
78 | draw_line(pixel_buffer, p->world_vert[1].x, p->world_vert[1].y, p->world_vert[2].x, p->world_vert[2].y, 0xffffffff);
79 | draw_line(pixel_buffer, p->world_vert[2].x, p->world_vert[2].y, p->world_vert[0].x, p->world_vert[0].y, 0xffffffff);
80 | }
81 |
82 | /*
83 | //draw vers representing the player
84 | for (i = 0; i < P_VERTS; i++) {
85 |
86 | draw_pixel(pixel_buffer, p->world_vert[i].x, p->world_vert[i].y, 0xff00ffff);
87 | }
88 | */
89 |
90 | //draw verts representing the bullets
91 | for (i = 0; i < BULLETS; i++) {
92 |
93 | if (p->bullets[i].alive == TRUE) {
94 |
95 | draw_pixel(pixel_buffer, p->bullets[i].location.x, p->bullets[i].location.y, 0xffffffff);
96 | }
97 | }
98 |
99 | /*
100 | //draw vert representing ships location
101 | struct vector2d cpy = {p->location.x, p->location.y};
102 | struct vector2d translation = {SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2};
103 | add_vector(&cpy, &translation);
104 |
105 | draw_pixel(pixel_buffer, cpy.x, cpy.y, 0x00ff00ff);
106 | */
107 | }
108 |
109 | void update_player(struct player* p) {
110 |
111 | limit_vector(&p->velocity, 2);
112 | add_vector(&p->location, &p->velocity);
113 |
114 | struct vector2d translation = {SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2};
115 |
116 | int i = 0;
117 |
118 | for (i =0; i < P_VERTS; i++) {
119 |
120 | p->world_vert[i] = add_vector_new(&p->obj_vert[i], &p->location);
121 | add_vector(&p->world_vert[i], &translation);
122 | }
123 |
124 | for (i = 0; i < BULLETS; i++) {
125 |
126 | add_vector(&p->bullets[i].location, &p->bullets[i].velocity);
127 | }
128 | }
129 |
130 | void rotate_player(struct player* p, float degrees) {
131 |
132 | int i = 0;
133 |
134 | for (i =0; i < P_VERTS; i++) {
135 |
136 | rotate_vector(&p->obj_vert[i], degrees);
137 | }
138 | }
139 |
140 | void bounds_player(struct player* p) {
141 |
142 | int i = 0;
143 |
144 | if (p->location.x < -SCREEN_WIDTH / 2) {
145 |
146 | p->location.x = SCREEN_WIDTH / 2;
147 | }
148 |
149 | if (p->location.x > SCREEN_WIDTH / 2) {
150 |
151 | p->location.x = -SCREEN_WIDTH / 2;
152 | }
153 |
154 | if (p->location.y < -SCREEN_HEIGHT / 2) {
155 |
156 | p->location.y = SCREEN_HEIGHT / 2;
157 | }
158 |
159 | if (p->location.y > SCREEN_HEIGHT / 2) {
160 |
161 | p->location.y = -SCREEN_HEIGHT / 2;
162 | }
163 |
164 | //bullet is out of bounds, reset bullet to be shot again
165 | //bullets are in world space
166 | for (i = 0; i < BULLETS; i++) {
167 |
168 | if (p->bullets[i].location.x < 0 || p->bullets[i].location.x >= SCREEN_WIDTH) {
169 |
170 | p->bullets[i].alive = FALSE;
171 | }
172 |
173 | if (p->bullets[i].location.y < 0 || p->bullets[i].location.y >= SCREEN_HEIGHT) {
174 |
175 | p->bullets[i].alive = FALSE;
176 | }
177 | }
178 | }
179 |
180 |
--------------------------------------------------------------------------------
/asteroids/player.h:
--------------------------------------------------------------------------------
1 |
2 | //player.h
3 |
4 | #include "vector.h"
5 |
6 | #define P_VERTS 3
7 | #define BULLETS 3
8 |
9 | enum boolean {TRUE, FALSE};
10 |
11 | struct bullet {
12 |
13 | struct vector2d location;
14 | struct vector2d velocity;
15 | enum boolean alive;
16 | };
17 |
18 | struct player {
19 |
20 | float hit_radius;
21 | int lives;
22 | struct vector2d location;
23 | struct vector2d velocity;
24 | struct vector2d obj_vert[P_VERTS];
25 | struct vector2d world_vert[P_VERTS];
26 | struct bullet bullets[BULLETS];
27 | };
28 |
29 | void init_player(struct player* p);
30 |
31 | void draw_player(uint32_t* pixel_buffer, struct player* p);
32 |
33 | void shoot_bullet(struct player* p);
34 |
35 | void update_player(struct player* p);
36 |
37 | void bounds_player(struct player* p);
38 |
39 | void apply_force(struct vector2d* velocity, struct vector2d v);
40 |
41 | void rotate_player(struct player* p, float degrees);
42 |
43 | struct vector2d get_direction(struct player* p);
44 |
45 |
--------------------------------------------------------------------------------
/asteroids/renderer.c:
--------------------------------------------------------------------------------
1 |
2 | //renderer.c
3 |
4 | #include "renderer.h"
5 |
6 | int draw_line(uint32_t* pixel_buffer, int x1, int y1, int x2, int y2, uint32_t colour) {
7 |
8 |
9 | //plot the first point
10 | draw_pixel(pixel_buffer, x1, y1, colour);
11 |
12 | //make sure we draw the line always from left to right
13 | if (x1 > x2) {
14 |
15 | int temp_x = x1;
16 | int temp_y = y1;
17 |
18 | x1 = x2;
19 | y1 = y2;
20 |
21 | x2 = temp_x;
22 | y2 = temp_y;
23 | }
24 |
25 | int dx = x2 - x1;
26 | int dy = y2 - y1;
27 |
28 | //the length of the line is greater along the X axis
29 | if (dx >= fabs(dy)) {
30 |
31 | float slope = (float) dy / dx;
32 |
33 | //line travels from top to bottom
34 | if (y1 <= y2) {
35 |
36 | float ideal_y = y1 + slope;
37 | int y = (int) round(ideal_y);
38 | float error = ideal_y - y;
39 |
40 | int i = 0;
41 |
42 | //loop through all the X values
43 | for(i = 1; i <= dx; i++) {
44 |
45 | int x = x1 + i;
46 |
47 | draw_pixel(pixel_buffer, x, y, colour);
48 |
49 | error += slope;
50 |
51 | if (error >= 0.5) {
52 |
53 | y++;
54 | error -= 1;
55 | }
56 | }
57 | }
58 |
59 | //line travels from bottom to top
60 | if (y1 > y2) {
61 |
62 | float ideal_y = y1 + slope;
63 | int y = (int) round(ideal_y);
64 | float error = ideal_y - y;
65 |
66 | int i = 0;
67 |
68 | //loop through all the x values
69 | for(i = 1; i <= dx; i++) {
70 |
71 | int x = x1 + i;
72 |
73 | draw_pixel(pixel_buffer, x, y, colour);
74 |
75 | error += slope;
76 |
77 | if (error <= -0.5) {
78 |
79 | y--;
80 | error += 1;
81 | }
82 | }
83 | }
84 |
85 |
86 | }
87 |
88 | //the length of the line is greater along the Y axis
89 | if (fabs(dy) > dx) {
90 |
91 | float slope = (float) dx / dy;
92 |
93 | //line travels from top to bottom
94 | if (y1 < y2) {
95 |
96 | float ideal_x = x1 + slope;
97 | int x = (int) round(ideal_x);
98 | float error = ideal_x - x;
99 |
100 | int i = 0;
101 |
102 | //loop through all the y values
103 | for(i = 1; i <= dy; i++) {
104 |
105 | int y = y1 + i;
106 |
107 | draw_pixel(pixel_buffer, x, y, colour);
108 |
109 | error += slope;
110 |
111 | if (error >= 0.5) {
112 |
113 | x++;
114 | error -= 1;
115 | }
116 | }
117 | }
118 |
119 | //draw line from bottom to top
120 | if (y1 > y2) {
121 |
122 | float ideal_x = x1 - slope;
123 | int x = (int) round(ideal_x);
124 | float error = ideal_x - x;
125 |
126 | int i = 0;
127 |
128 | //loop through all the y values
129 | for(i = 1; i <= fabs(dy); i++) {
130 |
131 | int y = y1 - i;
132 |
133 | draw_pixel(pixel_buffer, x, y, colour);
134 |
135 | error += slope;
136 |
137 | if (error <= -0.5) {
138 |
139 | x++;
140 | error += 1;
141 | }
142 | }
143 | }
144 | }
145 |
146 | return 0;
147 | }
148 |
149 | int draw_pixel(uint32_t* pixel_buffer, int x, int y, uint32_t colour) {
150 |
151 | //dont draw any pixels that are outside of the pixel buffer
152 | if (x < 0 || y < 0) {
153 |
154 | return 1;
155 | }
156 |
157 | //dont draw any pixels that are outside of the pixel buffer
158 | if (x >= SCREEN_WIDTH || y >= SCREEN_HEIGHT) {
159 |
160 | return 1;
161 | }
162 |
163 | uint32_t position = y * SCREEN_WIDTH + x;
164 | pixel_buffer[position] = colour;
165 |
166 | return 0;
167 | }
168 |
169 | void clear_pixels(uint32_t* pixel_buffer, uint32_t colour) {
170 |
171 | int i = 0;
172 | int buffer_size = SCREEN_WIDTH * SCREEN_HEIGHT;
173 |
174 | for (i = 0; i < buffer_size; i++) {
175 |
176 | pixel_buffer[i] = colour;
177 | }
178 | }
179 |
180 |
--------------------------------------------------------------------------------
/asteroids/renderer.h:
--------------------------------------------------------------------------------
1 |
2 | //renderer.h
3 |
4 | #include
5 | #include
6 |
7 | //Screen dimension constants
8 | #define SCREEN_WIDTH 640
9 | #define SCREEN_HEIGHT 480
10 |
11 | //assing a colour to a single pixel
12 | int draw_pixel(uint32_t* pixel_buffer, int x, int y, uint32_t colour);
13 |
14 | int draw_line(uint32_t* pixel_buffer, int x1, int y1, int x2, int y2, uint32_t colour);
15 |
16 | //assign a colour to all pixels
17 | void clear_pixels(uint32_t* pixel_buffer, uint32_t colour);
18 |
19 |
--------------------------------------------------------------------------------
/asteroids/vector.c:
--------------------------------------------------------------------------------
1 |
2 | //vector.c
3 |
4 | #include
5 | #include
6 | #include "vector.h"
7 |
8 | void add_vector(struct vector2d* a, struct vector2d* b) {
9 |
10 | a->x += b->x;
11 | a->y += b->y;
12 | }
13 |
14 | struct vector2d add_vector_new(struct vector2d* a, struct vector2d* b) {
15 |
16 | struct vector2d new = {a->x + b->x, a->y + b->y};
17 |
18 | return new;
19 | }
20 |
21 | void multiply_vector(struct vector2d* v, float n) {
22 |
23 | v->x *= n;
24 | v->y *= n;
25 | }
26 |
27 | void divide_vector(struct vector2d* v, float n) {
28 |
29 | v->x /= n;
30 | v->y /= n;
31 | }
32 |
33 | void print_vector(struct vector2d* a) {
34 |
35 | printf("x = %f\n y = %f\n", a->x, a->y);
36 | }
37 |
38 | float magnitude_vector(struct vector2d* v) {
39 |
40 | float c2 = pow(v->x, 2) + pow(v->y, 2);
41 |
42 | return sqrt(c2);
43 | }
44 |
45 | void normalise_vector(struct vector2d* v) {
46 |
47 | float mag = magnitude_vector(v);
48 |
49 | divide_vector(v, mag);
50 | }
51 |
52 | void limit_vector(struct vector2d* v, float limit) {
53 |
54 | float mag = magnitude_vector(v);
55 |
56 | if (mag > limit) {
57 |
58 | float ratio = limit / mag;
59 | v->x *= ratio;
60 | v->y *= ratio;
61 | }
62 | }
63 |
64 | void rotate_vector(struct vector2d* v, float degrees) {
65 |
66 | //calculate radians
67 | float angle = degrees * M_PI / 180;
68 | float sine = sin(angle);
69 | float cosine = cos(angle);
70 |
71 | //rotation matix
72 | float matrix[2][2] = {{cosine, -sine}, {sine, cosine}};
73 |
74 | float x = v->x;
75 | float y = v->y;
76 |
77 | v->x = matrix[0][0] * x + matrix[0][1] * y;
78 | v->y = matrix[1][0] * x + matrix[1][1] * y;
79 | }
80 |
81 |
--------------------------------------------------------------------------------
/asteroids/vector.h:
--------------------------------------------------------------------------------
1 |
2 | //vector.h
3 |
4 | #ifndef VECTOR_H
5 | #define VECTOR_H
6 |
7 | struct vector2d {
8 |
9 | float x;
10 | float y;
11 | };
12 |
13 | void add_vector(struct vector2d* a, struct vector2d* b);
14 |
15 | struct vector2d add_vector_new(struct vector2d* a, struct vector2d* b);
16 |
17 | void multiply_vector(struct vector2d* v, float n);
18 |
19 | void divide_vector(struct vector2d* v, float n);
20 |
21 | void print_vector(struct vector2d* a);
22 |
23 | void rotate_vector(struct vector2d* v, float degrees);
24 |
25 | float magnitude_vector(struct vector2d* v);
26 |
27 | void normalise_vector(struct vector2d* v);
28 |
29 | void limit_vector(struct vector2d* v, float limit);
30 |
31 | #endif //VECTOR_H
32 |
--------------------------------------------------------------------------------
/screenshot.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/robertaboukhalil/wasm-asteroids/d9b08b1f0d16053d86c3353922d390bfb881f8f0/screenshot.png
--------------------------------------------------------------------------------