├── COPYING ├── Makefile ├── README.textile ├── levels ├── climb.lvl ├── m.lvl ├── out.lvl ├── stairs.lvl ├── steps.lvl └── triangle.lvl ├── physics.c ├── physics.h ├── raycaster.c ├── raycaster.h ├── textures ├── floor.tga ├── monster_aim.tga ├── monster_fire.tga ├── monster_stand.tga ├── monster_walk1.tga ├── monster_walk2.tga ├── sprite.tga ├── wall.tga └── wall.xcf ├── tga.c ├── tga.h ├── vector.c ├── vector.h ├── world.c └── world.h /COPYING: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc. 5 | 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 Library 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 | 294 | Copyright (C) 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 307 | along with this program; if not, write to the Free Software 308 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 309 | 310 | 311 | Also add information on how to contact you by electronic and paper mail. 312 | 313 | If the program is interactive, make it output a short notice like this 314 | when it starts in an interactive mode: 315 | 316 | Gnomovision version 69, Copyright (C) year name of author 317 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 318 | This is free software, and you are welcome to redistribute it 319 | under certain conditions; type `show c' for details. 320 | 321 | The hypothetical commands `show w' and `show c' should show the appropriate 322 | parts of the General Public License. Of course, the commands you use may 323 | be called something other than `show w' and `show c'; they could even be 324 | mouse-clicks or menu items--whatever suits your program. 325 | 326 | You should also get your employer (if you work as a programmer) or your 327 | school, if any, to sign a "copyright disclaimer" for the program, if 328 | necessary. Here is a sample; alter the names: 329 | 330 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 331 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 332 | 333 | , 1 April 1989 334 | Ty Coon, President of Vice 335 | 336 | This General Public License does not permit incorporating your program into 337 | proprietary programs. If your program is a subroutine library, you may 338 | consider it more useful to permit linking proprietary applications with the 339 | library. If this is what you want to do, use the GNU Library General 340 | Public License instead of this License. 341 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # Copyright (C) Matthew Earl 2 | # 3 | # This program is free software; you can redistribute it and/or 4 | # modify it under the terms of the GNU General Public License 5 | # as published by the Free Software Foundation; either version 2 6 | # of the License, or (at your option) any later version. 7 | # 8 | # This program is distributed in the hope that it will be useful, 9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 | # GNU General Public License for more details. 12 | # 13 | # You should have received a copy of the GNU General Public License 14 | # along with this program; if not, write to the Free Software 15 | # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 16 | 17 | CC=gcc 18 | CFLAGS=-Wall -O3 -ffast-math -funroll-loops -malign-double -fstrict-aliasing -ggdb 19 | #CFLAGS=-ggdb -Wall -pg 20 | #CFLAGS=-g 21 | 22 | all: raycaster 23 | 24 | raycaster: raycaster.o vector.o tga.o physics.o world.o 25 | $(CC) $(CFLAGS) physics.o tga.o raycaster.o vector.o world.o -o raycaster -lSDL -lpthread 26 | 27 | raycaster.o: raycaster.c raycaster.h vector.h world.h 28 | $(CC) $(CFLAGS) -c raycaster.c -o raycaster.o 29 | 30 | vector.o: vector.c 31 | $(CC) $(CFLAGS) -c vector.c -o vector.o 32 | 33 | tga.o: tga.c 34 | $(CC) $(CFLAGS) -c tga.c -o tga.o 35 | 36 | physics.o: physics.c raycaster.h world.h vector.h 37 | $(CC) $(CFLAGS) -c physics.c -o physics.o 38 | 39 | world.o: world.c raycaster.h world.h vector.h 40 | $(CC) $(CFLAGS) -c world.c -o world.o 41 | 42 | clean: 43 | -rm -f *.o raycaster gmon.out 44 | -------------------------------------------------------------------------------- /README.textile: -------------------------------------------------------------------------------- 1 | h1. Raycaster 2 | 3 | A small raycaster I wrote in C using SDL. Features doom-style levels: Walls are vertical but do not otherwise have to be axially aligned and it's not possible to put rooms on top of each other. Unlike Doom, it does not feature lighting. 4 | 5 | "YouTube video.":http://youtu.be/iuuhSg8GiLg 6 | 7 | -------------------------------------------------------------------------------- /levels/climb.lvl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/matthewearl/Raycaster/50a3a22e99342f2bcf2c20b439516586666b6df1/levels/climb.lvl -------------------------------------------------------------------------------- /levels/m.lvl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/matthewearl/Raycaster/50a3a22e99342f2bcf2c20b439516586666b6df1/levels/m.lvl -------------------------------------------------------------------------------- /levels/out.lvl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/matthewearl/Raycaster/50a3a22e99342f2bcf2c20b439516586666b6df1/levels/out.lvl -------------------------------------------------------------------------------- /levels/stairs.lvl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/matthewearl/Raycaster/50a3a22e99342f2bcf2c20b439516586666b6df1/levels/stairs.lvl -------------------------------------------------------------------------------- /levels/steps.lvl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/matthewearl/Raycaster/50a3a22e99342f2bcf2c20b439516586666b6df1/levels/steps.lvl -------------------------------------------------------------------------------- /levels/triangle.lvl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/matthewearl/Raycaster/50a3a22e99342f2bcf2c20b439516586666b6df1/levels/triangle.lvl -------------------------------------------------------------------------------- /physics.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) Matthew Earl 3 | * 4 | * This program is free software; you can redistribute it and/or 5 | * modify it under the terms of the GNU General Public License 6 | * as published by the Free Software Foundation; either version 2 7 | * of the License, or (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program; if not, write to the Free Software 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 17 | */ 18 | 19 | #include 20 | #include "raycaster.h" 21 | #include "physics.h" 22 | #include "vector.h" 23 | #include "world.h" 24 | 25 | #define TRIG_ANGLES 512 26 | #define TRIG_ANGLES_OVER_4 128 27 | 28 | float 29 | sinlookup[TRIG_ANGLES]; 30 | 31 | void 32 | inittriglookup( void ) 33 | { 34 | int i; 35 | for(i=0;icurrentplatform,*prevprevplat=NULL;; 61 | int headclip; 62 | float pushextra; 63 | 64 | if(edgeintersect(r,p->currentplatform,dir,origin,0.0f,&in,NULL) == NULL) 65 | return; 66 | while(0<1) 67 | { 68 | if(dist <= in.distance) 69 | break; 70 | prevprevplat=prevplat; 71 | prevplat=in.platform; 72 | if(in.platform == &r->level.infplatform) 73 | break; 74 | 75 | if(p->vpos + VIEW_HEIGHT >= prevplat->ceilheight) 76 | break; 77 | if(prevplat->ceilheight - prevplat->floorheight < VIEW_HEIGHT) 78 | break; 79 | 80 | if(edgeintersect(r,in.platform,dir,&in.pos,in.distance,&in,NULL) == NULL) 81 | return; 82 | } 83 | vectorcopy(&origviewpos,&p->pos); 84 | vectorscale(dir,dist,&temp); 85 | vectoradd(&temp,&p->pos,&p->pos); 86 | if(!prevprevplat) 87 | return; 88 | headclip = (p->vpos + VIEW_HEIGHT >= prevplat->ceilheight); 89 | if(!headclip && prevplat->floorheight < p->vpos) 90 | { 91 | p->onground = 0; 92 | p->vvel = 0.0f; 93 | p->currentplatform = prevplat; 94 | return; 95 | } 96 | headclip = (prevplat->floorheight + VIEW_HEIGHT >= prevplat->ceilheight); 97 | if(!headclip && prevplat->floorheight <= p->vpos + STEP_HEIGHT) 98 | { 99 | p->currentplatform = prevplat; 100 | p->vpos = prevplat->floorheight; 101 | return; 102 | } 103 | 104 | if(in.edge->rightplat == p->currentplatform) 105 | { 106 | pushextra = -PUSH_EXTRA; 107 | } else 108 | { 109 | pushextra = PUSH_EXTRA; 110 | } 111 | 112 | vectorscale(&in.edge->normal, 113 | pushextra+dotproduct(dir,&in.edge->normal),&temp); 114 | vectorsubtract(dir,&temp,dir); 115 | dist = vectorlength ( dir ); 116 | vectorscale( dir, 1.0f/dist, dir ); 117 | moveplayer ( r, p, origin, dir, dist ); 118 | 119 | /* vectorcopy(&p->pos,&origviewpos);*/ 120 | } 121 | 122 | void 123 | dogravity ( entity_t *p, int dt ) 124 | { 125 | float dtf = 0.001f*(float)dt; 126 | if(p->onground) 127 | return; 128 | p->vvel -= GRAVITY*dtf; 129 | p->vpos += p->vvel*dtf; 130 | if(p->vpos <= p->currentplatform->floorheight) 131 | { 132 | p->vpos = p->currentplatform->floorheight; 133 | p->onground = 1; 134 | p->vvel = 0.0f; 135 | } 136 | } 137 | 138 | void 139 | physics_monster ( raycaster_t *r, world_t *w, entity_t *e, int dt ) 140 | { 141 | if(e->wishdir.x == 0.0f && e->wishdir.y == 0.0f) 142 | return; 143 | 144 | moveplayer ( r, e, &e->pos, &e->wishdir, ((float)dt)*0.001f*MONSTER_RUNSPEED); 145 | dogravity(e,dt); 146 | } 147 | 148 | void 149 | physics_player ( raycaster_t *r, world_t *w, entity_t *p, int dt ) 150 | { 151 | vector2d_t forward,right,temp; 152 | float forwardf=0.0f,rightf=0.0f,dist,dtf,turn=0.0f; 153 | 154 | if(p->keys == 0 && p->onground && r->mousespeed.x == 0.0f) 155 | return; 156 | 157 | dtf = (float)dt*0.001f; 158 | 159 | if(p->keys & KEY_TRIGHT) 160 | turn += TURN_SPEED; 161 | 162 | if(p->keys & KEY_TLEFT) 163 | turn -= TURN_SPEED; 164 | 165 | turn -= r->mousespeed.x * MOUSE_SENS; 166 | 167 | vectorrotangle(&p->angle, turn*dtf, &p->angle); 168 | 169 | vectorcopy(&forward,&p->angle); 170 | vectorrot90(&forward,&right); 171 | 172 | if(p->keys & KEY_FORWARD) 173 | forwardf += 1.0f; 174 | 175 | if(p->keys & KEY_BACK) 176 | forwardf -= 1.0f; 177 | 178 | if(p->keys & KEY_RIGHT) 179 | rightf += 1.0f; 180 | 181 | if(p->keys & KEY_LEFT) 182 | rightf -= 1.0f; 183 | 184 | if(forwardf != 0.0f || rightf != 0.0f) 185 | { 186 | dist = RUN_SPEED*dtf; 187 | rightf *= dtf; 188 | forwardf *= dtf; 189 | 190 | if(forwardf != 0.0f && rightf != 0.0f) 191 | { 192 | forwardf*=0.707107f; /* 1/sqrt(2) */ 193 | rightf*=0.707107f; 194 | } 195 | 196 | vectorscale(&forward,forwardf,&temp); 197 | vectorscale(&right,rightf,&right); 198 | vectoradd(&temp,&right,&temp); 199 | 200 | moveplayer(r,p,&p->pos,&temp,dist); 201 | } 202 | dogravity(p,dt); 203 | } 204 | 205 | void 206 | dophysics ( raycaster_t *r, world_t *w, int dt ) 207 | { 208 | int i; 209 | entity_t *e; 210 | 211 | for(i=0;inumentities;i++) 212 | { 213 | e = &w->entities[i]; 214 | if(!e->physics) 215 | continue; 216 | e->physics(r,w,e,dt); 217 | } 218 | } 219 | 220 | -------------------------------------------------------------------------------- /physics.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) Matthew Earl 3 | * 4 | * This program is free software; you can redistribute it and/or 5 | * modify it under the terms of the GNU General Public License 6 | * as published by the Free Software Foundation; either version 2 7 | * of the License, or (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program; if not, write to the Free Software 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 17 | */ 18 | 19 | #ifndef _PHYSICS_H_ 20 | #define _PHYSICS_H_ 21 | 22 | #include "raycaster.h" 23 | #include "world.h" 24 | 25 | #define KEY_FORWARD 1 26 | #define KEY_RIGHT 2 27 | #define KEY_LEFT 4 28 | #define KEY_BACK 8 29 | #define KEY_TRIGHT 16 30 | #define KEY_TLEFT 32 31 | 32 | #define RUN_SPEED 20000.0f /* units per second */ 33 | #define TURN_SPEED -3.0f /* radians per second */ 34 | #define GRAVITY 2048.0f /* units per second^2 */ 35 | #define MOUSE_SENS 0.01f /* radians per pixel */ 36 | 37 | #define MONSTER_RUNSPEED 100.0f /* units per second */ 38 | 39 | void dophysics ( raycaster_t *r, world_t *w, int dt ); 40 | void physics_player ( raycaster_t *r, world_t *w, entity_t *e, int dt ); 41 | void physics_monster ( raycaster_t *r, world_t *w, entity_t *e, int dt ); 42 | #endif 43 | 44 | -------------------------------------------------------------------------------- /raycaster.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) Matthew Earl 3 | * 4 | * This program is free software; you can redistribute it and/or 5 | * modify it under the terms of the GNU General Public License 6 | * as published by the Free Software Foundation; either version 2 7 | * of the License, or (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program; if not, write to the Free Software 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 17 | */ 18 | 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include "raycaster.h" 25 | #include "world.h" 26 | #include "vector.h" 27 | #include "physics.h" 28 | #include "tga.h" 29 | 30 | #define SCREEN_WIDTH 1024 31 | #define SCREEN_HEIGHT 768 32 | 33 | #define DEFAULT_LEVEL "levels/out.lvl" 34 | 35 | #define PACKED __attribute__((packed)) 36 | 37 | /* Edge structure, as stored in the file. */ 38 | typedef struct fedge_s 39 | { 40 | int32_t vertrefs[2]; 41 | int32_t leftplatref,rightplatref; 42 | } PACKED fedge_t; 43 | 44 | /* Vector structure, as stored in the file. */ 45 | typedef struct fvector2d_s 46 | { 47 | float x,y; 48 | } PACKED fvector2d_t; 49 | 50 | /* Vertex structure, as stored in the file. */ 51 | typedef struct fvert_s 52 | { 53 | vector2d_t pos; 54 | int32_t numedges; 55 | uint32_t dummy; 56 | } PACKED fvert_t; 57 | 58 | /* Platform structure, as stored in the file. */ 59 | typedef struct fplatform_s 60 | { 61 | float ceilheight,floorheight; 62 | int32_t numedges; 63 | uint32_t dummy; 64 | } PACKED fplatform_t; 65 | 66 | /* Level structure, as stored in the file. */ 67 | typedef struct levelfile_s 68 | { 69 | int32_t numedges; 70 | uint32_t dummy1; 71 | 72 | int32_t numplatforms; 73 | uint32_t dummy2; 74 | fplatform_t infplatform; 75 | 76 | int32_t numverts; 77 | uint32_t dummy3; 78 | vector2d_t size; 79 | } PACKED levelfile_t; 80 | 81 | /* Intermediate vertex structure, used when loading the level. */ 82 | typedef struct ivert_s 83 | { 84 | fvert_t f; 85 | int32_t *edgerefs; 86 | } ivert_t; 87 | 88 | /* Intermediate platform structure, used when loading the level. */ 89 | typedef struct iplatform_s 90 | { 91 | fplatform_t f; 92 | int32_t *edgerefs; 93 | } iplatform_t; 94 | 95 | 96 | #define FINAL_FLOOR 1 97 | #define FINAL_CEILING 2 98 | 99 | #define PRECISION_BITS 8 100 | #define DOUBLE_PRECISION_BITS (2*PRECISION_BITS) 101 | #define PRECISION_PRODUCT ((float)(1<width,&t->log2width)) 126 | return 0; 127 | if(!intlog2(t->height,&t->log2height)) 128 | return 0; 129 | return 1; 130 | } 131 | 132 | int 133 | loadtexture ( texture_t *t, raycaster_t *r, char *filename ) 134 | { 135 | byte *bpixel; 136 | int x,y; 137 | bitmap_t b; 138 | 139 | snprintf(t->path, sizeof(t->path), "textures/%s", filename); 140 | t->pixels = NULL; 141 | 142 | if(!loadTGA(t->path,&b)) 143 | return 0; 144 | t->width = b.width; 145 | t->height = b.height; 146 | 147 | t->widthmask = t->width-1; 148 | t->heightmask = t->height-1; 149 | 150 | t->widthmaskshift = (t->width<heightmaskshift = (t->height<widthmasksmallshift = (t->width<heightmasksmallshift = (t->height<path,t->width,t->height); 159 | freeTGA(&b); 160 | return 0; 161 | } 162 | 163 | t->pixels = (unsigned short*)malloc(sizeof(unsigned short)*b.width*b.height); 164 | for(x=0;xpixels[y+x*b.height] = 170 | SDL_MapRGB(r->screen->format,bpixel[2],bpixel[1],bpixel[0]); 171 | } 172 | } 173 | freeTGA(&b); 174 | return 1; 175 | } 176 | 177 | texture_t * 178 | allocatetexture ( level_t *l ) 179 | { 180 | texture_t *new; 181 | 182 | new = (texture_t*)malloc(sizeof(texture_t)); 183 | 184 | new->next = NULL; 185 | if(!l->lasttexture) 186 | { 187 | l->texturelist = l->lasttexture = new; 188 | new->prev = NULL; 189 | return new; 190 | } 191 | l->lasttexture->next = new; 192 | new->prev = l->lasttexture; 193 | l->lasttexture = new; 194 | 195 | return new; 196 | } 197 | 198 | texture_t * 199 | texturefrompath ( raycaster_t *r, char *path ) 200 | { 201 | texture_t *t; 202 | 203 | for(t=r->level.texturelist;t;t=t->next) 204 | { 205 | if(!strcmp(path,t->path)) 206 | return t; 207 | } 208 | printf("loading texture %s...\n", path); 209 | t = allocatetexture ( &r->level ); 210 | if(!loadtexture( t, r, path )) 211 | return NULL; 212 | 213 | return t; 214 | } 215 | 216 | unsigned short 217 | gettexturepixel ( texture_t *t, int x, int y ) 218 | { 219 | return t->pixels[y+(x<log2height)]; 220 | } 221 | 222 | void 223 | freetexture ( texture_t *t ) 224 | { 225 | if(t->pixels) 226 | { 227 | free(t->pixels); 228 | t->pixels = NULL; 229 | } 230 | 231 | } 232 | 233 | /**************************************************************/ 234 | 235 | int 236 | startsdl( raycaster_t *r ) 237 | { 238 | if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) == -1) 239 | { 240 | printf("Failed to start SDL\n"); 241 | return 0; 242 | } 243 | 244 | r->screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 16, SDL_HWSURFACE|SDL_HWPALETTE); 245 | if(!r->screen) 246 | { 247 | fprintf(stderr, "Unable to set video: %s\n", SDL_GetError()); 248 | return 0; 249 | } 250 | return 1; 251 | } 252 | 253 | void 254 | convertedge ( raycaster_t *r, level_t *l, fedge_t *fe, edge_t *e ) 255 | { 256 | int i; 257 | 258 | e->texture = texturefrompath(r,"wall.tga"); 259 | for(i=0;i<2;i++) 260 | e->verts[i] = &l->verts[fe->vertrefs[i]]; 261 | if(fe->leftplatref == -1) 262 | e->leftplat = &l->infplatform; 263 | else 264 | e->leftplat = &l->platforms[fe->leftplatref]; 265 | 266 | if(fe->rightplatref == -1) 267 | e->rightplat = &l->infplatform; 268 | else 269 | e->rightplat = &l->platforms[fe->rightplatref]; 270 | 271 | vectorsubtract(&e->verts[1]->pos,&e->verts[0]->pos,&e->line); 272 | vectornormalise(&e->line,&e->line); 273 | vectorrot90(&e->line,&e->normal); 274 | e->planedist = dotproduct(&e->normal,&e->verts[0]->pos); 275 | } 276 | 277 | void 278 | convertvert ( raycaster_t *r, level_t *l, ivert_t *iv, vert_t *v ) 279 | { 280 | int i; 281 | v->pos.x = iv->f.pos.x; 282 | v->pos.y = iv->f.pos.y; 283 | v->numedges = iv->f.numedges; 284 | 285 | v->edges = (edge_t**)malloc(sizeof(edge_t*)*iv->f.numedges); 286 | for(i=0;inumedges;i++) 287 | v->edges[i] = &l->edges[iv->edgerefs[i]]; 288 | } 289 | 290 | void 291 | convertplatform ( raycaster_t *r, level_t *l, iplatform_t *ip, platform_t *p ) 292 | { 293 | int i; 294 | 295 | p->ceilheight = ip->f.ceilheight; 296 | p->floorheight = ip->f.floorheight; 297 | 298 | p->numedges = ip->f.numedges; 299 | p->edges = (edge_t**)malloc(sizeof(edge_t*)*ip->f.numedges); 300 | for(i=0;if.numedges;i++) 301 | { 302 | p->edges[i] = &l->edges[ip->edgerefs[i]]; 303 | } 304 | p->texture = texturefrompath(r,"floor.tga"); 305 | p->allocatedsprites = 4; 306 | p->sprites = (sprite_t**)malloc(p->allocatedsprites*sizeof(sprite_t*)); 307 | p->numsprites = 0; 308 | 309 | } 310 | 311 | void 312 | optimiseplatform ( raycaster_t *r, level_t *l, platform_t *p) 313 | { 314 | int i,changeceil=1,changefloor=1,firstfloor=1,firstceil=1; 315 | platform_t *n; /* Neighbouring platform. */ 316 | float highest=0.0f,lowest=0.0f; 317 | 318 | /* If the platform's floor is higher than all its neighbours' ceilings then 319 | * set the platform's floor height to the maximum of its neighbours 320 | * ceilings. This is to minimise overdraw. 321 | * 322 | * Do similar if the platform's ceiling is lower than all of its 323 | * neighbours' floors. 324 | */ 325 | for(i=0;inumedges;i++) 326 | { 327 | if(p->edges[i]->leftplat != p) 328 | n = p->edges[i]->leftplat; 329 | else 330 | n = p->edges[i]->rightplat; 331 | if(p->floorheight > n->ceilheight) 332 | { 333 | if(firstfloor || n->ceilheight > highest) 334 | highest = n->ceilheight; 335 | firstfloor = 0; 336 | } else 337 | { 338 | changefloor = 0; 339 | } 340 | 341 | if(p->ceilheight < n->floorheight) 342 | { 343 | if(firstceil || n->floorheight < lowest) 344 | lowest = n->floorheight; 345 | firstceil = 0; 346 | } else 347 | { 348 | changeceil = 0; 349 | } 350 | } 351 | if(changefloor) 352 | p->floorheight = highest+1.0f; 353 | if(changeceil) 354 | p->ceilheight = lowest-1.0f; 355 | } 356 | 357 | int 358 | loadlevel ( raycaster_t *r, char *filename ) 359 | { 360 | FILE *f; 361 | levelfile_t lf; 362 | level_t *l=&r->level; 363 | int i; 364 | fedge_t *fedges; 365 | iplatform_t *iplatforms; 366 | iplatform_t iinfplatform; 367 | ivert_t *iverts; 368 | 369 | f=fopen(filename,"rb"); 370 | if(!f) 371 | { 372 | printf("Could not open file %s\n", filename); 373 | return 0; 374 | } 375 | 376 | fread(&lf,sizeof(levelfile_t),1,f); 377 | 378 | fedges = (fedge_t*)malloc(sizeof(fedge_t)*lf.numedges); 379 | for(i=0;inumedges = lf.numedges; 410 | l->numplatforms = lf.numplatforms; 411 | l->numverts = lf.numverts; 412 | l->size.x = lf.size.x; 413 | l->size.y = lf.size.y; 414 | 415 | l->edges = (edge_t*)malloc(sizeof(edge_t)*l->numedges); 416 | l->platforms = (platform_t*)malloc(sizeof(platform_t)*l->numplatforms); 417 | l->verts = (vert_t*)malloc(sizeof(vert_t)*l->numverts); 418 | 419 | for(i=0;iverts[i]); 421 | for(i=0;iedges[i]); 423 | for(i=0;iplatforms[i]); 425 | convertplatform(r,l,&iinfplatform,&l->infplatform); 426 | 427 | for(i=0;iplatforms[i]); 429 | optimiseplatform(r,l,&l->infplatform); 430 | 431 | /* 432 | * Cleanup. 433 | */ 434 | for(i=0;iplayerentity->keys |= movekey; 478 | else 479 | w->playerentity->keys &= ~movekey; 480 | } 481 | 482 | void 483 | handleevents( raycaster_t *r, world_t *w, int *done ) 484 | { 485 | SDL_Event event; 486 | 487 | while(SDL_PollEvent(&event)) 488 | { 489 | switch (event.type) 490 | { 491 | case SDL_KEYDOWN: 492 | handlekeypress(r,w,event.key.keysym.sym,done,1); 493 | break; 494 | case SDL_KEYUP: 495 | handlekeypress(r,w,event.key.keysym.sym,done,0); 496 | break; 497 | case SDL_QUIT: 498 | *done = 1; 499 | break; 500 | case SDL_MOUSEMOTION: 501 | r->cursorx=event.motion.x; 502 | r->cursory=event.motion.y; 503 | 504 | break; 505 | default: 506 | break; 507 | } 508 | } 509 | } 510 | 511 | #define HUNK_SPRITES 4 512 | 513 | void 514 | addspritetolist ( raycaster_t *r, sprite_t **i, float mingrad, float maxgrad ) 515 | { 516 | if(!(*i)->renderflag) 517 | { 518 | if(r->numsprites == r->allocatedsprites) 519 | { 520 | r->allocatedsprites+=HUNK_SPRITES; 521 | r->spritelist = (sprite_t**)realloc(r->spritelist, 522 | sizeof(sprite_t*)*r->allocatedsprites); 523 | } 524 | memcpy(&r->spritelist[r->numsprites++],i,sizeof(sprite_t*)); 525 | 526 | (*i)->renderflag = 1; 527 | } 528 | 529 | (*i)->mingrad = mingrad; 530 | (*i)->maxgrad = maxgrad; 531 | 532 | return; 533 | } 534 | 535 | #define DIST_THRESHOLD 0.1f 536 | 537 | int 538 | linelineintersect ( vector2d_t *origin, vector2d_t *dir, vector2d_t *vert1, 539 | vector2d_t *vert2, vector2d_t *normal, vector2d_t *normalrot, 540 | float *dist, float *texoffset, vector2d_t *poi ) 541 | { 542 | float d,dp1,dp2; 543 | 544 | d=dotproduct(dir,normal); 545 | if(d==0.0f) 546 | return 0; 547 | 548 | *dist = (dotproduct(vert1,normal)-dotproduct(origin,normal)); 549 | 550 | if((*dist <= 0.0f && d > 0.0f) || 551 | (*dist > 0.0f && d <= 0.0f)) 552 | return 0; 553 | 554 | *dist/=d; 555 | 556 | vectorscale(dir,*dist,poi); 557 | vectoradd(poi,origin,poi); 558 | 559 | d = dotproduct(poi,normalrot); 560 | dp1 = dotproduct(vert1,normalrot); 561 | dp2 = dotproduct(vert2,normalrot); 562 | 563 | if(texoffset) 564 | *texoffset = d-dp1; 565 | 566 | if( (d>=dp1 && d<=dp2) || (d<=dp1 && d>=dp2) ) 567 | return 1; 568 | return 0; 569 | } 570 | 571 | /* check for intersections of the passed ray with the specified platform */ 572 | intersection_t * 573 | edgeintersect ( raycaster_t *r, platform_t *p, vector2d_t *dir, 574 | vector2d_t *passedorigin, float prevdist, 575 | intersection_t *intersection, edge_t *ignoreedge) 576 | { 577 | vector2d_t poi,origin; 578 | int i,first,done; 579 | float dist,mindist=0.0f,texoffset; 580 | platform_t *nextplatform; 581 | intersection_t in; 582 | edge_t *e; 583 | 584 | vectorcopy(&origin,passedorigin); 585 | 586 | first = 1; 587 | done = 0; 588 | for(i=0;inumedges;i++) 589 | { 590 | e=p->edges[i]; 591 | 592 | if(!linelineintersect(&origin,dir,&e->verts[0]->pos, 593 | &e->verts[1]->pos,&e->normal,&e->line, 594 | &dist,&texoffset,&poi)) 595 | { 596 | continue; 597 | } 598 | 599 | // Determine what the next platform is, and check that it's in the forward 600 | // direction. 601 | if(e->leftplat != p) 602 | { 603 | if (dotproduct(&e->normal, dir) > 0.0f) 604 | continue; 605 | nextplatform = e->leftplat; 606 | } 607 | else if(e->rightplat != p) 608 | { 609 | if (dotproduct(&e->normal, dir) < 0.0f) 610 | continue; 611 | nextplatform = e->rightplat; 612 | } 613 | else 614 | { 615 | printf("edgeintersect() problem\n"); 616 | nextplatform = NULL; 617 | } 618 | 619 | if(first || dist < mindist) 620 | { 621 | mindist = dist; 622 | first = 0; 623 | } else 624 | { 625 | continue; 626 | } 627 | 628 | vectorcopy(&in.pos,&poi); 629 | in.distance = dist + prevdist; 630 | in.edge = e; 631 | in.final = 0; 632 | in.platform = nextplatform; 633 | 634 | in.texoffset = texoffset; 635 | done = 1; 636 | } 637 | if(!done) 638 | { 639 | printf("No intersection!\n"); 640 | return NULL; 641 | } 642 | memcpy(intersection,&in,sizeof(intersection_t)); 643 | return intersection; 644 | } 645 | 646 | /* sets intersection to the nearest point on the line where the cylinder either 647 | * leaves a platform or enters a platform 648 | */ 649 | void 650 | cylinderintersect ( raycaster_t *r, platform_t **plats, int nplats, 651 | vector2d_t *dir, vector2d_t *passedorigin, float radius 652 | ,float prevdist, solidintersection_t *intersection ) 653 | { 654 | int i,j; 655 | platform_t *plat; 656 | edge_t *e; 657 | float mindist = -1.0f,dist; 658 | vector2d_t verts[2],push; 659 | vector2d_t poi; 660 | 661 | for(i=0;inumedges;j++) 665 | { 666 | e = plat->edges[j]; 667 | vectorscale(&e->normal,radius,&push); 668 | 669 | vectoradd(&e->verts[0]->pos,&push,&verts[0]); 670 | vectoradd(&e->verts[1]->pos,&push,&verts[1]); 671 | 672 | if(linelineintersect(passedorigin,dir,&verts[0],&verts[1], 673 | &e->normal,&e->line,&dist,NULL,&poi) && 674 | (dist < mindist || mindist < 0.0f)) 675 | { 676 | intersection->distance = dist; 677 | intersection->edge = e; 678 | if(e->rightplat != plat) 679 | { 680 | intersection->type = INTERSECTION_LEAVING; 681 | } else 682 | { 683 | intersection->type = INTERSECTION_ENTRY; 684 | } 685 | intersection->platform = e->leftplat; 686 | mindist = dist; 687 | } 688 | 689 | vectorsubtract(&e->verts[0]->pos,&push,&verts[0]); 690 | vectorsubtract(&e->verts[1]->pos,&push,&verts[1]); 691 | 692 | if(linelineintersect(passedorigin,dir,&verts[0],&verts[1], 693 | &e->normal,&e->line,&dist,NULL,&poi) && 694 | (dist < mindist || mindist < 0.0f)) 695 | { 696 | intersection->distance = dist; 697 | intersection->edge = e; 698 | if(e->rightplat == plat) 699 | { 700 | intersection->type = INTERSECTION_LEAVING; 701 | } else 702 | { 703 | intersection->type = INTERSECTION_ENTRY; 704 | } 705 | intersection->platform = e->rightplat; 706 | mindist = dist; 707 | } 708 | 709 | } 710 | } 711 | } 712 | 713 | /* checkceiling 714 | * 715 | * Determines if a platform's ceiling appears lower on the screen than 716 | * the previous lowest ceiling 717 | */ 718 | int 719 | checkceiling ( raycaster_t *r, platform_t *p, float g, float *best, 720 | intersection_t **in, intersection_t *current , int first ) 721 | { 722 | if(first || g < *best) 723 | { 724 | *best = g; 725 | *in = current; 726 | return 1; 727 | } 728 | return 0; 729 | } 730 | 731 | /* checkfloor 732 | * 733 | * Determines if a platform's floor appears higher on the screen than 734 | * the previous highest floor 735 | */ 736 | int 737 | checkfloor ( raycaster_t *r, platform_t *p, float g, float *best, 738 | intersection_t **in, intersection_t *current , int first ) 739 | { 740 | if(first || g > *best) 741 | { 742 | *best = g; 743 | *in = current; 744 | return 1; 745 | } 746 | return 0; 747 | } 748 | 749 | /* checkdone 750 | * 751 | * Determine if the highest floor is above the lowest ceiling 752 | * ie. our view has been obscured and we can not see beyond these 753 | * platforms 754 | */ 755 | int 756 | checkdone( int first, float high, float low, intersection_t *hi, 757 | intersection_t *li ) 758 | { 759 | if(first) 760 | return 0; 761 | if(high > low) 762 | { 763 | hi->final |= FINAL_FLOOR; 764 | li->final |= FINAL_CEILING; 765 | return 1; 766 | } 767 | return 0; 768 | } 769 | 770 | #define SCREEN_DISTANCE 1.0f 771 | #define TAN_FOV 1.0f /* tan(45) */ 772 | #define HALF_SCREEN_HEIGHT (SCREEN_HEIGHT>>1) 773 | 774 | float pixeltogradcoefficent; 775 | float 776 | pixeltogradslow ( int pixel ) 777 | { 778 | return (float)(pixel-HALF_SCREEN_HEIGHT)*pixeltogradcoefficent; 779 | } 780 | 781 | float pixeltograd[SCREEN_HEIGHT]; 782 | float invpixeltograd[SCREEN_HEIGHT]; 783 | int invpixeltogradint[SCREEN_HEIGHT]; 784 | void 785 | pixeltogradinit ( void ) 786 | { 787 | int i=0; 788 | for(i=0;i SCREEN_HEIGHT-1) 819 | p1 = SCREEN_HEIGHT-1; 820 | if(p2 < 0) 821 | p2 = 0; 822 | else if(p2 > SCREEN_HEIGHT-1) 823 | p2 = SCREEN_HEIGHT-1; 824 | 825 | pixel = ((unsigned short*)r->screen->pixels)+(p1)*SCREEN_WIDTH+(x); 826 | for(y=p1;yscreen->format, re,g,b); 829 | pixel += SCREEN_WIDTH; 830 | } 831 | } 832 | 833 | int 834 | propermodulo( int a, int b ) 835 | { 836 | if(a<0) 837 | { 838 | while(a<0) 839 | a+=b; 840 | return a; 841 | } 842 | return a%b; 843 | } 844 | 845 | /* drawwall 846 | * 847 | * Draw a piece of a wall, between gradients g1 and g2 848 | */ 849 | inline void 850 | drawwall ( raycaster_t *r, intersection_t *in, float g1, float g2, int x ) 851 | { 852 | unsigned short *pixel,*tpixel; 853 | int p1,p2,y,tx,ty,i,h1,h2; 854 | texture_t *t=in->edge->texture; 855 | 856 | p1 = gradtopixel(g1); 857 | p2 = gradtopixel(g2); 858 | if(p2 == p1) 859 | return; 860 | 861 | /* Clamp p1,p2 and the corresponding values of h1,h2 to the screen. 862 | */ 863 | if(p1 < 0) 864 | p1 = 0; 865 | 866 | if(p2 > SCREEN_HEIGHT-1) 867 | p2 = SCREEN_HEIGHT-1; 868 | 869 | h1 = (int)(PRECISION_PRODUCT * (r->eyelevel + pixeltograd[p1] * in->distance)); 870 | h2 = (int)(PRECISION_PRODUCT * (r->eyelevel + pixeltograd[p2] * in->distance)); 871 | 872 | tx = ((int)in->texoffset)&(t->widthmask); 873 | i = ((h2-h1)/(p2-p1)) & t->heightmasksmallshift; 874 | ty = h1 & t->heightmasksmallshift; 875 | pixel = ((unsigned short*)r->screen->pixels)+(p1)*SCREEN_WIDTH+(x); 876 | tpixel = &t->pixels[tx<log2height]; 877 | for(y=p1;y>PRECISION_BITS]; 880 | 881 | /* Recalculate the vertical texture pixel `ty`. Most of the time do this by 882 | * adding on a constant but periodically do an expensive recalculation. This is 883 | * to mitigate acculated rounding errors in the increment value `i`. 884 | */ 885 | if ((y & 0xF) == 0) 886 | ty = ((h2 * (y - p1) + h1 * (p2 - y)) / (p2 - p1)) & t->heightmasksmallshift; 887 | else 888 | ty = (ty+i)&(t->heightmasksmallshift); 889 | pixel += SCREEN_WIDTH; 890 | } 891 | } 892 | 893 | 894 | /* drawfloor 895 | * 896 | * h is the distance the floor is above or below current eye level 897 | * it is required for correct texture mapping 898 | */ 899 | inline void 900 | drawfloor ( raycaster_t *r, platform_t *p, float h, 901 | vector2d_t *dir, float g1, float g2, int x ) 902 | { 903 | unsigned short *pixel; 904 | int p1,p2,y,ty,tx;; 905 | int hdirx,hdiry,ox,oy; 906 | texture_t *t; 907 | 908 | p1 = gradtopixel(g1); 909 | p2 = gradtopixel(g2); 910 | 911 | if(p1 < 0) 912 | p1 = 0; 913 | 914 | if(p2 > SCREEN_HEIGHT-1) 915 | p2 = SCREEN_HEIGHT-1; 916 | 917 | pixel = ((unsigned short*)r->screen->pixels)+(p1)*SCREEN_WIDTH+(x); 918 | hdirx = (int)(dir->x*h*PRECISION_PRODUCT); 919 | hdiry = (int)(dir->y*h*PRECISION_PRODUCT); 920 | ox = ((int)r->viewpos.x)<viewpos.y)<texture; 924 | for(y=p1;ytexture->widthmaskshift); 927 | ty = (hdiry*invpixeltogradint[y]+oy)&(p->texture->heightmaskshift); 928 | 929 | *pixel = t->pixels[(ty>>DOUBLE_PRECISION_BITS)+((tx>>DOUBLE_PRECISION_BITS)<log2height)]; 930 | 931 | pixel += SCREEN_WIDTH; 932 | } 933 | } 934 | 935 | static float clamp(float v, float low, float high) 936 | { 937 | return v < low ? low : (v > high ? high : v); 938 | } 939 | 940 | void 941 | drawsprite ( raycaster_t *r, sprite_t *s, vector2d_t *dir, int x ) 942 | { 943 | float sprmingrad,sprmaxgrad,mingr,maxgr; 944 | int i,y; 945 | int p1,p2,p1b,p2b,ty,tx,h1=s->heights[0],h2=s->heights[1]; 946 | unsigned short *pixel,*tpixel; 947 | texture_t *t=s->texture; 948 | 949 | sprmingrad = (s->heights[0]-r->eyelevel)/s->dist; 950 | sprmaxgrad = (s->heights[1]-r->eyelevel)/s->dist; 951 | 952 | p1b = gradtopixel(sprmaxgrad); 953 | p2b = gradtopixel(sprmingrad); 954 | 955 | if(sprmingrad > s->mingrad) 956 | { 957 | mingr = sprmingrad; 958 | p2 = p2b; 959 | } else 960 | { 961 | mingr = s->mingrad; 962 | p2 = gradtopixel(mingr); 963 | } 964 | 965 | if(sprmaxgrad < s->maxgrad) 966 | { 967 | maxgr = sprmaxgrad; 968 | p1 = p1b; 969 | } else 970 | { 971 | maxgr = s->maxgrad; 972 | p1 = gradtopixel(maxgr); 973 | } 974 | 975 | if(p1 < 0) 976 | p1 = 0; 977 | if(p2 > SCREEN_HEIGHT-1) 978 | p2 = SCREEN_HEIGHT-1; 979 | 980 | tx = ((int)s->texoffset)%(t->widthmask); 981 | pixel = ((unsigned short*)r->screen->pixels)+(p1)*SCREEN_WIDTH+(x); 982 | tpixel = &t->pixels[tx<log2height]; 983 | ty = ((((p1-p1b)*(h2-h1))<heightmasksmallshift); 985 | i = ((h2-h1)<>PRECISION_BITS] != r->transpixel) 990 | *pixel = tpixel[ty>>PRECISION_BITS]; 991 | ty = (ty+i)&(t->heightmasksmallshift); 992 | pixel += SCREEN_WIDTH; 993 | } 994 | } 995 | 996 | void 997 | drawsprites( raycaster_t *r, vector2d_t *dir, int x ) 998 | { 999 | int i; 1000 | sprite_t *s; 1001 | 1002 | for(i=0;inumsprites;i++) 1003 | { 1004 | s = r->spritelist[i]; 1005 | drawsprite(r,s,dir,x); 1006 | s->renderflag = 0; 1007 | } 1008 | } 1009 | 1010 | /* drawcolumn 1011 | * 1012 | * This function draws a vertical line of pixels representing 1013 | * the world. 1014 | * 1015 | * x is the column we are drawing. 1016 | */ 1017 | void 1018 | drawcolumn ( raycaster_t *r, vector2d_t *dir, int x ) 1019 | { 1020 | int i; 1021 | intersection_t in; 1022 | float floorgrad, ceilgrad; 1023 | float prevfloorgrad, prevceilgrad; 1024 | float maxfloorgrad, minceilgrad; 1025 | float g1,g2; 1026 | float prevdist; 1027 | platform_t *prevplat; 1028 | sprite_t *s; 1029 | vector2d_t poi; 1030 | 1031 | /* Step through the intersections rendering the ceiling and the floor 1032 | * from near to far. Keep track of the highest floor gradient and lowest 1033 | * ceiling gradient rendered so far and use this to prevent drawing over 1034 | * the nearer surfaces. 1035 | * 1036 | * When stepping through also add sprites to a list of sprites to be 1037 | * drawn. The sprites are rendered last. 1038 | */ 1039 | maxfloorgrad = pixeltograd[SCREEN_HEIGHT-1]; /* Highest floor gradient so far. */ 1040 | minceilgrad = pixeltograd[0]; /* Lowest ceiling gradient so far. */ 1041 | 1042 | prevfloorgrad = -INFINITY; /* Straight down. */ 1043 | prevceilgrad = +INFINITY; /* Straight up. */ 1044 | 1045 | prevplat = r->currentplatform; 1046 | prevdist = 0.0f; 1047 | 1048 | r->numsprites = 0; 1049 | 1050 | if (edgeintersect(r, prevplat, dir, &r->viewpos, 0.0f, &in, NULL) == NULL) 1051 | return; 1052 | while(maxfloorgrad < minceilgrad) 1053 | { 1054 | /* Add sprites to list to be rendered. */ 1055 | for(i=0;inumsprites;i++) 1056 | { 1057 | s=prevplat->sprites[i]; 1058 | if(linelineintersect(&r->viewpos,dir,&s->verts[0],&s->verts[1], 1059 | &s->normal,&s->line,&s->dist,&s->texoffset,&poi) && 1060 | s->dist > prevdist && s->dist < in.distance) 1061 | { 1062 | addspritetolist(r,&prevplat->sprites[i],maxfloorgrad,minceilgrad); 1063 | } 1064 | } 1065 | 1066 | /* Draw floor from prev platform to current platform. */ 1067 | floorgrad = (prevplat->floorheight - r->eyelevel) / in.distance; 1068 | g2 = clamp(prevfloorgrad, maxfloorgrad, minceilgrad); 1069 | g1 = clamp(floorgrad, maxfloorgrad, minceilgrad); 1070 | if (g2 < g1) 1071 | { 1072 | drawfloor(r, prevplat, prevplat->floorheight - r->eyelevel, dir, g1, g2, x); 1073 | maxfloorgrad = g1; 1074 | } 1075 | prevfloorgrad = floorgrad; 1076 | 1077 | /* Draw ceiling from prev platform to current platform. */ 1078 | ceilgrad = (prevplat->ceilheight - r->eyelevel) / in.distance; 1079 | g1 = clamp(prevceilgrad, maxfloorgrad, minceilgrad); 1080 | g2 = clamp(ceilgrad, maxfloorgrad, minceilgrad); 1081 | if (g2 < g1) 1082 | { 1083 | drawfloor(r, prevplat, prevplat->ceilheight - r->eyelevel, dir, g1, g2, x); 1084 | minceilgrad = g2; 1085 | } 1086 | prevceilgrad = ceilgrad; 1087 | 1088 | /* Draw wall from prev floor to current floor. */ 1089 | floorgrad = (in.platform->floorheight - r->eyelevel) / in.distance; 1090 | g2 = clamp(prevfloorgrad, maxfloorgrad, minceilgrad); 1091 | g1 = clamp(floorgrad, maxfloorgrad, minceilgrad); 1092 | if (g2 < g1) 1093 | { 1094 | drawwall(r, &in, g1, g2, x); 1095 | maxfloorgrad = g1; 1096 | } 1097 | prevfloorgrad = floorgrad; 1098 | 1099 | /* Draw wall from prev ceiling to current ceiling. */ 1100 | ceilgrad = (in.platform->ceilheight - r->eyelevel) / in.distance; 1101 | g1 = clamp(prevceilgrad, maxfloorgrad, minceilgrad); 1102 | g2 = clamp(ceilgrad, maxfloorgrad, minceilgrad); 1103 | if (g2 < g1) 1104 | { 1105 | drawwall(r, &in, g1, g2, x); 1106 | minceilgrad = g2; 1107 | } 1108 | prevceilgrad = ceilgrad; 1109 | 1110 | prevplat = in.platform; 1111 | prevdist = in.distance; 1112 | if (maxfloorgrad < minceilgrad) 1113 | if (edgeintersect(r, prevplat, dir, &in.pos, prevdist, &in, NULL) == NULL) 1114 | return; 1115 | } 1116 | 1117 | drawsprites(r,dir,x); 1118 | } 1119 | 1120 | void 1121 | drawscene ( raycaster_t *r ) 1122 | { 1123 | int x; 1124 | vector2d_t v,temp; 1125 | 1126 | for(x=0;xviewdir,&temp); 1129 | vectorscale(&r->viewdir,SCREEN_DISTANCE,&v); 1130 | vectorscale(&temp,TAN_FOV*SCREEN_DISTANCE* 1131 | ((2.0f*(float)x/(float)SCREEN_WIDTH)-1.0f),&temp); 1132 | vectoradd(&v,&temp,&v); 1133 | 1134 | /* vectornormalise(&v,&v);*/ 1135 | 1136 | drawcolumn(r,&v,x); 1137 | } 1138 | } 1139 | 1140 | #define MIN_PHYSICS_FRAME_TIME 10 /* ms */ 1141 | #define MIN_MOUSE_POLL_TIME 0 /* ms */ 1142 | #define MIN_FPS_POLL_TIME 1000 /* ms */ 1143 | 1144 | void 1145 | perframe ( raycaster_t *r, world_t *w ) 1146 | { 1147 | static int last=0,current; 1148 | int dt; 1149 | 1150 | current = SDL_GetTicks(); 1151 | dt = current-last; 1152 | if(dt > MIN_PHYSICS_FRAME_TIME) 1153 | { 1154 | dophysics(r,w,MIN_PHYSICS_FRAME_TIME); 1155 | last = current; 1156 | } 1157 | 1158 | if(current > r->lastmousepolltime + MIN_MOUSE_POLL_TIME) 1159 | { 1160 | if(r->lastcursorx > -1) 1161 | { 1162 | r->mousespeed.x = ((float)(r->cursorx-r->lastcursorx))/(float)(current-r->lastmousepolltime); 1163 | r->mousespeed.y = (float)(r->cursory-r->lastcursory)/(float)(current-r->lastmousepolltime); 1164 | vectorscale(&r->mousespeed,1000.0f,&r->mousespeed); 1165 | } 1166 | 1167 | r->lastmousepolltime = current; 1168 | r->lastcursorx = r->cursorx; 1169 | r->lastcursory = r->cursory; 1170 | } 1171 | 1172 | if(current > r->lastfpsreporttime + MIN_FPS_POLL_TIME) 1173 | { 1174 | printf("FPS: %f (%f ms per frame)\n", 1175 | (float)(1000.0f * r->framessincelastreport)/(float)(current - r->lastfpsreporttime), 1176 | (float)(current - r->lastfpsreporttime)/(float)r->framessincelastreport); 1177 | 1178 | r->framessincelastreport = 0; 1179 | r->lastfpsreporttime = current; 1180 | } else 1181 | { 1182 | r->framessincelastreport++; 1183 | } 1184 | } 1185 | 1186 | void 1187 | drawscreen( raycaster_t *r ) 1188 | { 1189 | SDL_Rect screenrect; 1190 | 1191 | screenrect.x=0; 1192 | screenrect.y=0; 1193 | screenrect.w=SCREEN_WIDTH; 1194 | screenrect.h=SCREEN_HEIGHT; 1195 | 1196 | drawscene(r); 1197 | SDL_UpdateRects(r->screen, 1, &screenrect); 1198 | } 1199 | 1200 | void 1201 | twopiconvention ( float *a ) 1202 | { 1203 | if(*a < 0) 1204 | { 1205 | do 1206 | { 1207 | *a += 2*M_PI; 1208 | } while(*a<0); 1209 | return; 1210 | } 1211 | if(*a > 2.0f*M_PI) 1212 | { 1213 | do 1214 | { 1215 | *a -= 2*M_PI; 1216 | } while(*a>2.0f*M_PI); 1217 | return; 1218 | } 1219 | } 1220 | 1221 | void 1222 | piconvention ( float *a ) 1223 | { 1224 | if(*a < -M_PI) 1225 | { 1226 | do 1227 | { 1228 | *a += 2*M_PI; 1229 | } while(*a<-M_PI); 1230 | return; 1231 | } 1232 | if(*a > M_PI) 1233 | { 1234 | do 1235 | { 1236 | *a -= 2*M_PI; 1237 | } while(*a>M_PI); 1238 | return; 1239 | } 1240 | } 1241 | 1242 | enum 1243 | { 1244 | CONV_PI, 1245 | CONV_TWO_PI 1246 | }; 1247 | 1248 | /* returns the angle ACB using the specified convention */ 1249 | float 1250 | findangle ( vector2d_t *a, vector2d_t *b, vector2d_t *c, int convention ) 1251 | { 1252 | float f; 1253 | vector2d_t t1,t2; 1254 | vectorsubtract(a,c,&t1); 1255 | vectorsubtract(b,c,&t2); 1256 | 1257 | f=atan2f(t2.x,t2.y)-atan2f(t1.x,t1.y); 1258 | if(convention == CONV_PI) 1259 | piconvention(&f); 1260 | else 1261 | twopiconvention(&f); 1262 | 1263 | return f; 1264 | } 1265 | 1266 | #if 1 1267 | int 1268 | isinplatform( raycaster_t *r, platform_t *p, vector2d_t *v ) 1269 | { 1270 | int i; 1271 | float angle,totalangle; 1272 | edge_t *d; 1273 | 1274 | totalangle=0.0f; 1275 | for(i=0;inumedges;i++) 1276 | { 1277 | d=p->edges[i]; 1278 | angle=findangle(&d->verts[0]->pos, 1279 | &d->verts[1]->pos,v,CONV_PI); 1280 | if(d->leftplat == p) 1281 | { 1282 | totalangle+=angle; 1283 | } else if(d->rightplat == p) 1284 | { 1285 | totalangle-=angle; 1286 | } else 1287 | { 1288 | printf("Edge not associated with parent platform\n"); 1289 | } 1290 | } 1291 | if(totalangle > M_PI) 1292 | return 1; 1293 | return 0; 1294 | } 1295 | #else 1296 | int 1297 | isinplatform2( raycaster_t *r, platform_t *p, vector2d_t *v ) 1298 | { 1299 | int i,count=0; 1300 | edge_t *d; 1301 | vector2d_t verts[2]; 1302 | 1303 | for(i=0;inumedges;i++) 1304 | { 1305 | d=p->edges[i]; 1306 | vectorsubtract ( &d->verts[0]->pos, v, &verts[0] ); 1307 | vectorsubtract ( &d->verts[1]->pos, v, &verts[1] ); 1308 | 1309 | if((verts[0].y > 0.0f) == (verts[1].y > 0.0f)) 1310 | continue; 1311 | if(verts[0].x <= 0.0f && verts[1].x <= 0.0f) 1312 | continue; 1313 | if( verts[0].x > 0.0f && verts[1].x > 0.0f ) 1314 | { 1315 | count++; 1316 | continue; 1317 | } 1318 | if ( verts[0].x*(verts[1].y-verts[1].y) > verts[0].y*(verts[1].x-verts[1].x) ) 1319 | { 1320 | count++; 1321 | continue; 1322 | } 1323 | } 1324 | return count&1; 1325 | } 1326 | 1327 | #endif 1328 | #if 0 1329 | int 1330 | cylinderisinplatform ( raycaster_t *r, platform_t *p, vector2d_t *v, float radius ) 1331 | { 1332 | edge_t *e; 1333 | vert_t *vert; 1334 | vector2d_t diff; 1335 | int i; 1336 | float dist,dp1,dp2,dpv,radsqr; 1337 | 1338 | radsqr = radius*radius; 1339 | 1340 | if(isinplatform(r,p,v)) 1341 | return 1; 1342 | /* otherwise check if we're radius distance from any edge 1343 | */ 1344 | for(i=0;inumedges;i++) 1345 | { 1346 | e = p->edges[i]; 1347 | 1348 | dist = dotproduct(&e->normal, v)-e->planedist; 1349 | if(dist > radius) 1350 | continue; 1351 | if(dist < -radius) 1352 | continue; 1353 | 1354 | dp1 = dotproduct(&e->line, &e->verts[0]->pos); 1355 | dp2 = dotproduct(&e->line, &e->verts[1]->pos); 1356 | dpv = dotproduct(&e->line, v); 1357 | if( (dpv > dp1) == (dpv > dp2) ) 1358 | continue; 1359 | 1360 | return 1; 1361 | } 1362 | 1363 | /* check if we're within radius distance of any point 1364 | */ 1365 | for(i=0;inumverts;i++) 1366 | { 1367 | vert = p->verts[i]; 1368 | vectorsubtract(v,&vert->pos,&diff); 1369 | if(diff.x*diff.x + diff.y*diff.y > radsqr) 1370 | continue; 1371 | return 1; 1372 | } 1373 | return 0; 1374 | } 1375 | int 1376 | cylinderpickplatforms ( raycaster_t *r, vector2d_t *v, float radius, 1377 | platform_t **list ) 1378 | { 1379 | int i,num=0; 1380 | 1381 | level_t *l=&r->level; 1382 | for(i=0;inumplatforms;i++) 1383 | { 1384 | if(cylinderisinplatform(r,&l->platforms[i],v,radius)) 1385 | { 1386 | if(num == MAX_CYLINDER_PICKS) 1387 | break; 1388 | list[num++] = &l->platforms[i]; 1389 | } 1390 | } 1391 | return num; 1392 | } 1393 | 1394 | #endif 1395 | platform_t * 1396 | pickplatform ( raycaster_t *r, vector2d_t *v ) 1397 | { 1398 | int i; 1399 | 1400 | level_t *l=&r->level; 1401 | for(i=0;inumplatforms;i++) 1402 | { 1403 | if(isinplatform(r,&l->platforms[i],v)) 1404 | return &l->platforms[i]; 1405 | } 1406 | 1407 | printf("Pick platform returned infplat!\n"); 1408 | return &l->infplatform; 1409 | } 1410 | 1411 | int 1412 | pointcanseepoint ( raycaster_t *r, vector2d_t *v1, float v1height, vector2d_t *v2, float v2height ) 1413 | { 1414 | intersection_t in; 1415 | float tracedist,tracegrad,dist=0.0f,h; 1416 | platform_t *plat; 1417 | vector2d_t dir,pos,offs; 1418 | 1419 | vectorsubtract ( v2, v1, &dir ); 1420 | 1421 | tracedist = vectorlength ( &dir ); 1422 | vectorscale(&dir,1.0f/tracedist,&dir); 1423 | 1424 | tracegrad = (v2height-v1height)/tracedist; 1425 | 1426 | vectorcopy(&pos,v1); 1427 | plat = pickplatform(r,v1); 1428 | if(!plat) 1429 | return 0; 1430 | 1431 | vectorscale(&dir,0.001f,&offs); 1432 | while ( 0 < 1) 1433 | { 1434 | edgeintersect ( r, plat, &dir, &pos, dist, &in, NULL ); 1435 | if(in.distance > tracedist) 1436 | return 1; 1437 | 1438 | /* Check if trace intersects with this edge's ceilings 1439 | */ 1440 | if( in.edge->rightplat->ceilheight < in.edge->leftplat->ceilheight ) 1441 | { 1442 | h = in.edge->rightplat->ceilheight; 1443 | } else 1444 | { 1445 | h = in.edge->leftplat->ceilheight; 1446 | } 1447 | if(((h-v1height)/in.distance) < tracegrad) 1448 | return 0; 1449 | 1450 | /* Check if trace intersects with this edge's floors 1451 | */ 1452 | if( in.edge->rightplat->floorheight > in.edge->leftplat->floorheight ) 1453 | { 1454 | h = in.edge->rightplat->floorheight; 1455 | } else 1456 | { 1457 | h = in.edge->leftplat->floorheight; 1458 | } 1459 | if(((h-v1height)/in.distance) > tracegrad) 1460 | return 0; 1461 | 1462 | dist = in.distance; 1463 | /* vectorcopy(&pos,&in.pos);*/ 1464 | vectoradd(&offs,&in.pos,&pos); 1465 | plat = in.platform; 1466 | } 1467 | 1468 | return 1; 1469 | } 1470 | 1471 | void 1472 | initvariables ( raycaster_t *r ) 1473 | { 1474 | r->viewpos.y = 384.0f; 1475 | r->viewpos.x = 32.0f; 1476 | 1477 | r->currentplatform = pickplatform(r,&r->viewpos); 1478 | 1479 | r->numsprites = 0; 1480 | r->allocatedsprites = HUNK_SPRITES; 1481 | r->spritelist = (sprite_t**)malloc( 1482 | sizeof(sprite_t)*r->allocatedsprites); 1483 | 1484 | r->transpixel = SDL_MapRGB(r->screen->format,255,0,255); 1485 | 1486 | r->mousespeed.x = r->mousespeed.y = 0.0f; 1487 | r->lastcursorx = r->lastcursory = -1; 1488 | r->lastmousepolltime = 0; 1489 | r->lastfpsreporttime = 0; 1490 | r->framessincelastreport = 0; 1491 | 1492 | gradtopixelcoefficent = -(float)(SCREEN_WIDTH*HALF_SCREEN_HEIGHT)/((float)SCREEN_HEIGHT*TAN_FOV); 1493 | pixeltogradcoefficent = -(TAN_FOV*(float)SCREEN_HEIGHT/(float)SCREEN_WIDTH)/(float)HALF_SCREEN_HEIGHT; 1494 | pixeltogradinit(); 1495 | return; 1496 | } 1497 | #define HUNK_PLATFORM_SPRITES 4 1498 | void 1499 | addspritetoplatform( sprite_t *sprite, platform_t *platform ) 1500 | { 1501 | if(platform->numsprites >= platform->allocatedsprites) 1502 | { 1503 | platform->allocatedsprites += HUNK_PLATFORM_SPRITES; 1504 | platform->sprites = (sprite_t**)realloc(platform->sprites, 1505 | platform->allocatedsprites*sizeof(sprite_t*)); 1506 | } 1507 | platform->sprites[platform->numsprites++] = sprite; 1508 | } 1509 | 1510 | /* addsprite 1511 | * 1512 | * designates a sprite to be rendered by assigning platforms to it, 1513 | * and setting its parameters. 1514 | * 1515 | * surface defines whether the sprite is "attached" to the floor or ceiling 1516 | * it is used for determining the vertical position of the sprite only 1517 | * vdist is the distance the sprite will be from the floor or ceiling 1518 | */ 1519 | sprite_t * 1520 | addsprite ( raycaster_t *r, vector2d_t *verts, float height, float vdist, 1521 | int surface, texture_t *texture ) 1522 | { 1523 | platform_t *currentplat; 1524 | vector2d_t direction,pos; 1525 | sprite_t *sprite; 1526 | intersection_t currentint; 1527 | float width,dist,highestfloor=0.0f,lowestceil=0.0f; 1528 | int first=1,i; 1529 | 1530 | currentplat = pickplatform(r,&verts[0]); 1531 | 1532 | sprite = (sprite_t*)malloc(sizeof(sprite_t)); 1533 | 1534 | vectorsubtract(&verts[1],&verts[0],&direction); 1535 | width = vectorlength(&direction); 1536 | vectorscale(&direction,1.0f/width,&direction); 1537 | 1538 | vectorcopy(&sprite->line,&direction); 1539 | vectorrot90(&sprite->line,&sprite->normal); 1540 | vectorcopy(&pos,&verts[0]); 1541 | 1542 | dist = 0.0f; 1543 | while(0<1) 1544 | { 1545 | edgeintersect(r,currentplat,&direction,&pos,dist,¤tint,NULL); 1546 | for(i=0;inumsprites;i++) 1547 | { 1548 | if(currentplat->sprites[i] == sprite) 1549 | break; 1550 | } 1551 | 1552 | if(i==currentplat->numsprites) 1553 | { 1554 | addspritetoplatform(sprite,currentplat); 1555 | } 1556 | if(currentplat == &r->level.infplatform) 1557 | { 1558 | fprintf(stderr,"Sprite is on inf platform\n"); 1559 | break; 1560 | } 1561 | if(currentint.distance > width) 1562 | { 1563 | /* The sprite ends in this platform */ 1564 | break; 1565 | } 1566 | if(surface == SURFACE_FLOOR && 1567 | (first || currentplat->floorheight > highestfloor) ) 1568 | { 1569 | highestfloor = currentplat->floorheight; 1570 | first = 0; 1571 | } else if(surface == SURFACE_CEILING && 1572 | (first || currentplat->ceilheight < lowestceil) ) 1573 | { 1574 | lowestceil = currentplat->ceilheight; 1575 | first = 0; 1576 | } 1577 | dist = currentint.distance; 1578 | currentplat = currentint.platform; 1579 | vectorcopy(&pos,¤tint.pos); 1580 | } 1581 | if(surface == SURFACE_FLOOR) 1582 | { 1583 | sprite->heights[0] = highestfloor + vdist; 1584 | sprite->heights[1] = highestfloor + vdist + height; 1585 | } else if(surface == SURFACE_CEILING) 1586 | { 1587 | sprite->heights[1] = lowestceil - vdist; 1588 | sprite->heights[0] = lowestceil - vdist - height; 1589 | } else /* surface == SURFACE NONE */ 1590 | { 1591 | /* just take the height value directly */ 1592 | sprite->heights[0] = vdist; 1593 | sprite->heights[1] = vdist + height; 1594 | } 1595 | memcpy(sprite->verts,verts,2*sizeof(vector2d_t)); 1596 | sprite->texture = texture; 1597 | sprite->renderflag = 0; 1598 | return sprite; 1599 | } 1600 | 1601 | void 1602 | initraycaster( raycaster_t *r, char *level ) 1603 | { 1604 | printf("loading level...\n"); 1605 | 1606 | memset(r,0,sizeof(r)); 1607 | memset(&r->level,0,sizeof(level_t)); 1608 | 1609 | if(!startsdl(r)) 1610 | return; 1611 | if(!loadlevel(r,level)) 1612 | return; 1613 | 1614 | initvariables(r); 1615 | } 1616 | 1617 | void 1618 | cleanup ( raycaster_t *r ) 1619 | { 1620 | texture_t *t,*next; 1621 | level_t *l=&r->level; 1622 | int i; 1623 | for(i=0;inumplatforms;i++) 1624 | { 1625 | free(l->platforms[i].edges); 1626 | free(l->platforms[i].sprites); 1627 | } 1628 | free(l->platforms); 1629 | free(l->infplatform.edges); 1630 | free(l->infplatform.sprites); 1631 | for(i=0;inumverts;i++) 1632 | free(l->verts[i].edges); 1633 | free(l->verts); 1634 | free(l->edges); 1635 | 1636 | for(next=r->level.texturelist;(t=next);) 1637 | { 1638 | next = t->next; 1639 | freetexture(t); 1640 | free(t); 1641 | } 1642 | 1643 | SDL_Quit(); 1644 | } 1645 | 1646 | /* remove all sprites from all platforms - 1647 | * they are re-added each frame 1648 | */ 1649 | void 1650 | clearsprites ( raycaster_t *r ) 1651 | { 1652 | int i; 1653 | platform_t *p; 1654 | 1655 | for(i=0;ilevel.numplatforms;i++) 1656 | { 1657 | p = &r->level.platforms[i]; 1658 | p->numsprites = 0; 1659 | } 1660 | } 1661 | 1662 | 1663 | void 1664 | renderloop( raycaster_t *r, world_t *w ) 1665 | { 1666 | int done; 1667 | 1668 | done = 0; 1669 | while(!done) 1670 | { 1671 | handleevents(r,w,&done); 1672 | setupworld (w); 1673 | drawscreen(r); 1674 | clearsprites(r); 1675 | perframe(r,w); 1676 | } 1677 | } 1678 | 1679 | int 1680 | main ( int argc, char **argv ) 1681 | { 1682 | char *level; 1683 | raycaster_t r; 1684 | world_t w; 1685 | 1686 | printf("Copyright Notice: This program is licensed under the GNU General Public License\nSee COPYING for details\n\n"); 1687 | 1688 | if(argc >= 2) 1689 | level = argv[1]; 1690 | else 1691 | level = DEFAULT_LEVEL; 1692 | initraycaster(&r,level); 1693 | initworld(&w,&r); 1694 | if(!addentity(&w, "type=spawn\\coords=384 384\\angle=0")) 1695 | return 0; 1696 | if(!addentity(&w, "type=monster\\coords=300 300\\angle=90")) 1697 | return 0; 1698 | 1699 | renderloop(&r,&w); 1700 | 1701 | cleanup(&r); 1702 | freeworld(&w); 1703 | return 0; 1704 | } 1705 | 1706 | -------------------------------------------------------------------------------- /raycaster.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) Matthew Earl 3 | * 4 | * This program is free software; you can redistribute it and/or 5 | * modify it under the terms of the GNU General Public License 6 | * as published by the Free Software Foundation; either version 2 7 | * of the License, or (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program; if not, write to the Free Software 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 17 | */ 18 | 19 | #ifndef _RAYCASTER_H_ 20 | #define _RAYCASTER_H_ 21 | 22 | #include 23 | #include "vector.h" 24 | 25 | #define VIEW_HEIGHT 64.0f 26 | #define MAX_CYLINDER_PICKS 24 27 | 28 | enum 29 | { 30 | SURFACE_CEILING, 31 | SURFACE_FLOOR, 32 | SURFACE_NONE 33 | }; 34 | 35 | typedef struct texture_s 36 | { 37 | char path[64]; 38 | unsigned short *pixels; /* pixels are stored up-down first */ 39 | int width,height; 40 | int widthmask,heightmask; 41 | int widthmaskshift,heightmaskshift; 42 | int widthmasksmallshift,heightmasksmallshift; 43 | int log2width,log2height; 44 | 45 | struct texture_s *prev,*next; 46 | } texture_t; 47 | 48 | typedef struct edge_s 49 | { 50 | struct vert_s *verts[2]; 51 | struct platform_s *leftplat,*rightplat; 52 | struct texture_s *texture; 53 | vector2d_t line,normal; 54 | float planedist; 55 | } edge_t; 56 | 57 | typedef struct vert_s 58 | { 59 | vector2d_t pos; 60 | int numedges; 61 | edge_t **edges; 62 | } vert_t; 63 | 64 | typedef struct sprite_s 65 | { 66 | vector2d_t verts[2],line,normal; 67 | float heights[2]; /* "height" of the sprite */ 68 | texture_t *texture; 69 | 70 | float mingrad,maxgrad; /* used in runtime for visibility clipping */ 71 | int nobounds; 72 | int renderflag; /* flagged for rendering, prevents duplicates */ 73 | float texoffset; 74 | float dist; 75 | } sprite_t; 76 | 77 | typedef struct platform_s 78 | { 79 | float ceilheight,floorheight; 80 | int numedges; 81 | edge_t **edges; 82 | struct texture_s *texture; 83 | 84 | int allocatedsprites; 85 | int numsprites; 86 | sprite_t **sprites; 87 | } platform_t; 88 | 89 | typedef struct level_s 90 | { 91 | int numedges; 92 | edge_t *edges; 93 | 94 | int numplatforms; 95 | platform_t *platforms; 96 | platform_t infplatform; 97 | 98 | texture_t *texturelist; 99 | texture_t *lasttexture; 100 | 101 | int numverts; 102 | vert_t *verts; 103 | vector2d_t size; 104 | } level_t; 105 | 106 | #define HUNK_INTERSECTIONS 8 107 | 108 | typedef struct intersection_s 109 | { 110 | vector2d_t pos; 111 | edge_t *edge; 112 | platform_t *platform; 113 | float distance; 114 | float texoffset; 115 | int final; 116 | } intersection_t; 117 | 118 | typedef enum 119 | { 120 | INTERSECTION_ENTRY, 121 | INTERSECTION_LEAVING 122 | } intersectiontype_t; 123 | 124 | typedef struct solidintersection_s 125 | { 126 | vector2d_t pos; 127 | edge_t *edge; 128 | platform_t *platform; 129 | vert_t *vert; 130 | float distance; 131 | intersectiontype_t type; 132 | 133 | } solidintersection_t; 134 | 135 | typedef struct raycaster_s 136 | { 137 | SDL_Surface *screen; 138 | level_t level; 139 | 140 | vector2d_t viewdir; 141 | vector2d_t viewpos; 142 | float eyelevel; 143 | platform_t *currentplatform; 144 | int cursorx,cursory; 145 | int lastcursorx,lastcursory; 146 | 147 | int numsprites; 148 | int allocatedsprites; 149 | sprite_t **spritelist; 150 | unsigned short transpixel; 151 | 152 | vector2d_t mousespeed; 153 | int lastmousepolltime; 154 | 155 | int lastfpsreporttime; 156 | int framessincelastreport; 157 | } raycaster_t; 158 | 159 | #include "physics.h" 160 | intersection_t * 161 | edgeintersect ( raycaster_t *r, platform_t *p, vector2d_t *dir, 162 | vector2d_t *passedorigin, float prevdist, 163 | intersection_t *intersection, edge_t *ignoreedge); 164 | 165 | sprite_t * addsprite ( raycaster_t *r, vector2d_t *verts, float height, float vdist, 166 | int surface, texture_t *texture ); 167 | texture_t *texturefrompath ( raycaster_t *r, char *path ); 168 | platform_t * pickplatform ( raycaster_t *r, vector2d_t *v ); 169 | int pointcanseepoint ( raycaster_t *r, vector2d_t *v1, float v1height, vector2d_t *v2, float v2height ); 170 | 171 | #endif 172 | 173 | -------------------------------------------------------------------------------- /textures/floor.tga: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/matthewearl/Raycaster/50a3a22e99342f2bcf2c20b439516586666b6df1/textures/floor.tga -------------------------------------------------------------------------------- /textures/monster_aim.tga: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/matthewearl/Raycaster/50a3a22e99342f2bcf2c20b439516586666b6df1/textures/monster_aim.tga -------------------------------------------------------------------------------- /textures/monster_fire.tga: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/matthewearl/Raycaster/50a3a22e99342f2bcf2c20b439516586666b6df1/textures/monster_fire.tga -------------------------------------------------------------------------------- /textures/monster_stand.tga: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/matthewearl/Raycaster/50a3a22e99342f2bcf2c20b439516586666b6df1/textures/monster_stand.tga -------------------------------------------------------------------------------- /textures/monster_walk1.tga: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/matthewearl/Raycaster/50a3a22e99342f2bcf2c20b439516586666b6df1/textures/monster_walk1.tga -------------------------------------------------------------------------------- /textures/monster_walk2.tga: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/matthewearl/Raycaster/50a3a22e99342f2bcf2c20b439516586666b6df1/textures/monster_walk2.tga -------------------------------------------------------------------------------- /textures/sprite.tga: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/matthewearl/Raycaster/50a3a22e99342f2bcf2c20b439516586666b6df1/textures/sprite.tga -------------------------------------------------------------------------------- /textures/wall.tga: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/matthewearl/Raycaster/50a3a22e99342f2bcf2c20b439516586666b6df1/textures/wall.tga -------------------------------------------------------------------------------- /textures/wall.xcf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/matthewearl/Raycaster/50a3a22e99342f2bcf2c20b439516586666b6df1/textures/wall.xcf -------------------------------------------------------------------------------- /tga.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) Matthew Earl 3 | * 4 | * This program is free software; you can redistribute it and/or 5 | * modify it under the terms of the GNU General Public License 6 | * as published by the Free Software Foundation; either version 2 7 | * of the License, or (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program; if not, write to the Free Software 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 17 | */ 18 | 19 | // Handles .tga file loading 20 | 21 | #include "tga.h" 22 | 23 | byte *getPixel ( bitmap_t *bitmap, int x, int y ) 24 | { 25 | /* if(x > bitmap->width || y > bitmap->height || x < 0 || y < 0) 26 | return NULL;*/ 27 | 28 | return (byte*)(bitmap->image+(bitmap->bitsperpixel>>3)*((y*bitmap->width)+x)); 29 | } 30 | 31 | int drawLine ( bitmap_t *bitmap, int x1, int y1, int x2, int y2, byte r, byte g, byte b ) 32 | { 33 | int xdiff,ydiff,xdiffmod,ydiffmod; 34 | int x,y; 35 | byte *p; 36 | 37 | xdiff = x2-x1; 38 | ydiff = y2-y1; 39 | 40 | if(xdiff < 0) 41 | xdiffmod = -xdiff; 42 | else 43 | xdiffmod = xdiff; 44 | if(ydiff < 0) 45 | ydiffmod = -ydiff; 46 | else 47 | ydiffmod = ydiff; 48 | 49 | 50 | if(xdiffmod > ydiffmod) 51 | { 52 | for(x=x1;x!=x2;xdiff<0?x--:x++) 53 | { 54 | p = getPixel(bitmap, x, y1+(int)((float)ydiff*(float)(x-x1)/(float)xdiff)); 55 | if(p) 56 | { 57 | p[0] = b; 58 | p[1] = g; 59 | p[2] = r; 60 | } 61 | } 62 | } else 63 | { 64 | for(y=y1;y!=y2;ydiff<0?y--:y++) 65 | { 66 | p = getPixel(bitmap, x1+(int)((float)xdiff*(float)(y-y1)/(float)ydiff), y); 67 | if(p) 68 | { 69 | p[0] = b; 70 | p[1] = g; 71 | p[2] = r; 72 | } 73 | } 74 | } 75 | return 1; 76 | } 77 | 78 | int getPixelState ( bitmap_t* bitmap, int x, int y ) 79 | { 80 | // if it is beyond the border we assume it is white 81 | if(x > bitmap->width || y > bitmap->height) 82 | return 1; 83 | 84 | return *(byte*)(bitmap->image+(y*bitmap->width)+x)<128?0:1; 85 | } 86 | 87 | float compareAreas ( bitmap_t *area1, bitmap_t *area2, int xorigin1, int yorigin1, int xorigin2, int yorigin2, int areaWidth, int areaHeight ) 88 | { 89 | int x, y; 90 | int totalSimilarPixels=0; 91 | 92 | for(y=0;yimage = (byte*)malloc(bitmap->width*bitmap->height*(bitmap->bitsperpixel>>3)); 129 | return 1; 130 | } 131 | 132 | int freeTGA ( bitmap_t* bitmap ) 133 | { 134 | free( bitmap->image ); 135 | return 1; 136 | } 137 | 138 | int writeTGA ( char* filename, bitmap_t *bitmap ) 139 | { 140 | FILE *f_out = fopen(filename, "wb"); 141 | int y; 142 | byte *tempbuf; 143 | 144 | if(!f_out) 145 | { 146 | printf("Failed to open output file: %s\n", filename); 147 | return 0; 148 | } 149 | 150 | putc(0, f_out); // id length 151 | putc(0, f_out); // colour map type 152 | putc(2, f_out); // we will only do uncompressed types for now 153 | 154 | fseek(f_out, 8, SEEK_SET); 155 | putshort(bitmap->xorigin, f_out); // xorigin 156 | putshort(bitmap->yorigin, f_out); // yorigin 157 | putword(bitmap->width, f_out); // width 158 | putword(bitmap->height, f_out); // height 159 | putc(bitmap->bitsperpixel, f_out); // bpp 160 | putc( 0%00100100, f_out ); // image descriptor byte 161 | 162 | // jump to the start of the actual data 163 | fseek(f_out, 18, SEEK_SET); 164 | 165 | // write the tga upside down... 166 | for(y=bitmap->height-1;y>=0;y--) 167 | { 168 | tempbuf = bitmap->image + y*bitmap->width*(bitmap->bitsperpixel>>3); 169 | fwrite ((char*)tempbuf, bitmap->bitsperpixel>>3, bitmap->width, f_out); 170 | } 171 | fclose(f_out); 172 | 173 | return 1; 174 | } 175 | 176 | int CreateBlankBitmap ( bitmap_t *bitmap, int width, int height, int bpp ) 177 | { 178 | bitmap->xorigin = 0; 179 | bitmap->yorigin = 0; 180 | bitmap->width = width; 181 | bitmap->height = height; 182 | bitmap->bitsperpixel = bpp; 183 | allocTGA(bitmap); 184 | memset(bitmap->image, 0, bitmap->width*bitmap->height*bitmap->bitsperpixel>>3); 185 | return 1; 186 | } 187 | 188 | int loadTGA ( char *filename, bitmap_t *bitmap ) 189 | { 190 | FILE *f_in = fopen(filename, "rb"); 191 | int idLength; 192 | int colourMapType; 193 | int x, y; 194 | int i; 195 | byte *tempbuf; 196 | int type; 197 | byte header; 198 | byte count; 199 | byte temppixel[4]; 200 | byte imagedescriptor; 201 | int done; 202 | 203 | if(!f_in) 204 | { 205 | printf("Could not open input file: %s\n", filename); 206 | return 0; 207 | } 208 | 209 | idLength = getc(f_in); 210 | colourMapType = getc(f_in); 211 | 212 | if(colourMapType) 213 | { 214 | printf("Program does not handle colour maps\n"); 215 | return 0; 216 | } 217 | 218 | // We only handle type 2 and type 10 TGA's atm 219 | type = getc(f_in); 220 | if(type != 2 && type != 10) 221 | { 222 | printf("input file is not of type 2\n"); 223 | return 0; 224 | } 225 | 226 | fseek(f_in, 8, SEEK_SET); 227 | bitmap->xorigin = getshort(f_in); 228 | bitmap->yorigin = getshort(f_in); 229 | bitmap->width = getword(f_in); 230 | bitmap->height = getword(f_in); 231 | bitmap->bitsperpixel = getc(f_in); 232 | imagedescriptor = getc(f_in); 233 | 234 | if(bitmap->bitsperpixel == 24) 235 | bitmap->translucent = 0; 236 | else 237 | bitmap->translucent = 1; 238 | 239 | // Seek to the start of the pixel data 240 | fseek(f_in, 18+idLength, SEEK_SET); 241 | 242 | bitmap->image = (char*)malloc(bitmap->height*bitmap->width*(bitmap->bitsperpixel>>3)); 243 | tempbuf = bitmap->image; 244 | if(type == 2) 245 | { 246 | for(y=0;yheight;y++) 247 | { 248 | if(!(32&imagedescriptor)) 249 | tempbuf = bitmap->image+bitmap->width*(bitmap->bitsperpixel>>3)*(bitmap->height-1-y); 250 | for(x=0;xwidth;x++) 251 | { 252 | for(i=0;ibitsperpixel>>3;i++) 253 | *(tempbuf++) = getc(f_in); 254 | if(bitmap->bitsperpixel==32&&*(tempbuf-1)<255) 255 | bitmap->translucent=1; 256 | } 257 | } 258 | } else 259 | { 260 | done = x = y = 0; 261 | while ( !done ) 262 | { 263 | // Read in the header packet 264 | header = getc(f_in); 265 | count = header & 127; 266 | count++; 267 | 268 | if(header & 128) 269 | { 270 | // We have a run-length 271 | for(i=0;ibitsperpixel>>3;i++) 272 | temppixel[i] = getc(f_in); 273 | 274 | if(bitmap->bitsperpixel==32&&temppixel[3]<255) 275 | bitmap->translucent=1; 276 | 277 | // temppixel is the pixel we want to load 278 | for(;yheight;y++) 279 | { 280 | if(!(32&imagedescriptor)) 281 | tempbuf = bitmap->image+(bitmap->width*(bitmap->height-1-y)+x)*(bitmap->bitsperpixel>>3); 282 | for(;xwidth;) 283 | { 284 | for(i=0;ibitsperpixel>>3;i++) 285 | *(tempbuf++) = temppixel[i]; 286 | x++; 287 | count--; 288 | if(!count) 289 | goto out; 290 | } 291 | x=0; 292 | } 293 | done=1; 294 | } else 295 | { 296 | // We have a normal set of pixels 297 | for(;yheight;y++) 298 | { 299 | if(!(32&imagedescriptor)) 300 | tempbuf = bitmap->image+(bitmap->width*(bitmap->height-1-y)+x)*(bitmap->bitsperpixel>>3); 301 | for(;xwidth;) 302 | { 303 | for(i=0;ibitsperpixel>>3;i++) 304 | *(tempbuf++) = getc(f_in); 305 | if(bitmap->bitsperpixel==32&&*(tempbuf-1)<255) 306 | bitmap->translucent=1; 307 | x++; 308 | count--; 309 | if(!count) 310 | goto out; 311 | } 312 | x=0; 313 | } 314 | done=1; 315 | } 316 | out:; 317 | } 318 | } 319 | fclose(f_in); 320 | return 1; 321 | } 322 | 323 | -------------------------------------------------------------------------------- /tga.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) Matthew Earl 3 | * 4 | * This program is free software; you can redistribute it and/or 5 | * modify it under the terms of the GNU General Public License 6 | * as published by the Free Software Foundation; either version 2 7 | * of the License, or (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program; if not, write to the Free Software 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 17 | */ 18 | 19 | // Header file for tga.c 20 | #ifndef _TGA_H_ 21 | #define _TGA_H_ 22 | 23 | #include 24 | #include 25 | #include 26 | 27 | typedef unsigned short WORD; 28 | typedef unsigned char byte; 29 | 30 | typedef struct _bitmap_t 31 | { 32 | byte *image; 33 | WORD width, height; 34 | short xorigin, yorigin; 35 | int bitsperpixel; 36 | int translucent; // 0 if bpp is 24 or alpha channel is solid 37 | } bitmap_t; 38 | 39 | int writeTGA ( char* filename, bitmap_t *bitmap ); 40 | int loadTGA ( char *filename, bitmap_t *bitmap ); 41 | float compareAreas ( bitmap_t *area1, bitmap_t *area2, int xorigin1, int yorigin1, int xorigin2, int yorigin2, int areaWidth, int areaHeight ); 42 | int allocTGA ( bitmap_t *bitmap ); 43 | int freeTGA ( bitmap_t* bitmap ); 44 | int drawLine ( bitmap_t *bitmap, int x1, int y1, int x2, int y2, byte r, byte g, byte b ); 45 | int CreateBlankBitmap ( bitmap_t *bitmap, int width, int height, int bpp ); 46 | byte *getPixel ( bitmap_t *bitmap, int x, int y ); 47 | 48 | #endif 49 | 50 | -------------------------------------------------------------------------------- /vector.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) Matthew Earl 3 | * 4 | * This program is free software; you can redistribute it and/or 5 | * modify it under the terms of the GNU General Public License 6 | * as published by the Free Software Foundation; either version 2 7 | * of the License, or (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program; if not, write to the Free Software 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 17 | */ 18 | 19 | #include 20 | #include 21 | #include 22 | #include "vector.h" 23 | 24 | void 25 | vectorcopy( vector2d_t *a, vector2d_t *b ) 26 | { 27 | memcpy(a,b,sizeof(vector2d_t)); 28 | } 29 | 30 | void 31 | vectorzero( vector2d_t *a ) 32 | { 33 | a->x=a->y=0.0f; 34 | } 35 | 36 | void 37 | vectorscale( vector2d_t *a, float f, vector2d_t *b ) 38 | { 39 | b->x=a->x*f; 40 | b->y=a->y*f; 41 | } 42 | 43 | void 44 | vectoradd( vector2d_t *a, vector2d_t *b, vector2d_t *o ) 45 | { 46 | o->x=a->x+b->x; 47 | o->y=a->y+b->y; 48 | } 49 | 50 | void 51 | vectorsubtract( vector2d_t *a, vector2d_t *b, vector2d_t *o ) 52 | { 53 | o->x=a->x-b->x; 54 | o->y=a->y-b->y; 55 | } 56 | 57 | float sqrtf(float f); 58 | 59 | float 60 | vectorlength( vector2d_t *a ) 61 | { 62 | return sqrtf(a->x*a->x+a->y*a->y); 63 | } 64 | 65 | void 66 | vectornormalise( vector2d_t *a, vector2d_t *b ) 67 | { 68 | vectorscale(a,1.0f/vectorlength(a),b); 69 | } 70 | 71 | float 72 | dotproduct( vector2d_t *a, vector2d_t *b ) 73 | { 74 | return a->x*b->x+a->y*b->y; 75 | } 76 | 77 | /* rotate the vector 90 degrees */ 78 | void 79 | vectorrot90( vector2d_t *a, vector2d_t *b ) 80 | { 81 | vector2d_t r; 82 | r.x = -a->y; 83 | r.y = a->x; 84 | 85 | vectorcopy(b,&r); 86 | } 87 | 88 | void 89 | vectorrotangle ( vector2d_t *a, float angle, vector2d_t *out ) 90 | { 91 | float c,s; 92 | vector2d_t b; 93 | c = cos(angle); 94 | s = sin(angle); 95 | b.x = a->x*c + a->y*s; 96 | b.y = a->x*(-s) + a->y*c; 97 | vectorcopy(out,&b); 98 | } 99 | 100 | /* dir should be normalised */ 101 | void 102 | rotatebyvector ( vector2d_t *a, vector2d_t *dir, vector2d_t *out ) 103 | { 104 | vector2d_t xvec,temp,temp2; 105 | 106 | vectorscale(dir,a->y,&temp); 107 | vectorrot90(dir,&xvec); 108 | vectorscale(&xvec,a->x,&temp2); 109 | vectoradd(&temp2,&temp,out); 110 | } 111 | 112 | void 113 | angletovector ( float angle, vector2d_t *out ) 114 | { 115 | out->x = cosf(angle); 116 | out->y = sinf(angle); 117 | } 118 | 119 | void 120 | vectormidpoint ( vector2d_t *a, vector2d_t *b, float f, vector2d_t *out ) 121 | { 122 | out->x = a->x + f*(b->x-a->x); 123 | out->y = a->y + f*(b->y-a->y); 124 | } 125 | 126 | 127 | 128 | -------------------------------------------------------------------------------- /vector.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) Matthew Earl 3 | * 4 | * This program is free software; you can redistribute it and/or 5 | * modify it under the terms of the GNU General Public License 6 | * as published by the Free Software Foundation; either version 2 7 | * of the License, or (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program; if not, write to the Free Software 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 17 | */ 18 | 19 | #ifndef _VECTOR_H_ 20 | #define _VECTOR_H_ 21 | 22 | typedef struct vector2d_s 23 | { 24 | float x,y; 25 | } vector2d_t; 26 | 27 | void vectorcopy( vector2d_t *a, vector2d_t *b ); 28 | void vectorzero( vector2d_t *a ); 29 | void vectorscale( vector2d_t *a, float f, vector2d_t *b ); 30 | void vectoradd( vector2d_t *a, vector2d_t *b, vector2d_t *o ); 31 | void vectorsubtract( vector2d_t *a, vector2d_t *b, vector2d_t *o ); 32 | float vectorlength( vector2d_t *a ); 33 | void vectornormalise( vector2d_t *a, vector2d_t *b ); 34 | float dotproduct( vector2d_t *a, vector2d_t *b ); 35 | void vectorrot90( vector2d_t *a, vector2d_t *b ); 36 | void vectormidpoint ( vector2d_t *a, vector2d_t *b, float f, vector2d_t *out ); 37 | void rotatebyvector ( vector2d_t *a, vector2d_t *dir, vector2d_t *out ); 38 | void angletovector ( float angle, vector2d_t *out ); 39 | void vectorrotangle ( vector2d_t *a, float angle, vector2d_t *out ); 40 | #endif 41 | 42 | -------------------------------------------------------------------------------- /world.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) Matthew Earl 3 | * 4 | * This program is free software; you can redistribute it and/or 5 | * modify it under the terms of the GNU General Public License 6 | * as published by the Free Software Foundation; either version 2 7 | * of the License, or (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program; if not, write to the Free Software 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 17 | */ 18 | 19 | /* The "world" is the section of the program which determimes 20 | * where all the entities are. At the end it sets up the 21 | * raycaster so that it renders the scene properly 22 | */ 23 | #include 24 | #include 25 | #include "raycaster.h" 26 | #include "world.h" 27 | 28 | entity_t *allocentity ( world_t *world ) 29 | { 30 | entity_t *newent; 31 | 32 | if(world->numentities == world->allocatedentities) 33 | { 34 | world->allocatedentities += HUNK_ENTITIES; 35 | world->entities = (entity_t*)realloc(world->entities, 36 | sizeof(entity_t)*world->allocatedentities); 37 | } 38 | newent = &world->entities[world->numentities++]; 39 | memset(newent,0,sizeof(entity_t)); 40 | 41 | return newent; 42 | } 43 | 44 | void 45 | setfollowangle ( world_t *w, entity_t *e, vector2d_t *normal ) 46 | { 47 | /* vectorsubtract( &w->playerentity->pos, &e->pos, normal ); 48 | vectornormalise(normal,normal);*/ 49 | vectorcopy( normal, &w->playerentity->angle ); 50 | } 51 | 52 | int 53 | spawnplayer ( world_t *world ) 54 | { 55 | int i; 56 | entity_t *playerent,*e=NULL; 57 | 58 | /* find a spawn point */ 59 | for(i=0;inumentities;i++) 60 | { 61 | e = &world->entities[i]; 62 | if(e->type==ENTITYTYPE_SPAWN) 63 | break; 64 | } 65 | if(i==world->numentities) 66 | { 67 | fprintf(stderr,"Could not find spawn point\n"); 68 | return 0; 69 | } 70 | 71 | world->playerentity = playerent = allocentity ( world ); 72 | vectorcopy(&playerent->pos, &e->pos); 73 | vectorcopy(&playerent->angle, &e->angle); 74 | 75 | playerent->type = ENTITYTYPE_PLAYER; 76 | playerent->vvel = 0.0f; 77 | playerent->onground = 1; 78 | playerent->keys = 0; 79 | playerent->physics = physics_player; 80 | playerent->currentplatform = pickplatform ( world->raycaster, 81 | &playerent->pos); 82 | if(playerent->currentplatform == &world->raycaster->level.infplatform) 83 | printf("Warning: Spawn point on infplat\n"); 84 | playerent->vpos = playerent->currentplatform->floorheight; 85 | return 1; 86 | } 87 | 88 | void 89 | setupworld ( world_t *world ) 90 | { 91 | int i; 92 | entity_t *e; 93 | vector2d_t angle,dir,verts[2]; 94 | 95 | world->time = SDL_GetTicks(); 96 | 97 | if(!world->playerentity) 98 | { 99 | if(!spawnplayer( world )) 100 | return; 101 | } 102 | for(i=0;inumentities;i++) 103 | { 104 | e = &world->entities[i]; 105 | 106 | if(e->think && world->time > e->nextthink) 107 | { 108 | void (*think)(world_t *w, struct entity_s *e); 109 | think = e->think; 110 | e->think = NULL; 111 | think(world,e); 112 | } 113 | 114 | /* don't need to add transparent stuff to the world */ 115 | if(!e->texture) 116 | continue; 117 | if(e->follow) 118 | setfollowangle(world,e,&angle); 119 | else 120 | vectorcopy(&angle,&e->angle); 121 | 122 | vectorrot90( &angle, &dir ); 123 | vectorscale( &dir, e->texture->width/2, &dir ); 124 | vectorsubtract( &e->pos, &dir, &verts[0]); 125 | vectoradd( &e->pos, &dir, &verts[1]); 126 | 127 | addsprite(world->raycaster,verts,e->texture->height,e->vpos, 128 | SURFACE_NONE,e->texture); 129 | } 130 | 131 | /* copy over player view pos to raycaster */ 132 | world->raycaster->currentplatform = world->playerentity->currentplatform; 133 | vectorcopy(&world->raycaster->viewpos,&world->playerentity->pos); 134 | vectorcopy(&world->raycaster->viewdir,&world->playerentity->angle); 135 | world->raycaster->eyelevel = world->playerentity->vpos+VIEW_HEIGHT; 136 | } 137 | 138 | int 139 | findvalueforkey ( char *strings, char *key, char *value, int maxlen ) 140 | { 141 | char *pos,*next,*end,*eq; 142 | int keylen; 143 | 144 | keylen = strlen(key); 145 | for(pos=strings;;) 146 | { 147 | end = next = strchr(pos,'\\'); 148 | if(!end) 149 | end = strings+strlen(strings); 150 | eq = strchr(pos,'='); 151 | if(eq && eq < end && keylen == eq-pos && 152 | maxlen > end-eq && 153 | !strncasecmp(pos,key,keylen)) 154 | { 155 | memcpy(value,eq+1,end-eq-1); 156 | value[end-eq-1] = '\0'; 157 | return 1; 158 | } 159 | if(!next) 160 | break; 161 | pos = next+1; 162 | } 163 | return 0; 164 | } 165 | 166 | enum 167 | { 168 | MONSTERFRAME_STAND, 169 | MONSTERFRAME_WALK1, 170 | MONSTERFRAME_WALK2, 171 | MONSTERFRAME_AIM, 172 | MONSTERFRAME_FIRE, 173 | MONSTERFRAME_MAX 174 | }; 175 | 176 | char *monsterframelookup[] = 177 | { 178 | "monster_stand.tga", 179 | "monster_walk1.tga", 180 | "monster_walk2.tga", 181 | "monster_aim.tga", 182 | "monster_fire.tga" 183 | }; 184 | 185 | /* how quickly the monster is able to think 186 | */ 187 | #define MONSTER_WIT 100 /* ms */ 188 | #define MONSTER_AIMTIME 200 /* ms */ 189 | #define MONSTER_RELOADTIME 1000 /* ms */ 190 | 191 | #define MONSTER_SHOOTRANGE 300.0f /* units */ 192 | #define MONSTER_MUZZLEHEIGHT 64.0f /* units */ 193 | #define MONSTER_FIRETIME 50 /* ms */ 194 | 195 | void monster_think ( world_t *world, entity_t *ent ); 196 | 197 | void 198 | monster_aimpose ( world_t *world, entity_t *ent ) 199 | { 200 | ent->texture = ent->frames[MONSTERFRAME_AIM]; 201 | ent->think = monster_think; 202 | ent->nextthink = world->time + MONSTER_WIT; 203 | } 204 | 205 | /* root of the monster's thinking - make a decision about 206 | * what to do 207 | */ 208 | void 209 | monster_think ( world_t *world, entity_t *ent ) 210 | { 211 | vector2d_t dir; 212 | float dist; 213 | 214 | ent->wishdir.x = ent->wishdir.y = 0.0f; 215 | 216 | ent->think = monster_think; 217 | ent->nextthink = world->time + MONSTER_WIT; 218 | 219 | if(!world->playerentity) 220 | { 221 | ent->texture = ent->frames[MONSTERFRAME_STAND]; 222 | ent->shoottime = -1; 223 | return; 224 | } 225 | if(!pointcanseepoint(world->raycaster, 226 | &world->playerentity->pos,world->playerentity->vpos+VIEW_HEIGHT, 227 | &ent->pos,ent->vpos+MONSTER_MUZZLEHEIGHT)) 228 | { 229 | ent->texture = ent->frames[MONSTERFRAME_STAND]; 230 | ent->nextthink = world->time + MONSTER_WIT*5; /* monster is not very alert in the absence of 231 | the player */ 232 | ent->shoottime = -1; 233 | return; 234 | } 235 | vectorsubtract(&world->playerentity->pos,&ent->pos,&dir); 236 | dist = vectorlength(&dir); 237 | vectorscale(&dir,1.0f/dist,&dir); 238 | 239 | if(dist > MONSTER_SHOOTRANGE) 240 | { 241 | if((world->time/276)%2) 242 | { 243 | ent->texture = ent->frames[MONSTERFRAME_WALK1]; 244 | } else 245 | { 246 | ent->texture = ent->frames[MONSTERFRAME_WALK2]; 247 | } 248 | vectorcopy(&ent->wishdir,&dir); 249 | ent->shoottime = -1; /* if we back off and re-approach 250 | * the monster still has to take aim 251 | */ 252 | return; 253 | } else if(ent->shoottime > 0 && world->time > ent->shoottime ) 254 | { 255 | ent->texture = ent->frames[MONSTERFRAME_FIRE]; 256 | ent->shoottime = world->time + MONSTER_RELOADTIME; 257 | ent->think = monster_aimpose; /* return to aim pose */ 258 | ent->nextthink = world->time + MONSTER_FIRETIME; 259 | return; 260 | } else 261 | { 262 | if(ent->shoottime < 0) 263 | { 264 | ent->texture = ent->frames[MONSTERFRAME_AIM]; 265 | ent->shoottime = world->time + MONSTER_AIMTIME; 266 | } 267 | } 268 | } 269 | 270 | int 271 | spawn_monster ( world_t *world, entity_t *ent, char *strings ) 272 | { 273 | int i; 274 | 275 | ent->frames = (texture_t**)malloc(sizeof(texture_t*)*MONSTERFRAME_MAX); 276 | ent->follow = 1; 277 | 278 | for(i=0;iframes[i] = texturefrompath(world->raycaster, 281 | monsterframelookup[i]); 282 | } 283 | ent->texture = ent->frames[MONSTERFRAME_STAND]; 284 | 285 | ent->think = monster_think; 286 | ent->nextthink = world->time; 287 | ent->physics = physics_monster; 288 | 289 | ent->currentplatform = pickplatform ( world->raycaster, 290 | &ent->pos); 291 | ent->vpos = ent->currentplatform->floorheight; 292 | 293 | return 1; 294 | } 295 | 296 | int 297 | spawn_static ( world_t *world, entity_t *ent, char *strings ) 298 | { 299 | char buffer[64]; 300 | 301 | if(!findvalueforkey(strings,"follow",buffer,sizeof(buffer))) 302 | return 0; 303 | ent->follow = atoi(buffer); 304 | if(!findvalueforkey(strings,"texture",buffer,sizeof(buffer))) 305 | return 0; 306 | ent->texture = texturefrompath(world->raycaster,buffer); 307 | if(!ent->texture) 308 | return 0; 309 | 310 | ent->currentplatform = pickplatform ( world->raycaster, 311 | &ent->pos); 312 | ent->vpos = ent->currentplatform->floorheight; 313 | 314 | return 1; 315 | } 316 | 317 | /* Only entities which can be added from the map editor are in this lookup 318 | */ 319 | entitystring_t entitylookup[] = 320 | { 321 | { "spawn", ENTITYTYPE_SPAWN, NULL, NULL }, 322 | { "static", ENTITYTYPE_STATIC, spawn_static, NULL }, 323 | { "monster", ENTITYTYPE_MONSTER, spawn_monster, NULL } 324 | }; 325 | 326 | /* Adds an entity to the world based on the strings 327 | * defined in the level editor. 328 | */ 329 | int 330 | addentity ( world_t *world, char *strings ) 331 | { 332 | int i,lim; 333 | entitystring_t *es; 334 | char type[64],coords[64],anglestr[64]; 335 | entity_t *newent; 336 | float angle; 337 | 338 | newent = allocentity(world); 339 | 340 | if(!findvalueforkey(strings, "type",type,sizeof(type))) 341 | { 342 | fprintf(stderr,"No classname in entity strings\n"); 343 | return 0; 344 | } 345 | if(!findvalueforkey(strings, "coords",coords,sizeof(coords))) 346 | { 347 | fprintf(stderr,"No coords in entity strings\n"); 348 | return 0; 349 | } 350 | if(!findvalueforkey(strings, "angle",anglestr,sizeof(anglestr))) 351 | { 352 | fprintf(stderr,"No angle in entity strings\n"); 353 | return 0; 354 | } 355 | 356 | if(sscanf(coords,"%f %f",&newent->pos.x,&newent->pos.y) != 2) 357 | { 358 | fprintf(stderr,"Malformed coords in entity strings\n"); 359 | return 0; 360 | } 361 | if(sscanf(anglestr,"%f",&angle) != 1) 362 | { 363 | fprintf(stderr,"Malformed angle in entity strings\n"); 364 | return 0; 365 | } 366 | 367 | angletovector(DEGREESTORADS(angle),&newent->angle); 368 | 369 | lim = sizeof(entitylookup)/sizeof(entitystring_t); 370 | for(i=0;iname,type)) 374 | { 375 | continue; 376 | } 377 | newent->type = es->type; 378 | if(es->spawn) 379 | return es->spawn(world,newent,strings); 380 | return 1; 381 | } 382 | fprintf(stderr,"Entity type \"%s\" not found\n",type); 383 | return 0; 384 | } 385 | 386 | void 387 | initworld ( world_t *world, raycaster_t *r ) 388 | { 389 | world->time = SDL_GetTicks(); 390 | world->raycaster = r; 391 | world->allocatedentities = 0; 392 | world->numentities = 0; 393 | world->entities = NULL; 394 | world->playerentity = NULL; 395 | return; 396 | 397 | } 398 | 399 | void 400 | freeworld ( world_t *world ) 401 | { 402 | int i,j,lim; 403 | entity_t *e; 404 | entitystring_t *es; 405 | 406 | for(i=0;inumentities;i++) 407 | { 408 | e = &world->entities[i]; 409 | lim = sizeof(entitylookup)/sizeof(entitystring_t); 410 | for(j=0;jtype != e->type) 415 | continue; 416 | if(es->free) 417 | es->free(world,e); 418 | break; 419 | } 420 | } 421 | free(world->entities); 422 | } 423 | 424 | -------------------------------------------------------------------------------- /world.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) Matthew Earl 3 | * 4 | * This program is free software; you can redistribute it and/or 5 | * modify it under the terms of the GNU General Public License 6 | * as published by the Free Software Foundation; either version 2 7 | * of the License, or (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program; if not, write to the Free Software 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 17 | */ 18 | 19 | #ifndef _WORLD_H_ 20 | #define _WORLD_H_ 21 | 22 | #include "raycaster.h" 23 | 24 | #define HUNK_ENTITIES 16 25 | 26 | #define DEGREESTORADS(a) ((a)*0.01745f) 27 | #define RADSTODEGREES(a) ((a)*57.295f) 28 | 29 | typedef enum 30 | { 31 | ENTITYTYPE_PLAYER, 32 | ENTITYTYPE_SPAWN, 33 | ENTITYTYPE_PLASMA, 34 | ENTITYTYPE_STATIC, 35 | ENTITYTYPE_MONSTER 36 | } entitytype_t; 37 | 38 | struct entity_s; 39 | struct world_s; 40 | 41 | typedef struct world_s 42 | { 43 | int allocatedentities; 44 | int numentities; 45 | struct entity_s *entities; 46 | struct entity_s *playerentity; 47 | int time; /* ms */ 48 | 49 | raycaster_t *raycaster; 50 | } world_t; 51 | 52 | typedef struct entity_s 53 | { 54 | vector2d_t pos,vel,acc; 55 | float vpos,vvel; 56 | vector2d_t angle; 57 | entitytype_t type; 58 | 59 | texture_t *texture; 60 | int follow; 61 | 62 | texture_t **frames; 63 | 64 | int onground,keys; 65 | void (*physics)(raycaster_t *r,world_t *w,struct entity_s *e,int dt); 66 | void (*think)(world_t *w,struct entity_s *e); 67 | int nextthink; 68 | 69 | platform_t *currentplatform; 70 | 71 | /* monster */ 72 | int shoottime; 73 | vector2d_t wishdir; 74 | } entity_t; 75 | 76 | typedef struct entitystring_s 77 | { 78 | char name[64]; 79 | entitytype_t type; 80 | int (*spawn)(world_t *world, entity_t *ent,char *strings); 81 | void (*free)(world_t *world, entity_t *ent); 82 | } entitystring_t; 83 | 84 | extern entitystring_t entitylookup[]; 85 | 86 | void setupworld ( world_t *world ); 87 | void initworld ( world_t *world, raycaster_t *r ); 88 | void freeworld ( world_t *world ); 89 | int addentity ( world_t *world, char *string ); 90 | 91 | #endif 92 | 93 | --------------------------------------------------------------------------------