tuxanci2-engine
Game engine for tuxanci2
Log | Files | Refs | README | LICENSE
commit fb57c22dd61e27dba9e0d0562df74e96488f0cb4
Author: Connor Thomson <connor@gnufault.org>
Date:   Sun, 18 Oct 2026 13:04:37 -0700

Initial Commit

Diffstat:
A.gitignore | 3+++
ALICENSE | 674+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
AMakefile | 72++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
AREADME.md | 3+++
Afiles/fonts/LICENSE | 187+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Afiles/fonts/dejavu_sans.ttf | 0
Afiles/images/bg.tai | 0
Ainclude/ta_api.h | 30++++++++++++++++++++++++++++++
Ainclude/ta_button.h | 32++++++++++++++++++++++++++++++++
Ainclude/ta_config.h.in | 6++++++
Ainclude/ta_fb.h | 19+++++++++++++++++++
Ainclude/ta_files.h | 9+++++++++
Ainclude/ta_image.h | 9+++++++++
Ainclude/ta_keyboard.h | 150+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Ainclude/ta_text.h | 12++++++++++++
Ainclude/ta_types.h | 29+++++++++++++++++++++++++++++
Aoriginals/bg.jpg | 0
Aplatforms/dos/.gitignore | 4++++
Aplatforms/dos/Makefile | 35+++++++++++++++++++++++++++++++++++
Aplatforms/dos/dos.c | 201+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aplatforms/sdl2/.gitignore | 2++
Aplatforms/sdl2/Makefile | 33+++++++++++++++++++++++++++++++++
Aplatforms/sdl2/sdl2.c | 131+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aplatforms/sdl3/.gitignore | 2++
Aplatforms/sdl3/Makefile | 33+++++++++++++++++++++++++++++++++
Aplatforms/sdl3/sdl3.c | 139+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aplatforms/wayland/.gitignore | 2++
Aplatforms/wayland/Makefile | 42++++++++++++++++++++++++++++++++++++++++++
Aplatforms/wayland/wayland.c | 560+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aplatforms/x11/.gitignore | 2++
Aplatforms/x11/Makefile | 33+++++++++++++++++++++++++++++++++
Aplatforms/x11/x11.c | 194+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/ta_button.c | 163+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/ta_fb.c | 53+++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/ta_image.c | 309+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/ta_keyboard.c | 47+++++++++++++++++++++++++++++++++++++++++++++++
Asrc/ta_main.c | 24++++++++++++++++++++++++
Asrc/ta_text.c | 1023+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
38 files changed, 4267 insertions(+), 0 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -0,0 +1,2 @@ +build +tuxanci2* +\ No newline at end of file diff --git a/LICENSE b/LICENSE @@ -0,0 +1,674 @@ + GNU GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/> + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The GNU General Public License is a free, copyleft license for +software and other kinds of works. + + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +the GNU General Public License is intended to guarantee your freedom to +share and change all versions of a program--to make sure it remains free +software for all its users. We, the Free Software Foundation, use the +GNU General Public License for most of our software; it applies also to +any other work released this way by its authors. You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +them if you wish), that you receive source code or can get it if you +want it, that you can change the software or use pieces of it in new +free programs, and that you know you can do these things. + + To protect your rights, we need to prevent others from denying you +these rights or asking you to surrender the rights. Therefore, you have +certain responsibilities if you distribute copies of the software, or if +you modify it: responsibilities to respect the freedom of others. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must pass on to the recipients the same +freedoms that you received. You must make sure that they, too, receive +or can get the source code. And you must show them these terms so they +know their rights. + + Developers that use the GNU GPL protect your rights with two steps: +(1) assert copyright on the software, and (2) offer you this License +giving you legal permission to copy, distribute and/or modify it. + + For the developers' and authors' protection, the GPL clearly explains +that there is no warranty for this free software. For both users' and +authors' sake, the GPL requires that modified versions be marked as +changed, so that their problems will not be attributed erroneously to +authors of previous versions. + + Some devices are designed to deny users access to install or run +modified versions of the software inside them, although the manufacturer +can do so. This is fundamentally incompatible with the aim of +protecting users' freedom to change the software. The systematic +pattern of such abuse occurs in the area of products for individuals to +use, which is precisely where it is most unacceptable. Therefore, we +have designed this version of the GPL to prohibit the practice for those +products. If such problems arise substantially in other domains, we +stand ready to extend this provision to those domains in future versions +of the GPL, as needed to protect the freedom of users. + + Finally, every program is threatened constantly by software patents. +States should not allow patents to restrict development and use of +software on general-purpose computers, but in those that do, we wish to +avoid the special danger that patents applied to a free program could +make it effectively proprietary. To prevent this, the GPL assures that +patents cannot be used to render the program non-free. + + The precise terms and conditions for copying, distribution and +modification follow. + + TERMS AND CONDITIONS + + 0. Definitions. + + "This License" refers to version 3 of the GNU General Public License. + + "Copyright" also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. + + "The Program" refers to any copyrightable work licensed under this +License. Each licensee is addressed as "you". "Licensees" and +"recipients" may be individuals or organizations. + + To "modify" a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of an +exact copy. The resulting work is called a "modified version" of the +earlier work or a work "based on" the earlier work. + + A "covered work" means either the unmodified Program or a work based +on the Program. + + To "propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. + + To "convey" a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user through +a computer network, with no transfer of a copy, is not conveying. + + An interactive user interface displays "Appropriate Legal Notices" +to the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. + + 1. Source Code. + + The "source code" for a work means the preferred form of the work +for making modifications to it. "Object code" means any non-source +form of a work. + + A "Standard Interface" means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + + The "System Libraries" of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +"Major Component", in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + + The "Corresponding Source" for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work's +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + + The Corresponding Source need not include anything that users +can regenerate automatically from other parts of the Corresponding +Source. + + The Corresponding Source for a work in source code form is that +same work. + + 2. Basic Permissions. + + All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + + You may make, run and propagate covered works that you do not +convey, without conditions so long as your license otherwise remains +in force. You may convey covered works to others for the sole purpose +of having them make modifications exclusively for you, or provide you +with facilities for running those works, provided that you comply with +the terms of this License in conveying all material for which you do +not control copyright. Those thus making or running the covered works +for you must do so exclusively on your behalf, under your direction +and control, on terms that prohibit them from making any copies of +your copyrighted material outside their relationship with you. + + Conveying under any other circumstances is permitted solely under +the conditions stated below. Sublicensing is not allowed; section 10 +makes it unnecessary. + + 3. Protecting Users' Legal Rights From Anti-Circumvention Law. + + No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + + When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention +is effected by exercising rights under this License with respect to +the covered work, and you disclaim any intention to limit operation or +modification of the work as a means of enforcing, against the work's +users, your or third parties' legal rights to forbid circumvention of +technological measures. + + 4. Conveying Verbatim Copies. + + You may convey verbatim copies of the Program's source code as you +receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + + You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + + 5. Conveying Modified Source Versions. + + You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these conditions: + + a) The work must carry prominent notices stating that you modified + it, and giving a relevant date. + + b) The work must carry prominent notices stating that it is + released under this License and any conditions added under section + 7. This requirement modifies the requirement in section 4 to + "keep intact all notices". + + c) You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. + + d) If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + + A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +"aggregate" if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation's users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + + 6. Conveying Non-Source Forms. + + You may convey a covered work in object code form under the terms +of sections 4 and 5, provided that you also convey the +machine-readable Corresponding Source under the terms of this License, +in one of these ways: + + a) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. + + b) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the + Corresponding Source from a network server at no charge. + + c) Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. + + d) Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. + + e) Convey the object code using peer-to-peer transmission, provided + you inform other peers where the object code and Corresponding + Source of the work are being offered to the general public at no + charge under subsection 6d. + + A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + + A "User Product" is either (1) a "consumer product", which means any +tangible personal property which is normally used for personal, family, +or household purposes, or (2) anything designed or sold for incorporation +into a dwelling. In determining whether a product is a consumer product, +doubtful cases shall be resolved in favor of coverage. For a particular +product received by a particular user, "normally used" refers to a +typical or common use of that class of product, regardless of the status +of the particular user or of the way in which the particular user +actually uses, or expects or is expected to use, the product. A product +is a consumer product regardless of whether the product has substantial +commercial, industrial or non-consumer uses, unless such uses represent +the only significant mode of use of the product. + + "Installation Information" for a User Product means any methods, +procedures, authorization keys, or other information required to install +and execute modified versions of a covered work in that User Product from +a modified version of its Corresponding Source. The information must +suffice to ensure that the continued functioning of the modified object +code is in no case prevented or interfered with solely because +modification has been made. + + If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + + The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates +for a work that has been modified or installed by the recipient, or for +the User Product in which it has been modified or installed. Access to a +network may be denied when the modification itself materially and +adversely affects the operation of the network or violates the rules and +protocols for communication across the network. + + Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + + 7. Additional Terms. + + "Additional permissions" are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + + When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + + Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders of +that material) supplement the terms of this License with terms: + + a) Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or + + b) Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or + + c) Prohibiting misrepresentation of the origin of that material, or + requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or + + d) Limiting the use for publicity purposes of names of licensors or + authors of the material; or + + e) Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or + + f) Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions of + it) with contractual assumptions of liability to the recipient, for + any liability that these contractual assumptions directly impose on + those licensors and authors. + + All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + + If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + + Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; +the above requirements apply either way. + + 8. Termination. + + You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + + However, if you cease all violation of this License, then your +license from a particular copyright holder is reinstated (a) +provisionally, unless and until the copyright holder explicitly and +finally terminates your license, and (b) permanently, if the copyright +holder fails to notify you of the violation by some reasonable means +prior to 60 days after the cessation. + + Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + + Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + + 9. Acceptance Not Required for Having Copies. + + You are not required to accept this License in order to receive or +run a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + + 10. Automatic Licensing of Downstream Recipients. + + Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + + An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + + You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + + 11. Patents. + + A "contributor" is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor's "contributor version". + + A contributor's "essential patent claims" are all patent claims +owned or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + + Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor's essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + + In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To "grant" such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + + If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient's use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + + If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + + A patent license is "discriminatory" if it does not include within +the scope of its coverage, prohibits the exercise of, or is +conditioned on the non-exercise of one or more of the rights that are +specifically granted under this License. You may not convey a covered +work if you are a party to an arrangement with a third party that is +in the business of distributing software, under which you make payment +to the third party based on the extent of your activity of conveying +the work, and under which the third party grants, to any of the +parties who would receive the covered work from you, a discriminatory +patent license (a) in connection with copies of the covered work +conveyed by you (or copies made from those copies), or (b) primarily +for and in connection with specific products or compilations that +contain the covered work, unless you entered into that arrangement, +or that patent license was granted, prior to 28 March 2007. + + Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + + 12. No Surrender of Others' Freedom. + + If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot convey a +covered work so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you may +not convey it at all. For example, if you agree to terms that obligate you +to collect a royalty for further conveying from those to whom you convey +the Program, the only way you could satisfy both those terms and this +License would be to refrain entirely from conveying the Program. + + 13. Use with the GNU Affero General Public License. + + Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU Affero General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the special requirements of the GNU Affero General Public License, +section 13, concerning interaction through a network will apply to the +combination as such. + + 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + + Each version is given a distinguishing version number. If the +Program specifies that a certain numbered version of the GNU General +Public License "or any later version" applies to it, you have the +option of following the terms and conditions either of that numbered +version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of the +GNU General Public License, you may choose any version ever published +by the Free Software Foundation. + + If the Program specifies that a proxy can decide which future +versions of the GNU General Public License can be used, that proxy's +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + + Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + + 15. Disclaimer of Warranty. + + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF +ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. Limitation of Liability. + + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF +SUCH DAMAGES. + + 17. Interpretation of Sections 15 and 16. + + If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +state the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + <one line to give the program's name and a brief idea of what it does.> + Copyright (C) <year> <name of author> + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <https://www.gnu.org/licenses/>. + +Also add information on how to contact you by electronic and paper mail. + + If the program does terminal interaction, make it output a short +notice like this when it starts in an interactive mode: + + <program> Copyright (C) <year> <name of author> + This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, your program's commands +might be different; for a GUI interface, you would use an "about box". + + You should also get your employer (if you work as a programmer) or school, +if any, to sign a "copyright disclaimer" for the program, if necessary. +For more information on this, and how to apply and follow the GNU GPL, see +<https://www.gnu.org/licenses/>. + + The GNU General Public License does not permit incorporating your program +into proprietary programs. If your program is a subroutine library, you +may consider it more useful to permit linking proprietary applications with +the library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. But first, please read +<https://www.gnu.org/licenses/why-not-lgpl.html>. diff --git a/Makefile b/Makefile @@ -0,0 +1,72 @@ +CC ?= cc +AR ?= ar +XXD ?= xxd +MARCH ?= native +BITS ?= $(shell getconf LONG_BIT) + +.DEFAULT_GOAL := all + +CSTD := c99 + +SRC_DIR := src +FILES_DIR := files +INCLUDE_DIR := include +BUILD_DIR := build + +OUT := $(BUILD_DIR)/tuxanci2.a + +CONFIG_HEADER := $(BUILD_DIR)/ta_config.h + +FLAGS := -nostdlib -Wall -Wextra -Werror -pedantic -pedantic-errors + +# Needed for ta_types.h +ifeq ($(BITS),64) + ARCH_DEFINE := -DTA_64BIT +else + ARCH_DEFINE := -DTA_32BIT +endif + +CFLAGS := -march=$(MARCH) -mtune=$(MARCH) -m$(BITS) $(ARCH_DEFINE) -I$(INCLUDE_DIR) -I$(BUILD_DIR) -std=$(CSTD) + +ARFLAGS := rcs + +SRCS := $(wildcard $(SRC_DIR)/*.c) +ASSETS := $(shell find $(FILES_DIR) -type f -not -name 'LICENSE') +ASSET_PATHS := $(patsubst $(FILES_DIR)/%,%,$(ASSETS)) +GENERATED_SRCS := $(addprefix $(BUILD_DIR)/,$(addsuffix .c,$(basename $(ASSET_PATHS)))) +OBJS := $(patsubst $(SRC_DIR)/%.c, $(BUILD_DIR)/%.o, $(SRCS)) \ + $(patsubst $(BUILD_DIR)/%.c, $(BUILD_DIR)/%.o, $(GENERATED_SRCS)) +.SECONDARY: $(GENERATED_SRCS) +CONFIG_HEADER_IN := $(INCLUDE_DIR)/ta_config.h.in + +define ASSET_C_RULE +$(BUILD_DIR)/$(basename $(patsubst $(FILES_DIR)/%,%,$(1))).c: $(1) + mkdir -p $$(dir $$@) + $$(XXD) -i -n $$(shell printf '%s' 'ta_file_$(notdir $(basename $(1)))' | sed 's/[^a-zA-Z0-9_]/_/g; /^[0-9]/s/^/_/') $$< $$@ +endef + +$(foreach asset,$(ASSETS),$(eval $(call ASSET_C_RULE,$(asset)))) + +all: $(CONFIG_HEADER) $(GENERATED_SRCS) $(OUT) + +$(OUT): $(OBJS) + $(AR) $(ARFLAGS) $@ $^ + +$(BUILD_DIR): + mkdir -p $(BUILD_DIR) + +$(BUILD_DIR)/%.o: $(SRC_DIR)/%.c | $(BUILD_DIR) + mkdir -p $(dir $@) + $(CC) $(CFLAGS) -c $< -o $@ + +$(BUILD_DIR)/%.o: $(BUILD_DIR)/%.c + mkdir -p $(dir $@) + $(CC) $(CFLAGS) -c $< -o $@ + +$(CONFIG_HEADER): | $(BUILD_DIR) + BITS=$(BITS) envsubst < $(CONFIG_HEADER_IN) > $(CONFIG_HEADER) + +clean: + rm -rf $(BUILD_DIR) + +.PHONY: all clean diff --git a/README.md b/README.md @@ -0,0 +1,2 @@ +# Tuxánci 2 +Spirtiual successor to Tuxánci +\ No newline at end of file diff --git a/files/fonts/LICENSE b/files/fonts/LICENSE @@ -0,0 +1,187 @@ +Fonts are (c) Bitstream (see below). DejaVu changes are in public domain. +Glyphs imported from Arev fonts are (c) Tavmjong Bah (see below) + + +Bitstream Vera Fonts Copyright +------------------------------ + +Copyright (c) 2003 by Bitstream, Inc. All Rights Reserved. Bitstream Vera is +a trademark of Bitstream, Inc. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of the fonts accompanying this license ("Fonts") and associated +documentation files (the "Font Software"), to reproduce and distribute the +Font Software, including without limitation the rights to use, copy, merge, +publish, distribute, and/or sell copies of the Font Software, and to permit +persons to whom the Font Software is furnished to do so, subject to the +following conditions: + +The above copyright and trademark notices and this permission notice shall +be included in all copies of one or more of the Font Software typefaces. + +The Font Software may be modified, altered, or added to, and in particular +the designs of glyphs or characters in the Fonts may be modified and +additional glyphs or characters may be added to the Fonts, only if the fonts +are renamed to names not containing either the words "Bitstream" or the word +"Vera". + +This License becomes null and void to the extent applicable to Fonts or Font +Software that has been modified and is distributed under the "Bitstream +Vera" names. + +The Font Software may be sold as part of a larger software package but no +copy of one or more of the Font Software typefaces may be sold by itself. + +THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF COPYRIGHT, PATENT, +TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL BITSTREAM OR THE GNOME +FOUNDATION BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, INCLUDING +ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF +THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM OTHER DEALINGS IN THE +FONT SOFTWARE. + +Except as contained in this notice, the names of Gnome, the Gnome +Foundation, and Bitstream Inc., shall not be used in advertising or +otherwise to promote the sale, use or other dealings in this Font Software +without prior written authorization from the Gnome Foundation or Bitstream +Inc., respectively. For further information, contact: fonts at gnome dot +org. + +Arev Fonts Copyright +------------------------------ + +Copyright (c) 2006 by Tavmjong Bah. All Rights Reserved. + +Permission is hereby granted, free of charge, to any person obtaining +a copy of the fonts accompanying this license ("Fonts") and +associated documentation files (the "Font Software"), to reproduce +and distribute the modifications to the Bitstream Vera Font Software, +including without limitation the rights to use, copy, merge, publish, +distribute, and/or sell copies of the Font Software, and to permit +persons to whom the Font Software is furnished to do so, subject to +the following conditions: + +The above copyright and trademark notices and this permission notice +shall be included in all copies of one or more of the Font Software +typefaces. + +The Font Software may be modified, altered, or added to, and in +particular the designs of glyphs or characters in the Fonts may be +modified and additional glyphs or characters may be added to the +Fonts, only if the fonts are renamed to names not containing either +the words "Tavmjong Bah" or the word "Arev". + +This License becomes null and void to the extent applicable to Fonts +or Font Software that has been modified and is distributed under the +"Tavmjong Bah Arev" names. + +The Font Software may be sold as part of a larger software package but +no copy of one or more of the Font Software typefaces may be sold by +itself. + +THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT +OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL +TAVMJONG BAH BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL +DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM +OTHER DEALINGS IN THE FONT SOFTWARE. + +Except as contained in this notice, the name of Tavmjong Bah shall not +be used in advertising or otherwise to promote the sale, use or other +dealings in this Font Software without prior written authorization +from Tavmjong Bah. For further information, contact: tavmjong @ free +. fr. + +TeX Gyre DJV Math +----------------- +Fonts are (c) Bitstream (see below). DejaVu changes are in public domain. + +Math extensions done by B. Jackowski, P. Strzelczyk and P. Pianowski +(on behalf of TeX users groups) are in public domain. + +Letters imported from Euler Fraktur from AMSfonts are (c) American +Mathematical Society (see below). +Bitstream Vera Fonts Copyright +Copyright (c) 2003 by Bitstream, Inc. All Rights Reserved. Bitstream Vera +is a trademark of Bitstream, Inc. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of the fonts accompanying this license (“Fonts”) and associated +documentation +files (the “Font Software”), to reproduce and distribute the Font Software, +including without limitation the rights to use, copy, merge, publish, +distribute, +and/or sell copies of the Font Software, and to permit persons to whom +the Font Software is furnished to do so, subject to the following +conditions: + +The above copyright and trademark notices and this permission notice +shall be +included in all copies of one or more of the Font Software typefaces. + +The Font Software may be modified, altered, or added to, and in particular +the designs of glyphs or characters in the Fonts may be modified and +additional +glyphs or characters may be added to the Fonts, only if the fonts are +renamed +to names not containing either the words “Bitstream” or the word “Vera”. + +This License becomes null and void to the extent applicable to Fonts or +Font Software +that has been modified and is distributed under the “Bitstream Vera” +names. + +The Font Software may be sold as part of a larger software package but +no copy +of one or more of the Font Software typefaces may be sold by itself. + +THE FONT SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS +OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF COPYRIGHT, PATENT, +TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL BITSTREAM OR THE GNOME +FOUNDATION +BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, INCLUDING ANY GENERAL, +SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, WHETHER IN AN +ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF THE USE OR +INABILITY TO USE +THE FONT SOFTWARE OR FROM OTHER DEALINGS IN THE FONT SOFTWARE. +Except as contained in this notice, the names of GNOME, the GNOME +Foundation, +and Bitstream Inc., shall not be used in advertising or otherwise to promote +the sale, use or other dealings in this Font Software without prior written +authorization from the GNOME Foundation or Bitstream Inc., respectively. +For further information, contact: fonts at gnome dot org. + +AMSFonts (v. 2.2) copyright + +The PostScript Type 1 implementation of the AMSFonts produced by and +previously distributed by Blue Sky Research and Y&Y, Inc. are now freely +available for general use. This has been accomplished through the +cooperation +of a consortium of scientific publishers with Blue Sky Research and Y&Y. +Members of this consortium include: + +Elsevier Science IBM Corporation Society for Industrial and Applied +Mathematics (SIAM) Springer-Verlag American Mathematical Society (AMS) + +In order to assure the authenticity of these fonts, copyright will be +held by +the American Mathematical Society. This is not meant to restrict in any way +the legitimate use of the fonts, such as (but not limited to) electronic +distribution of documents containing these fonts, inclusion of these fonts +into other public domain or commercial font collections or computer +applications, use of the outline data to create derivative fonts and/or +faces, etc. However, the AMS does require that the AMS copyright notice be +removed from any derivative versions of the fonts which have been altered in +any way. In addition, to ensure the fidelity of TeX documents using Computer +Modern fonts, Professor Donald Knuth, creator of the Computer Modern faces, +has requested that any alterations which yield different font metrics be +given a different name. + +$Id$ diff --git a/files/fonts/dejavu_sans.ttf b/files/fonts/dejavu_sans.ttf Binary files differ. diff --git a/files/images/bg.tai b/files/images/bg.tai Binary files differ. diff --git a/include/ta_api.h b/include/ta_api.h @@ -0,0 +1,30 @@ +#ifndef TA_API_H +#define TA_API_H + +#include "ta_config.h" +#include "ta_types.h" +#include "ta_fb.h" +#include "ta_keyboard.h" +#include "ta_text.h" +#include "ta_button.h" +#include "ta_image.h" + +#ifdef __cplusplus +extern "C" { +#endif + +// Must be called before ta_update() +void ta_main_init(void); + +// Must be called every frame after ta_init() +void ta_main_update(void); + +// Misc stuff that you need to implement/wrap +void *ta_malloc(ta_size size); +void ta_free(void *ptr); + +#ifdef __cplusplus +} +#endif + +#endif // TA_API_H diff --git a/include/ta_button.h b/include/ta_button.h @@ -0,0 +1,31 @@ +#ifndef TA_BUTTON_H +#define TA_BUTTON_H + +#include "ta_types.h" + +typedef void (*ta_button_callback)(void); + +typedef struct { + const char *label; + ta_button_callback callback; +} ta_button; + +typedef struct { + ta_button *buttons; + ta_size size; + ta_size selected; + ta_size animation_from; + ta_uint32 animation_frame; + ta_bool animation_active; + ta_bool up_was_down; + ta_bool down_was_down; + ta_bool enter_was_down; +} ta_button_lists; + +void ta_button_lists_init(ta_button_lists *list); +ta_bool ta_button_lists_add(ta_button_lists *list, const char *label, ta_button_callback callback); +void ta_button_lists_update(ta_button_lists *list); +void ta_button_lists_draw(const ta_button_lists *list); +void ta_button_lists_destroy(ta_button_lists *list); + +#endif +\ No newline at end of file diff --git a/include/ta_config.h.in b/include/ta_config.h.in @@ -0,0 +1,6 @@ +#ifndef TA_CONFIG_H +#define TA_CONFIG_H + +#define TA_BITS $BITS + +#endif // TA_CONFIG_H diff --git a/include/ta_fb.h b/include/ta_fb.h @@ -0,0 +1,19 @@ +#ifndef TA_FB_H +#define TA_FB_H + +#include "ta_types.h" + +typedef struct { + ta_size width, height; + ta_size bpp; + ta_size size; + void *buffers[2]; +} ta_fbs; + +extern ta_fbs ta_fb; + +ta_size ta_fb_init(ta_fbs *fb); +void ta_fb_swap(ta_fbs *fb); +void ta_fb_clear(ta_fbs *fb); + +#endif diff --git a/include/ta_files.h b/include/ta_files.h @@ -0,0 +1,9 @@ +#ifndef TA_FILES_H +#define TA_FILES_H + +extern unsigned char ta_file_dejavu_sans[]; +extern unsigned int ta_file_dejavu_sans_len; +extern unsigned char ta_file_bg[]; +extern unsigned int ta_file_bg_len; + +#endif diff --git a/include/ta_image.h b/include/ta_image.h @@ -0,0 +1,9 @@ +#ifndef TA_IMAGE_H +#define TA_IMAGE_H + +#include "ta_types.h" + +ta_bool ta_image_draw(const ta_uint8 *data, ta_uint32 length, ta_uint32 x, ta_uint32 y, ta_uint32 width, ta_uint32 height, ta_uint16 frame); +void ta_image_release(void); + +#endif diff --git a/include/ta_keyboard.h b/include/ta_keyboard.h @@ -0,0 +1,149 @@ +#ifndef TA_KEYBOARD_H +#define TA_KEYBOARD_H + +#include "ta_types.h" + +#define TA_KEYBOARD_MAX_KEYS_DOWN 5 + +typedef enum { + KEY_NONE = 0, + KEY_BACKSPACE = 8, + KEY_TAB = 9, + KEY_ENTER = 13, + KEY_ESCAPE = 27, + KEY_SPACE = 32, + KEY_EXCLAMATION_MARK = 33, + KEY_QUOTATION_MARK = 34, + KEY_HASH = 35, + KEY_DOLLAR_SIGN = 36, + KEY_PERCENT_SIGN = 37, + KEY_AMPERSAND = 38, + KEY_APOSTROPHE = 39, + KEY_LEFT_PARENTHESIS = 40, + KEY_RIGHT_PARENTHESIS = 41, + KEY_ASTERISK = 42, + KEY_PLUS = 43, + KEY_COMMA = 44, + KEY_MINUS = 45, + KEY_PERIOD = 46, + KEY_SLASH = 47, + KEY_0 = 48, + KEY_1 = 49, + KEY_2 = 50, + KEY_3 = 51, + KEY_4 = 52, + KEY_5 = 53, + KEY_6 = 54, + KEY_7 = 55, + KEY_8 = 56, + KEY_9 = 57, + KEY_COLON = 58, + KEY_SEMICOLON = 59, + KEY_LESS_THAN = 60, + KEY_EQUALS = 61, + KEY_GREATER_THAN = 62, + KEY_QUESTION_MARK = 63, + KEY_AT_SIGN = 64, + KEY_A = 97, + KEY_B = 98, + KEY_C = 99, + KEY_D = 100, + KEY_E = 101, + KEY_F = 102, + KEY_G = 103, + KEY_H = 104, + KEY_I = 105, + KEY_J = 106, + KEY_K = 107, + KEY_L = 108, + KEY_M = 109, + KEY_N = 110, + KEY_O = 111, + KEY_P = 112, + KEY_Q = 113, + KEY_R = 114, + KEY_S = 115, + KEY_T = 116, + KEY_U = 117, + KEY_V = 118, + KEY_W = 119, + KEY_X = 120, + KEY_Y = 121, + KEY_Z = 122, + KEY_LEFT_BRACKET = 91, + KEY_BACKSLASH = 92, + KEY_RIGHT_BRACKET = 93, + KEY_CARET = 94, + KEY_UNDERSCORE = 95, + KEY_GRAVE_ACCENT = 96, + KEY_LEFT_BRACE = 123, + KEY_PIPE = 124, + KEY_RIGHT_BRACE = 125, + KEY_TILDE = 126, + KEY_DELETE = 127, + KEY_UP = 128, + KEY_DOWN = 129, + KEY_LEFT = 130, + KEY_RIGHT = 131, + KEY_F1 = 132, + KEY_F2 = 133, + KEY_F3 = 134, + KEY_F4 = 135, + KEY_F5 = 136, + KEY_F6 = 137, + KEY_F7 = 138, + KEY_F8 = 139, + KEY_F9 = 140, + KEY_F10 = 141, + KEY_F11 = 142, + KEY_F12 = 143, + KEY_HOME = 144, + KEY_END = 145, + KEY_PAGE_UP = 146, + KEY_PAGE_DOWN = 147, + KEY_INSERT = 148, + KEY_CAPS_LOCK = 149, + KEY_NUM_LOCK = 150, + KEY_SCROLL_LOCK = 151, + KEY_PRINT_SCREEN = 152, + KEY_PAUSE = 153, + KEY_MENU = 154, + KEY_LEFT_SHIFT = 155, + KEY_RIGHT_SHIFT = 156, + KEY_LEFT_CTRL = 157, + KEY_RIGHT_CTRL = 158, + KEY_LEFT_ALT = 159, + KEY_RIGHT_ALT = 160, + KEY_LEFT_SUPER = 161, + KEY_RIGHT_SUPER = 162, + KEY_KP_0 = 163, + KEY_KP_1 = 164, + KEY_KP_2 = 165, + KEY_KP_3 = 166, + KEY_KP_4 = 167, + KEY_KP_5 = 168, + KEY_KP_6 = 169, + KEY_KP_7 = 170, + KEY_KP_8 = 171, + KEY_KP_9 = 172, + KEY_KP_DECIMAL = 173, + KEY_KP_DIVIDE = 174, + KEY_KP_MULTIPLY = 175, + KEY_KP_SUBTRACT = 176, + KEY_KP_ADD = 177, + KEY_KP_ENTER = 178, + KEY_KP_EQUALS = 179 +} ta_keyboard_keys; + +typedef struct { + ta_uint8 keys_down[TA_KEYBOARD_MAX_KEYS_DOWN]; +} ta_keyboards; + +extern ta_keyboards ta_keyboard; + +ta_bool ta_keyboard_is_down(ta_keyboard_keys key); +void ta_keyboard_key_down(ta_keyboard_keys key); +void ta_keyboard_key_up(ta_keyboard_keys key); +void ta_keyboard_clear(void); + +#endif // TA_KEYBOARD_H +\ No newline at end of file diff --git a/include/ta_text.h b/include/ta_text.h @@ -0,0 +1,11 @@ +#ifndef TA_TEXT_H +#define TA_TEXT_H + +#include "ta_types.h" + +void ta_text_draw(const char *text, ta_uint32 x, ta_uint32 y, ta_uint32 width, ta_uint32 height, ta_uint8 red, ta_uint8 green, ta_uint8 blue); +void ta_text_draw_outline(const char *text, ta_uint32 x, ta_uint32 y, ta_uint32 width, ta_uint32 height, ta_uint8 red, ta_uint8 green, ta_uint8 blue, ta_uint8 border_thickness, ta_uint8 border_red, ta_uint8 border_green, ta_uint8 border_blue); +void ta_text_draw_size(const char *text, ta_uint32 x, ta_uint32 y, ta_uint32 size, ta_uint8 red, ta_uint8 green, ta_uint8 blue); +void ta_text_measure(const char *text, ta_uint32 size, ta_uint32 *width, ta_uint32 *height); + +#endif // TA_TEXT_H +\ No newline at end of file diff --git a/include/ta_types.h b/include/ta_types.h @@ -0,0 +1,29 @@ +#ifndef TA_TYPES_H +#define TA_TYPES_H + +#include "ta_config.h" + +typedef unsigned char ta_uint8; +typedef unsigned short ta_uint16; +typedef unsigned int ta_uint32; +typedef unsigned long long ta_uint64; + +typedef signed char ta_int8; +typedef short ta_int16; +typedef int ta_int32; +typedef long long ta_int64; + +#if TA_BITS == 64 + typedef ta_uint64 ta_size; +#elif TA_BITS == 32 + typedef ta_uint32 ta_size; +#else + #error "TA_BITS is not set!" +#endif + +typedef _Bool ta_bool; + +#define ta_true 1 +#define ta_false 0 + +#endif // TA_STDINT_H diff --git a/originals/bg.jpg b/originals/bg.jpg Binary files differ. diff --git a/platforms/dos/.gitignore b/platforms/dos/.gitignore @@ -0,0 +1,3 @@ +tuxanci2.exe +CWSDPMI.EXE +CWSDPMI.SWP +\ No newline at end of file diff --git a/platforms/dos/Makefile b/platforms/dos/Makefile @@ -0,0 +1,35 @@ +MAKEFILE_DIR := $(dir $(abspath $(lastword $(MAKEFILE_LIST)))) +ROOT_DIR := $(abspath $(MAKEFILE_DIR)/../..) + +DOS_CC ?= i586-pc-msdosdjgpp-gcc +DOS_AR ?= i586-pc-msdosdjgpp-ar +DOS_MARCH ?= i386 +DOS_BITS := 32 +DOS_CFLAGS ?= -O2 +DOS_BUILD ?= build/dos + +DOS_SOURCE := $(MAKEFILE_DIR)dos.c +DOS_BUILD_DIR := $(ROOT_DIR)/$(DOS_BUILD) +DOS_LIBRARY := $(DOS_BUILD_DIR)/tuxanci2.a +TARGET := $(MAKEFILE_DIR)tuxanci2.exe +ROOT_INPUTS := $(wildcard $(ROOT_DIR)/src/*.c) \ + $(wildcard $(ROOT_DIR)/include/*.h) \ + $(ROOT_DIR)/include/ta_config.h.in \ + $(shell find $(ROOT_DIR)/files -type f) \ + $(ROOT_DIR)/Makefile + +.DEFAULT_GOAL := all + +.PHONY: all clean + +all: $(TARGET) + +$(DOS_LIBRARY): $(ROOT_INPUTS) + $(MAKE) -C "$(ROOT_DIR)" CC="$(DOS_CC)" AR="$(DOS_AR)" BITS=$(DOS_BITS) MARCH=$(DOS_MARCH) BUILD_DIR="$(DOS_BUILD)" all + +$(TARGET): $(DOS_SOURCE) $(DOS_LIBRARY) + $(DOS_CC) $(DOS_CFLAGS) -I"$(ROOT_DIR)/include" -I"$(DOS_BUILD_DIR)" "$(DOS_SOURCE)" "$(DOS_LIBRARY)" -o "$@" + +clean: + $(RM) "$(TARGET)" + $(MAKE) -C "$(ROOT_DIR)" BUILD_DIR="$(DOS_BUILD)" clean diff --git a/platforms/dos/dos.c b/platforms/dos/dos.c @@ -0,0 +1,201 @@ +#include "ta_api.h" +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <dos.h> +#include <dpmi.h> +#include <go32.h> +#include <conio.h> +#include <pc.h> + +#define WINDOW_WIDTH 320 +#define WINDOW_HEIGHT 200 +#define WINDOW_BPP 8 + +#define VGA_FB_ADDR 0xA0000UL + +static void set_mode_13h(void) { + __dpmi_regs regs; + memset(&regs, 0, sizeof(regs)); + regs.x.ax = 0x0013; + __dpmi_int(0x10, &regs); +} + +static void clear_text_screen(void) { + static const unsigned char palette[16][3] = { + { 0, 0, 0}, { 0, 0, 42}, { 0, 42, 0}, { 0, 42, 42}, + {42, 0, 0}, {42, 0, 42}, {42, 21, 0}, {42, 42, 42}, + {21, 21, 21}, {21, 21, 63}, {21, 63, 21}, {21, 63, 63}, + {63, 21, 21}, {63, 21, 63}, {63, 63, 21}, {63, 63, 63} + }; + __dpmi_regs regs; + memset(&regs, 0, sizeof(regs)); + regs.x.ax = 0x0003; + __dpmi_int(0x10, &regs); + + outportb(0x3C8, 0); + for (int i = 0; i < 16; i++) { + outportb(0x3C9, palette[i][0]); + outportb(0x3C9, palette[i][1]); + outportb(0x3C9, palette[i][2]); + } + + memset(&regs, 0, sizeof(regs)); + regs.x.ax = 0x0100; + regs.x.cx = 0x0607; + __dpmi_int(0x10, &regs); + + memset(&regs, 0, sizeof(regs)); + regs.x.ax = 0x0600; + regs.x.bx = 0x0700; + regs.x.cx = 0; + regs.x.dx = 0x184F; + __dpmi_int(0x10, &regs); + + memset(&regs, 0, sizeof(regs)); + regs.x.ax = 0x0200; + regs.x.bx = 0; + regs.x.dx = 0; + __dpmi_int(0x10, &regs); +} + +static void set_palette_332(void) { + int i; + outportb(0x3C8, 0); + for (i = 0; i < 256; i++) { + outportb(0x3C9, ((i >> 5) & 7) * 63 / 7); + outportb(0x3C9, ((i >> 2) & 7) * 63 / 7); + outportb(0x3C9, (i & 3) * 63 / 3); + } +} + +static void blit(void) { + dosmemput(ta_fb.buffers[0], WINDOW_WIDTH * WINDOW_HEIGHT, VGA_FB_ADDR); +} + +static ta_keyboard_keys key_from_dos(int key, int extended) { + if (extended) { + switch (key) { + case 29: return KEY_LEFT_CTRL; + case 42: return KEY_LEFT_SHIFT; + case 54: return KEY_RIGHT_SHIFT; + case 56: return KEY_LEFT_ALT; + case 59: return KEY_F1; + case 60: return KEY_F2; + case 61: return KEY_F3; + case 62: return KEY_F4; + case 63: return KEY_F5; + case 64: return KEY_F6; + case 65: return KEY_F7; + case 66: return KEY_F8; + case 67: return KEY_F9; + case 68: return KEY_F10; + case 71: return KEY_HOME; + case 72: return KEY_UP; + case 73: return KEY_PAGE_UP; + case 75: return KEY_LEFT; + case 77: return KEY_RIGHT; + case 79: return KEY_END; + case 80: return KEY_DOWN; + case 81: return KEY_PAGE_DOWN; + case 82: return KEY_INSERT; + case 83: return KEY_DELETE; + case 133: return KEY_F11; + case 134: return KEY_F12; + default: return KEY_NONE; + } + } + + if (key >= 'A' && key <= 'Z') { + key += 'a' - 'A'; + } + + if ((key >= 'a' && key <= 'z') || (key >= '0' && key <= '9') || + (key >= KEY_SPACE && key <= KEY_AT_SIGN) || + (key >= KEY_LEFT_BRACKET && key <= KEY_TILDE)) { + return (ta_keyboard_keys)key; + } + + switch (key) { + case 8: return KEY_BACKSPACE; + case 9: return KEY_TAB; + case 13: return KEY_ENTER; + case 27: return KEY_ESCAPE; + case 127: return KEY_DELETE; + case 42: return KEY_LEFT_SHIFT; + case 54: return KEY_RIGHT_SHIFT; + case 29: return KEY_LEFT_CTRL; + case 56: return KEY_LEFT_ALT; + case 71: return KEY_HOME; + case 72: return KEY_UP; + case 73: return KEY_PAGE_UP; + case 75: return KEY_LEFT; + case 77: return KEY_RIGHT; + case 79: return KEY_END; + case 80: return KEY_DOWN; + case 81: return KEY_PAGE_DOWN; + case 82: return KEY_INSERT; + case 83: return KEY_DELETE; + case 59: return KEY_F1; + case 60: return KEY_F2; + case 61: return KEY_F3; + case 62: return KEY_F4; + case 63: return KEY_F5; + case 64: return KEY_F6; + case 65: return KEY_F7; + case 66: return KEY_F8; + case 67: return KEY_F9; + case 68: return KEY_F10; + case 133: return KEY_F11; + case 134: return KEY_F12; + default: return KEY_NONE; + } +} + +int main(void) { + ta_fb.width = WINDOW_WIDTH; + ta_fb.height = WINDOW_HEIGHT; + ta_fb.bpp = WINDOW_BPP; + + if (atexit(clear_text_screen) != 0) { + return 1; + } + + set_mode_13h(); + set_palette_332(); + + ta_main_init(); + + int running = 1; + while (running) { + ta_keyboard_clear(); + if (kbhit()) { + int key = getch(); + int extended = 0; + if (key == 0 || key == 224) { + key = getch(); + extended = 1; + } + ta_keyboard_keys mapped_key = key_from_dos(key, extended); + ta_keyboard_key_down(mapped_key); + if (mapped_key == KEY_ESCAPE) { + running = 0; + } + } + + ta_main_update(); + blit(); + ta_keyboard_clear(); + delay(16); + } + + return 0; +} + +void *ta_malloc(ta_size size) { + return malloc(size); +} + +void ta_free(void *ptr) { + free(ptr); +} diff --git a/platforms/sdl2/.gitignore b/platforms/sdl2/.gitignore @@ -0,0 +1 @@ +tuxanci2 +\ No newline at end of file diff --git a/platforms/sdl2/Makefile b/platforms/sdl2/Makefile @@ -0,0 +1,33 @@ +MAKEFILE_DIR := $(dir $(abspath $(lastword $(MAKEFILE_LIST)))) +ROOT_DIR := $(abspath $(MAKEFILE_DIR)/../..) + +SDL_CC ?= cc +SDL_CFLAGS ?= -O2 +SDL_LDFLAGS ?= +SDL_LDLIBS ?= -lSDL2 +SDL_BUILD ?= build/sdl2 + +SDL_SOURCE := $(MAKEFILE_DIR)sdl2.c +SDL_BUILD_DIR := $(ROOT_DIR)/$(SDL_BUILD) +SDL_LIBRARY := $(SDL_BUILD_DIR)/tuxanci2.a +TARGET := $(MAKEFILE_DIR)tuxanci2 +ROOT_INPUTS := $(wildcard $(ROOT_DIR)/src/*.c) \ + $(wildcard $(ROOT_DIR)/include/*.h) \ + $(ROOT_DIR)/include/ta_config.h.in \ + $(shell find $(ROOT_DIR)/files -type f) \ + $(ROOT_DIR)/Makefile + +.DEFAULT_GOAL := all +.PHONY: all clean + +all: $(TARGET) + +$(SDL_LIBRARY): $(ROOT_INPUTS) + $(MAKE) -C "$(ROOT_DIR)" CC="$(SDL_CC)" BUILD_DIR="$(SDL_BUILD)" all + +$(TARGET): $(SDL_SOURCE) $(SDL_LIBRARY) + $(SDL_CC) $(SDL_CFLAGS) -I"$(ROOT_DIR)/include" -I"$(SDL_BUILD_DIR)" "$(SDL_SOURCE)" "$(SDL_LIBRARY)" $(SDL_LDFLAGS) $(SDL_LDLIBS) -o "$@" + +clean: + $(RM) "$(TARGET)" + $(MAKE) -C "$(ROOT_DIR)" BUILD_DIR="$(SDL_BUILD)" clean diff --git a/platforms/sdl2/sdl2.c b/platforms/sdl2/sdl2.c @@ -0,0 +1,131 @@ +#include "ta_api.h" +#include <SDL2/SDL.h> +#include <unistd.h> +#include <stdlib.h> + +#define WINDOW_WIDTH 800 +#define WINDOW_HEIGHT 600 +#define WINDOW_BPP 32 +#define TARGET_FPS 60 +#define FRAME_TIME (1000000 / TARGET_FPS) + +static ta_keyboard_keys key_from_sdl(SDL_Keycode sym) { + if (sym >= SDLK_a && sym <= SDLK_z) { + return (ta_keyboard_keys)(KEY_A + (sym - SDLK_a)); + } + + if (sym >= SDLK_0 && sym <= SDLK_9) { + return (ta_keyboard_keys)(KEY_0 + (sym - SDLK_0)); + } + + switch (sym) { + case SDLK_BACKSPACE: return KEY_BACKSPACE; + case SDLK_TAB: return KEY_TAB; + case SDLK_RETURN: return KEY_ENTER; + case SDLK_ESCAPE: return KEY_ESCAPE; + case SDLK_SPACE: return KEY_SPACE; + case SDLK_DELETE: return KEY_DELETE; + case SDLK_UP: return KEY_UP; + case SDLK_DOWN: return KEY_DOWN; + case SDLK_LEFT: return KEY_LEFT; + case SDLK_RIGHT: return KEY_RIGHT; + case SDLK_F1: return KEY_F1; + case SDLK_F2: return KEY_F2; + case SDLK_F3: return KEY_F3; + case SDLK_F4: return KEY_F4; + case SDLK_F5: return KEY_F5; + case SDLK_F6: return KEY_F6; + case SDLK_F7: return KEY_F7; + case SDLK_F8: return KEY_F8; + case SDLK_F9: return KEY_F9; + case SDLK_F10: return KEY_F10; + case SDLK_F11: return KEY_F11; + case SDLK_F12: return KEY_F12; + case SDLK_HOME: return KEY_HOME; + case SDLK_END: return KEY_END; + case SDLK_PAGEUP: return KEY_PAGE_UP; + case SDLK_PAGEDOWN: return KEY_PAGE_DOWN; + case SDLK_INSERT: return KEY_INSERT; + case SDLK_CAPSLOCK: return KEY_CAPS_LOCK; + case SDLK_NUMLOCKCLEAR: return KEY_NUM_LOCK; + case SDLK_SCROLLLOCK: return KEY_SCROLL_LOCK; + case SDLK_PRINTSCREEN: return KEY_PRINT_SCREEN; + case SDLK_PAUSE: return KEY_PAUSE; + case SDLK_APPLICATION: return KEY_MENU; + case SDLK_LSHIFT: return KEY_LEFT_SHIFT; + case SDLK_RSHIFT: return KEY_RIGHT_SHIFT; + case SDLK_LCTRL: return KEY_LEFT_CTRL; + case SDLK_RCTRL: return KEY_RIGHT_CTRL; + case SDLK_LALT: return KEY_LEFT_ALT; + case SDLK_RALT: return KEY_RIGHT_ALT; + case SDLK_LGUI: return KEY_LEFT_SUPER; + case SDLK_RGUI: return KEY_RIGHT_SUPER; + default: return KEY_NONE; + } +} + +int main(void) { + if (SDL_Init(SDL_INIT_VIDEO) != 0) { + return 1; + } + + ta_fb.width = WINDOW_WIDTH; + ta_fb.height = WINDOW_HEIGHT; + ta_fb.bpp = WINDOW_BPP; + + ta_main_init(); + + SDL_Window *window = SDL_CreateWindow("", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_SHOWN); + if (!window) { + SDL_Quit(); + return 1; + } + + SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); + SDL_Texture *texture = SDL_CreateTexture( + renderer, + SDL_PIXELFORMAT_ARGB8888, + SDL_TEXTUREACCESS_STREAMING, + WINDOW_WIDTH, + WINDOW_HEIGHT + ); + + int running = 1; + while (running) { + SDL_Event event; + while (SDL_PollEvent(&event)) { + if (event.type == SDL_QUIT) { + running = 0; + } else if (event.type == SDL_KEYDOWN) { + ta_keyboard_key_down(key_from_sdl(event.key.keysym.sym)); + } else if (event.type == SDL_KEYUP) { + ta_keyboard_key_up(key_from_sdl(event.key.keysym.sym)); + } else if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_FOCUS_LOST) { + ta_keyboard_clear(); + } + } + + ta_main_update(); + + SDL_UpdateTexture(texture, NULL, ta_fb.buffers[0], WINDOW_WIDTH * sizeof(Uint32)); + SDL_RenderClear(renderer); + SDL_RenderCopy(renderer, texture, NULL, NULL); + SDL_RenderPresent(renderer); + + usleep(FRAME_TIME); + } + + SDL_DestroyTexture(texture); + SDL_DestroyRenderer(renderer); + SDL_DestroyWindow(window); + SDL_Quit(); + return 0; +} + +void *ta_malloc(ta_size size) { + return malloc(size); +} + +void ta_free(void *ptr) { + free(ptr); +} diff --git a/platforms/sdl3/.gitignore b/platforms/sdl3/.gitignore @@ -0,0 +1 @@ +tuxanci2 +\ No newline at end of file diff --git a/platforms/sdl3/Makefile b/platforms/sdl3/Makefile @@ -0,0 +1,33 @@ +MAKEFILE_DIR := $(dir $(abspath $(lastword $(MAKEFILE_LIST)))) +ROOT_DIR := $(abspath $(MAKEFILE_DIR)/../..) + +SDL_CC ?= cc +SDL_CFLAGS ?= -O2 +SDL_LDFLAGS ?= +SDL_LDLIBS ?= -lSDL3 +SDL_BUILD ?= build/sdl3 + +SDL_SOURCE := $(MAKEFILE_DIR)sdl3.c +SDL_BUILD_DIR := $(ROOT_DIR)/$(SDL_BUILD) +SDL_LIBRARY := $(SDL_BUILD_DIR)/tuxanci2.a +TARGET := $(MAKEFILE_DIR)tuxanci2 +ROOT_INPUTS := $(wildcard $(ROOT_DIR)/src/*.c) \ + $(wildcard $(ROOT_DIR)/include/*.h) \ + $(ROOT_DIR)/include/ta_config.h.in \ + $(shell find $(ROOT_DIR)/files -type f) \ + $(ROOT_DIR)/Makefile + +.DEFAULT_GOAL := all +.PHONY: all clean + +all: $(TARGET) + +$(SDL_LIBRARY): $(ROOT_INPUTS) + $(MAKE) -C "$(ROOT_DIR)" CC="$(SDL_CC)" BUILD_DIR="$(SDL_BUILD)" all + +$(TARGET): $(SDL_SOURCE) $(SDL_LIBRARY) + $(SDL_CC) $(SDL_CFLAGS) -I"$(ROOT_DIR)/include" -I"$(SDL_BUILD_DIR)" "$(SDL_SOURCE)" "$(SDL_LIBRARY)" $(SDL_LDFLAGS) $(SDL_LDLIBS) -o "$@" + +clean: + $(RM) "$(TARGET)" + $(MAKE) -C "$(ROOT_DIR)" BUILD_DIR="$(SDL_BUILD)" clean diff --git a/platforms/sdl3/sdl3.c b/platforms/sdl3/sdl3.c @@ -0,0 +1,139 @@ +#include "ta_api.h" +#include <SDL3/SDL.h> +#include <unistd.h> +#include <stdlib.h> + +#define WINDOW_WIDTH 800 +#define WINDOW_HEIGHT 600 +#define WINDOW_BPP 32 +#define TARGET_FPS 60 +#define FRAME_TIME (1000000 / TARGET_FPS) + +static ta_keyboard_keys key_from_sdl(SDL_Keycode sym) { + if (sym >= SDLK_A && sym <= SDLK_Z) { + return (ta_keyboard_keys)(KEY_A + (sym - SDLK_A)); + } + + if (sym >= SDLK_0 && sym <= SDLK_9) { + return (ta_keyboard_keys)(KEY_0 + (sym - SDLK_0)); + } + + switch (sym) { + case SDLK_BACKSPACE: return KEY_BACKSPACE; + case SDLK_TAB: return KEY_TAB; + case SDLK_RETURN: return KEY_ENTER; + case SDLK_ESCAPE: return KEY_ESCAPE; + case SDLK_SPACE: return KEY_SPACE; + case SDLK_DELETE: return KEY_DELETE; + case SDLK_UP: return KEY_UP; + case SDLK_DOWN: return KEY_DOWN; + case SDLK_LEFT: return KEY_LEFT; + case SDLK_RIGHT: return KEY_RIGHT; + case SDLK_F1: return KEY_F1; + case SDLK_F2: return KEY_F2; + case SDLK_F3: return KEY_F3; + case SDLK_F4: return KEY_F4; + case SDLK_F5: return KEY_F5; + case SDLK_F6: return KEY_F6; + case SDLK_F7: return KEY_F7; + case SDLK_F8: return KEY_F8; + case SDLK_F9: return KEY_F9; + case SDLK_F10: return KEY_F10; + case SDLK_F11: return KEY_F11; + case SDLK_F12: return KEY_F12; + case SDLK_HOME: return KEY_HOME; + case SDLK_END: return KEY_END; + case SDLK_PAGEUP: return KEY_PAGE_UP; + case SDLK_PAGEDOWN: return KEY_PAGE_DOWN; + case SDLK_INSERT: return KEY_INSERT; + case SDLK_CAPSLOCK: return KEY_CAPS_LOCK; + case SDLK_NUMLOCKCLEAR: return KEY_NUM_LOCK; + case SDLK_SCROLLLOCK: return KEY_SCROLL_LOCK; + case SDLK_PRINTSCREEN: return KEY_PRINT_SCREEN; + case SDLK_PAUSE: return KEY_PAUSE; + case SDLK_APPLICATION: return KEY_MENU; + case SDLK_LSHIFT: return KEY_LEFT_SHIFT; + case SDLK_RSHIFT: return KEY_RIGHT_SHIFT; + case SDLK_LCTRL: return KEY_LEFT_CTRL; + case SDLK_RCTRL: return KEY_RIGHT_CTRL; + case SDLK_LALT: return KEY_LEFT_ALT; + case SDLK_RALT: return KEY_RIGHT_ALT; + case SDLK_LGUI: return KEY_LEFT_SUPER; + case SDLK_RGUI: return KEY_RIGHT_SUPER; + default: return KEY_NONE; + } +} + +int main(void) { + if (!SDL_Init(SDL_INIT_VIDEO)) { + return 1; + } + + ta_fb.width = WINDOW_WIDTH; + ta_fb.height = WINDOW_HEIGHT; + ta_fb.bpp = WINDOW_BPP; + + ta_main_init(); + + SDL_Window *window = SDL_CreateWindow("", WINDOW_WIDTH, WINDOW_HEIGHT, 0); + if (!window) { + SDL_Quit(); + return 1; + } + + SDL_SetWindowPosition(window, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED); + + SDL_Renderer *renderer = SDL_CreateRenderer(window, NULL); + if (!renderer) { + SDL_DestroyWindow(window); + SDL_Quit(); + return 1; + } + + SDL_Texture *texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_XRGB8888, SDL_TEXTUREACCESS_STREAMING, WINDOW_WIDTH, WINDOW_HEIGHT); + if (!texture) { + SDL_DestroyRenderer(renderer); + SDL_DestroyWindow(window); + SDL_Quit(); + return 1; + } + + int running = 1; + while (running) { + SDL_Event event; + while (SDL_PollEvent(&event)) { + if (event.type == SDL_EVENT_QUIT) { + running = 0; + } else if (event.type == SDL_EVENT_KEY_DOWN) { + ta_keyboard_key_down(key_from_sdl(event.key.key)); + } else if (event.type == SDL_EVENT_KEY_UP) { + ta_keyboard_key_up(key_from_sdl(event.key.key)); + } else if (event.type == SDL_EVENT_WINDOW_FOCUS_LOST) { + ta_keyboard_clear(); + } + } + + ta_main_update(); + + SDL_UpdateTexture(texture, NULL, ta_fb.buffers[0], WINDOW_WIDTH * sizeof(Uint32)); + SDL_RenderClear(renderer); + SDL_RenderTexture(renderer, texture, NULL, NULL); + SDL_RenderPresent(renderer); + + usleep(FRAME_TIME); + } + + SDL_DestroyTexture(texture); + SDL_DestroyRenderer(renderer); + SDL_DestroyWindow(window); + SDL_Quit(); + return 0; +} + +void *ta_malloc(ta_size size) { + return malloc(size); +} + +void ta_free(void *ptr) { + free(ptr); +} diff --git a/platforms/wayland/.gitignore b/platforms/wayland/.gitignore @@ -0,0 +1 @@ +tuxanci2 +\ No newline at end of file diff --git a/platforms/wayland/Makefile b/platforms/wayland/Makefile @@ -0,0 +1,42 @@ +MAKEFILE_DIR := $(dir $(abspath $(lastword $(MAKEFILE_LIST)))) +ROOT_DIR := $(abspath $(MAKEFILE_DIR)/../..) + +WL_CC ?= cc +WL_CFLAGS ?= -O2 +WL_LDFLAGS ?= +WL_LDLIBS ?= -lwayland-client -lxkbcommon -lrt -lm +WL_BUILD ?= build/wayland + +WL_SOURCE := $(MAKEFILE_DIR)wayland.c +WL_BUILD_DIR := $(ROOT_DIR)/$(WL_BUILD) +WL_LIBRARY := $(WL_BUILD_DIR)/tuxanci2.a +TARGET := $(MAKEFILE_DIR)tuxanci2 + +XDG_SHELL_H := $(WL_BUILD_DIR)/xdg-shell-client-protocol.h +XDG_SHELL_C := $(WL_BUILD_DIR)/xdg-shell-protocol.c + +ROOT_INPUTS := $(wildcard $(ROOT_DIR)/src/*.c) $(wildcard $(ROOT_DIR)/include/*.h) $(ROOT_DIR)/include/ta_config.h.in $(shell find $(ROOT_DIR)/files -type f) $(ROOT_DIR)/Makefile + +.DEFAULT_GOAL := all +.PHONY: all clean + +all: $(TARGET) + +$(WL_BUILD_DIR): + mkdir -p $(WL_BUILD_DIR) + +$(XDG_SHELL_H): | $(WL_BUILD_DIR) + wayland-scanner client-header $$(pkg-config --variable=pkgdatadir wayland-protocols)/stable/xdg-shell/xdg-shell.xml $@ + +$(XDG_SHELL_C): | $(WL_BUILD_DIR) + wayland-scanner private-code $$(pkg-config --variable=pkgdatadir wayland-protocols)/stable/xdg-shell/xdg-shell.xml $@ + +$(WL_LIBRARY): $(ROOT_INPUTS) + $(MAKE) -C "$(ROOT_DIR)" CC="$(WL_CC)" BUILD_DIR="$(WL_BUILD)" all + +$(TARGET): $(WL_SOURCE) $(XDG_SHELL_H) $(XDG_SHELL_C) $(WL_LIBRARY) + $(WL_CC) $(WL_CFLAGS) -I"$(ROOT_DIR)/include" -I"$(WL_BUILD_DIR)" "$(WL_SOURCE)" "$(XDG_SHELL_C)" "$(WL_LIBRARY)" $(WL_LDFLAGS) $(WL_LDLIBS) -o "$@" + +clean: + $(RM) "$(TARGET)" "$(XDG_SHELL_H)" "$(XDG_SHELL_C)" + $(MAKE) -C "$(ROOT_DIR)" BUILD_DIR="$(WL_BUILD)" clean diff --git a/platforms/wayland/wayland.c b/platforms/wayland/wayland.c @@ -0,0 +1,560 @@ +#define _POSIX_C_SOURCE 200809L + +#include "ta_api.h" +#include <errno.h> +#include <fcntl.h> +#include <linux/input-event-codes.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/mman.h> +#include <unistd.h> +#include <wayland-client.h> +#include <xkbcommon/xkbcommon.h> +#include <xkbcommon/xkbcommon-keysyms.h> +#include "xdg-shell-client-protocol.h" + +#define WINDOW_WIDTH 800 +#define WINDOW_HEIGHT 600 +#define WINDOW_BPP 32 + +typedef struct { + struct wl_display *display; + struct wl_registry *registry; + struct wl_compositor *compositor; + struct wl_shm *shm; + struct wl_seat *seat; + struct wl_keyboard *keyboard; + struct xdg_wm_base *wm_base; + struct wl_surface *surface; + struct xdg_surface *xdg_surface; + struct xdg_toplevel *toplevel; + struct wl_buffer *buffer; + struct wl_callback *frame_callback; + struct xkb_context *xkb_context; + struct xkb_keymap *xkb_keymap; + struct xkb_state *xkb_state; + void *pixels; + size_t buffer_size; + int running; + int configured; + int frame_ready; + int buffer_released; +} wayland_state; + +static ta_keyboard_keys key_from_keysym(xkb_keysym_t symbol) { + symbol = xkb_keysym_to_lower(symbol); + if (symbol >= XKB_KEY_a && symbol <= XKB_KEY_z) { + return (ta_keyboard_keys)(KEY_A + symbol - XKB_KEY_a); + } + if (symbol >= XKB_KEY_0 && symbol <= XKB_KEY_9) { + return (ta_keyboard_keys)(KEY_0 + symbol - XKB_KEY_0); + } + + switch (symbol) { + case XKB_KEY_BackSpace: return KEY_BACKSPACE; + case XKB_KEY_Tab: return KEY_TAB; + case XKB_KEY_Return: return KEY_ENTER; + case XKB_KEY_Escape: return KEY_ESCAPE; + case XKB_KEY_space: return KEY_SPACE; + case XKB_KEY_exclam: return KEY_EXCLAMATION_MARK; + case XKB_KEY_quotedbl: return KEY_QUOTATION_MARK; + case XKB_KEY_numbersign: return KEY_HASH; + case XKB_KEY_dollar: return KEY_DOLLAR_SIGN; + case XKB_KEY_percent: return KEY_PERCENT_SIGN; + case XKB_KEY_ampersand: return KEY_AMPERSAND; + case XKB_KEY_apostrophe: return KEY_APOSTROPHE; + case XKB_KEY_parenleft: return KEY_LEFT_PARENTHESIS; + case XKB_KEY_parenright: return KEY_RIGHT_PARENTHESIS; + case XKB_KEY_asterisk: return KEY_ASTERISK; + case XKB_KEY_plus: return KEY_PLUS; + case XKB_KEY_comma: return KEY_COMMA; + case XKB_KEY_minus: return KEY_MINUS; + case XKB_KEY_period: return KEY_PERIOD; + case XKB_KEY_slash: return KEY_SLASH; + case XKB_KEY_colon: return KEY_COLON; + case XKB_KEY_semicolon: return KEY_SEMICOLON; + case XKB_KEY_less: return KEY_LESS_THAN; + case XKB_KEY_equal: return KEY_EQUALS; + case XKB_KEY_greater: return KEY_GREATER_THAN; + case XKB_KEY_question: return KEY_QUESTION_MARK; + case XKB_KEY_at: return KEY_AT_SIGN; + case XKB_KEY_bracketleft: return KEY_LEFT_BRACKET; + case XKB_KEY_backslash: return KEY_BACKSLASH; + case XKB_KEY_bracketright: return KEY_RIGHT_BRACKET; + case XKB_KEY_asciicircum: return KEY_CARET; + case XKB_KEY_underscore: return KEY_UNDERSCORE; + case XKB_KEY_grave: return KEY_GRAVE_ACCENT; + case XKB_KEY_braceleft: return KEY_LEFT_BRACE; + case XKB_KEY_bar: return KEY_PIPE; + case XKB_KEY_braceright: return KEY_RIGHT_BRACE; + case XKB_KEY_asciitilde: return KEY_TILDE; + case XKB_KEY_Delete: return KEY_DELETE; + case XKB_KEY_Up: return KEY_UP; + case XKB_KEY_Down: return KEY_DOWN; + case XKB_KEY_Left: return KEY_LEFT; + case XKB_KEY_Right: return KEY_RIGHT; + case XKB_KEY_F1: return KEY_F1; + case XKB_KEY_F2: return KEY_F2; + case XKB_KEY_F3: return KEY_F3; + case XKB_KEY_F4: return KEY_F4; + case XKB_KEY_F5: return KEY_F5; + case XKB_KEY_F6: return KEY_F6; + case XKB_KEY_F7: return KEY_F7; + case XKB_KEY_F8: return KEY_F8; + case XKB_KEY_F9: return KEY_F9; + case XKB_KEY_F10: return KEY_F10; + case XKB_KEY_F11: return KEY_F11; + case XKB_KEY_F12: return KEY_F12; + case XKB_KEY_Home: return KEY_HOME; + case XKB_KEY_End: return KEY_END; + case XKB_KEY_Page_Up: return KEY_PAGE_UP; + case XKB_KEY_Page_Down: return KEY_PAGE_DOWN; + case XKB_KEY_Insert: return KEY_INSERT; + case XKB_KEY_Caps_Lock: return KEY_CAPS_LOCK; + case XKB_KEY_Num_Lock: return KEY_NUM_LOCK; + case XKB_KEY_Scroll_Lock: return KEY_SCROLL_LOCK; + case XKB_KEY_Print: return KEY_PRINT_SCREEN; + case XKB_KEY_Pause: return KEY_PAUSE; + case XKB_KEY_Menu: return KEY_MENU; + case XKB_KEY_Shift_L: return KEY_LEFT_SHIFT; + case XKB_KEY_Shift_R: return KEY_RIGHT_SHIFT; + case XKB_KEY_Control_L: return KEY_LEFT_CTRL; + case XKB_KEY_Control_R: return KEY_RIGHT_CTRL; + case XKB_KEY_Alt_L: return KEY_LEFT_ALT; + case XKB_KEY_Alt_R: return KEY_RIGHT_ALT; + case XKB_KEY_Super_L: return KEY_LEFT_SUPER; + case XKB_KEY_Super_R: return KEY_RIGHT_SUPER; + case XKB_KEY_KP_0: return KEY_KP_0; + case XKB_KEY_KP_1: return KEY_KP_1; + case XKB_KEY_KP_2: return KEY_KP_2; + case XKB_KEY_KP_3: return KEY_KP_3; + case XKB_KEY_KP_4: return KEY_KP_4; + case XKB_KEY_KP_5: return KEY_KP_5; + case XKB_KEY_KP_6: return KEY_KP_6; + case XKB_KEY_KP_7: return KEY_KP_7; + case XKB_KEY_KP_8: return KEY_KP_8; + case XKB_KEY_KP_9: return KEY_KP_9; + case XKB_KEY_KP_Decimal: return KEY_KP_DECIMAL; + case XKB_KEY_KP_Divide: return KEY_KP_DIVIDE; + case XKB_KEY_KP_Multiply: return KEY_KP_MULTIPLY; + case XKB_KEY_KP_Subtract: return KEY_KP_SUBTRACT; + case XKB_KEY_KP_Add: return KEY_KP_ADD; + case XKB_KEY_KP_Enter: return KEY_KP_ENTER; + case XKB_KEY_KP_Equal: return KEY_KP_EQUALS; + default: return KEY_NONE; + } +} + +static void wm_base_ping(void *data, struct xdg_wm_base *wm_base, uint32_t serial) { + (void)data; + xdg_wm_base_pong(wm_base, serial); +} + +static const struct xdg_wm_base_listener wm_base_listener = { + .ping = wm_base_ping +}; + +static void toplevel_configure(void *data, struct xdg_toplevel *toplevel, int32_t width, int32_t height, struct wl_array *states) { + (void)data; + (void)toplevel; + (void)width; + (void)height; + (void)states; +} + +static void toplevel_close(void *data, struct xdg_toplevel *toplevel) { + wayland_state *state = data; + (void)toplevel; + state->running = 0; +} + +static const struct xdg_toplevel_listener toplevel_listener = { + .configure = toplevel_configure, + .close = toplevel_close +}; + +static void xdg_surface_configure(void *data, struct xdg_surface *xdg_surface, uint32_t serial) { + wayland_state *state = data; + xdg_surface_ack_configure(xdg_surface, serial); + state->configured = 1; +} + +static const struct xdg_surface_listener xdg_surface_listener = { + .configure = xdg_surface_configure +}; + +static int create_buffer(wayland_state *state) { + char path[] = "/tmp/tuxanci2-wayland-XXXXXX"; + int fd = mkstemp(path); + if (fd < 0) { + fprintf(stderr, "cannot create shared buffer: %s\n", strerror(errno)); + return 0; + } + unlink(path); + + state->buffer_size = (size_t)ta_fb.width * (size_t)ta_fb.height * 4; + if (ftruncate(fd, (off_t)state->buffer_size) < 0) { + fprintf(stderr, "cannot size shared buffer: %s\n", strerror(errno)); + close(fd); + return 0; + } + + state->pixels = mmap(NULL, state->buffer_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + if (state->pixels == MAP_FAILED) { + state->pixels = NULL; + fprintf(stderr, "cannot map shared buffer: %s\n", strerror(errno)); + close(fd); + return 0; + } + + struct wl_shm_pool *pool = wl_shm_create_pool(state->shm, fd, (int32_t)state->buffer_size); + close(fd); + if (!pool) { + fprintf(stderr, "cannot create Wayland shared-memory pool\n"); + return 0; + } + + state->buffer = wl_shm_pool_create_buffer(pool, 0, (int32_t)ta_fb.width, (int32_t)ta_fb.height, (int32_t)ta_fb.width * 4, WL_SHM_FORMAT_XRGB8888); + wl_shm_pool_destroy(pool); + if (!state->buffer) { + fprintf(stderr, "cannot create Wayland framebuffer\n"); + return 0; + } + return 1; +} + +static void buffer_release(void *data, struct wl_buffer *buffer) { + wayland_state *state = data; + (void)buffer; + state->buffer_released = 1; +} + +static const struct wl_buffer_listener buffer_listener = { + .release = buffer_release +}; + +static void frame_done(void *data, struct wl_callback *callback, uint32_t time) { + (void)time; + wayland_state *state = data; + wl_callback_destroy(callback); + state->frame_callback = NULL; + state->frame_ready = 1; +} + +static const struct wl_callback_listener frame_listener = { + .done = frame_done +}; + +static void keyboard_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format, int32_t fd, uint32_t size) { + (void)keyboard; + wayland_state *state = data; + + if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1 || size == 0) { + close(fd); + return; + } + + char *keymap_text = malloc((size_t)size + 1); + if (!keymap_text) { + close(fd); + return; + } + size_t offset = 0; + while (offset < size) { + ssize_t count = read(fd, keymap_text + offset, (size_t)size - offset); + if (count <= 0) { + break; + } + offset += (size_t)count; + } + close(fd); + if (offset != size) { + free(keymap_text); + return; + } + keymap_text[size] = '\0'; + + struct xkb_keymap *keymap = xkb_keymap_new_from_string(state->xkb_context, keymap_text, XKB_KEYMAP_FORMAT_TEXT_V1, XKB_KEYMAP_COMPILE_NO_FLAGS); + free(keymap_text); + if (!keymap) { + return; + } + + struct xkb_state *xkb_state = xkb_state_new(keymap); + if (!xkb_state) { + xkb_keymap_unref(keymap); + return; + } + + if (state->xkb_state) { + xkb_state_unref(state->xkb_state); + } + + if (state->xkb_keymap) { + xkb_keymap_unref(state->xkb_keymap); + } + + state->xkb_keymap = keymap; + state->xkb_state = xkb_state; +} + +static void keyboard_enter(void *data, struct wl_keyboard *keyboard, uint32_t serial, struct wl_surface *surface, struct wl_array *keys) { + (void)data; + (void)keyboard; + (void)serial; + (void)surface; + (void)keys; + ta_keyboard_clear(); +} + +static void keyboard_leave(void *data, struct wl_keyboard *keyboard, + uint32_t serial, struct wl_surface *surface) { + (void)data; + (void)keyboard; + (void)serial; + (void)surface; + ta_keyboard_clear(); +} + +static void keyboard_key(void *data, struct wl_keyboard *keyboard, uint32_t serial, uint32_t time, uint32_t key, uint32_t key_state) { + (void)keyboard; + (void)serial; + (void)time; + + wayland_state *state = data; + + if (!state->xkb_state) { + return; + } + + xkb_keysym_t symbol = xkb_state_key_get_one_sym(state->xkb_state, key + 8); + ta_keyboard_keys mapped = key_from_keysym(symbol); + if (key_state == WL_KEYBOARD_KEY_STATE_PRESSED) { + ta_keyboard_key_down(mapped); + if (mapped == KEY_ESCAPE) state->running = 0; + } else { + ta_keyboard_key_up(mapped); + } +} + +static void keyboard_modifiers(void *data, struct wl_keyboard *keyboard, uint32_t serial, uint32_t depressed, uint32_t latched, uint32_t locked, uint32_t group) { + (void)keyboard; + (void)serial; + wayland_state *state = data; + if (state->xkb_state && state->xkb_keymap) { + xkb_state_update_mask(state->xkb_state, depressed, latched, locked, 0, 0, group); + } +} + +static void keyboard_repeat_info(void *data, struct wl_keyboard *keyboard, int32_t rate, int32_t delay) { + (void)data; + (void)keyboard; + (void)rate; + (void)delay; +} + +static const struct wl_keyboard_listener keyboard_listener = { + .keymap = keyboard_keymap, + .enter = keyboard_enter, + .leave = keyboard_leave, + .key = keyboard_key, + .modifiers = keyboard_modifiers, + .repeat_info = keyboard_repeat_info +}; + +static void seat_capabilities(void *data, struct wl_seat *seat, uint32_t capabilities) { + wayland_state *state = data; + if ((capabilities & WL_SEAT_CAPABILITY_KEYBOARD) && !state->keyboard) { + state->keyboard = wl_seat_get_keyboard(seat); + wl_keyboard_add_listener(state->keyboard, &keyboard_listener, state); + } else if (!(capabilities & WL_SEAT_CAPABILITY_KEYBOARD) && state->keyboard) { + wl_keyboard_destroy(state->keyboard); + state->keyboard = NULL; + ta_keyboard_clear(); + } +} + +static void seat_name(void *data, struct wl_seat *seat, const char *name) { + (void)data; + (void)seat; + (void)name; +} + +static const struct wl_seat_listener seat_listener = { + .capabilities = seat_capabilities, + .name = seat_name +}; + +static void registry_global(void *data, struct wl_registry *registry, uint32_t name, const char *interface, uint32_t version) { + wayland_state *state = data; + if (strcmp(interface, wl_compositor_interface.name) == 0) { + state->compositor = wl_registry_bind(registry, name, &wl_compositor_interface, version < 4 ? version : 4); + } else if (strcmp(interface, wl_shm_interface.name) == 0) { + state->shm = wl_registry_bind(registry, name, &wl_shm_interface, 1); + } else if (strcmp(interface, xdg_wm_base_interface.name) == 0) { + state->wm_base = wl_registry_bind(registry, name, &xdg_wm_base_interface, version < 1 ? version : 1); + xdg_wm_base_add_listener(state->wm_base, &wm_base_listener, state); + } else if (strcmp(interface, wl_seat_interface.name) == 0) { + state->seat = wl_registry_bind(registry, name, &wl_seat_interface, version < 5 ? version : 5); + wl_seat_add_listener(state->seat, &seat_listener, state); + } +} + +static void registry_remove(void *data, struct wl_registry *registry, uint32_t name) { + (void)data; + (void)registry; + (void)name; +} + +static const struct wl_registry_listener registry_listener = { + .global = registry_global, + .global_remove = registry_remove +}; + +static void destroy_wayland(wayland_state *state) { + if (state->frame_callback) { + wl_callback_destroy(state->frame_callback); + } + + if (state->buffer) { + wl_buffer_destroy(state->buffer); + } + + if (state->pixels) { + munmap(state->pixels, state->buffer_size); + } + + if (state->toplevel) { + xdg_toplevel_destroy(state->toplevel); + } + + if (state->xdg_surface) { + xdg_surface_destroy(state->xdg_surface); + } + + if (state->surface) { + wl_surface_destroy(state->surface); + } + + if (state->keyboard) { + wl_keyboard_destroy(state->keyboard); + } + + if (state->seat) { + wl_seat_destroy(state->seat); + } + + if (state->wm_base) { + xdg_wm_base_destroy(state->wm_base); + } + + if (state->shm) { + wl_shm_destroy(state->shm); + } + + if (state->compositor) { + wl_compositor_destroy(state->compositor); + } + + if (state->registry) { + wl_registry_destroy(state->registry); + } + + if (state->xkb_state) { + xkb_state_unref(state->xkb_state); + } + + if (state->xkb_keymap) { + xkb_keymap_unref(state->xkb_keymap); + } + + if (state->xkb_context) { + xkb_context_unref(state->xkb_context); + } + + if (state->display) { + wl_display_flush(state->display); + wl_display_disconnect(state->display); + } +} + +int main(void) { + wayland_state state; + memset(&state, 0, sizeof(state)); + state.running = 1; + state.frame_ready = 1; + state.buffer_released = 1; + + state.display = wl_display_connect(NULL); + if (!state.display) { + fprintf(stderr, "cannot connect to the Wayland display\n"); + return 1; + } + state.xkb_context = xkb_context_new(XKB_CONTEXT_NO_FLAGS); + state.registry = wl_display_get_registry(state.display); + wl_registry_add_listener(state.registry, &registry_listener, &state); + if (wl_display_roundtrip(state.display) < 0 || !state.compositor || + !state.shm || !state.wm_base) { + fprintf(stderr, "compositor is missing required Wayland globals\n"); + destroy_wayland(&state); + return 1; + } + + ta_fb.width = WINDOW_WIDTH; + ta_fb.height = WINDOW_HEIGHT; + ta_fb.bpp = WINDOW_BPP; + ta_main_init(); + + state.surface = wl_compositor_create_surface(state.compositor); + state.xdg_surface = xdg_wm_base_get_xdg_surface(state.wm_base, state.surface); + xdg_surface_add_listener(state.xdg_surface, &xdg_surface_listener, &state); + state.toplevel = xdg_surface_get_toplevel(state.xdg_surface); + xdg_toplevel_add_listener(state.toplevel, &toplevel_listener, &state); + xdg_toplevel_set_title(state.toplevel, "Tuxanci2"); + xdg_toplevel_set_app_id(state.toplevel, "tuxanci2"); + xdg_toplevel_set_min_size(state.toplevel, WINDOW_WIDTH, WINDOW_HEIGHT); + xdg_toplevel_set_max_size(state.toplevel, WINDOW_WIDTH, WINDOW_HEIGHT); + wl_surface_commit(state.surface); + + while (state.running && !state.configured) { + if (wl_display_dispatch(state.display) < 0) state.running = 0; + } + if (!state.running || !create_buffer(&state)) { + destroy_wayland(&state); + return state.running ? 1 : 0; + } + wl_buffer_add_listener(state.buffer, &buffer_listener, &state); + + while (state.running) { + while (state.running && (!state.frame_ready || !state.buffer_released)) { + if (wl_display_dispatch(state.display) < 0) state.running = 0; + } + if (!state.running) break; + + state.frame_ready = 0; + state.buffer_released = 0; + ta_main_update(); + memcpy(state.pixels, ta_fb.buffers[0], state.buffer_size); + + state.frame_callback = wl_surface_frame(state.surface); + wl_callback_add_listener(state.frame_callback, &frame_listener, &state); + wl_surface_attach(state.surface, state.buffer, 0, 0); + wl_surface_damage(state.surface, 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT); + wl_surface_commit(state.surface); + if (wl_display_flush(state.display) < 0 && errno != EAGAIN) { + state.running = 0; + } + } + + destroy_wayland(&state); + return 0; +} + +void *ta_malloc(ta_size size) { + return malloc(size); +} + +void ta_free(void *ptr) { + free(ptr); +} diff --git a/platforms/x11/.gitignore b/platforms/x11/.gitignore @@ -0,0 +1 @@ +tuxanci2 +\ No newline at end of file diff --git a/platforms/x11/Makefile b/platforms/x11/Makefile @@ -0,0 +1,33 @@ +MAKEFILE_DIR := $(dir $(abspath $(lastword $(MAKEFILE_LIST)))) +ROOT_DIR := $(abspath $(MAKEFILE_DIR)/../..) + +X11_CC ?= cc +X11_CFLAGS ?= -O2 +X11_LDFLAGS ?= +X11_LDLIBS ?= -lX11 +X11_BUILD ?= build/x11 + +X11_SOURCE := $(MAKEFILE_DIR)x11.c +X11_BUILD_DIR := $(ROOT_DIR)/$(X11_BUILD) +X11_LIBRARY := $(X11_BUILD_DIR)/tuxanci2.a +TARGET := $(MAKEFILE_DIR)tuxanci2 +ROOT_INPUTS := $(wildcard $(ROOT_DIR)/src/*.c) \ + $(wildcard $(ROOT_DIR)/include/*.h) \ + $(ROOT_DIR)/include/ta_config.h.in \ + $(shell find $(ROOT_DIR)/files -type f) \ + $(ROOT_DIR)/Makefile + +.DEFAULT_GOAL := all +.PHONY: all clean + +all: $(TARGET) + +$(X11_LIBRARY): $(ROOT_INPUTS) + $(MAKE) -C "$(ROOT_DIR)" CC="$(X11_CC)" BUILD_DIR="$(X11_BUILD)" all + +$(TARGET): $(X11_SOURCE) $(X11_LIBRARY) + $(X11_CC) $(X11_CFLAGS) -I"$(ROOT_DIR)/include" -I"$(X11_BUILD_DIR)" "$(X11_SOURCE)" "$(X11_LIBRARY)" $(X11_LDFLAGS) $(X11_LDLIBS) -o "$@" + +clean: + $(RM) "$(TARGET)" + $(MAKE) -C "$(ROOT_DIR)" BUILD_DIR="$(X11_BUILD)" clean diff --git a/platforms/x11/x11.c b/platforms/x11/x11.c @@ -0,0 +1,193 @@ +#include "ta_api.h" +#include <unistd.h> +#include <stdlib.h> +#include <X11/Xlib.h> +#include <X11/Xutil.h> +#include <X11/keysym.h> + +#define WINDOW_WIDTH 800 +#define WINDOW_HEIGHT 600 +#define WINDOW_BPP 32 +#define TARGET_FPS 60 +#define FRAME_TIME (1000000 / TARGET_FPS) + +static ta_keyboard_keys key_from_keysym(KeySym symbol) { + if (symbol >= XK_a && symbol <= XK_z) { + return (ta_keyboard_keys)(KEY_A + symbol - XK_a); + } + if (symbol >= XK_A && symbol <= XK_Z) { + return (ta_keyboard_keys)(KEY_A + symbol - XK_A); + } + if (symbol >= XK_0 && symbol <= XK_9) { + return (ta_keyboard_keys)(KEY_0 + symbol - XK_0); + } + + switch (symbol) { + case XK_BackSpace: return KEY_BACKSPACE; + case XK_Tab: return KEY_TAB; + case XK_Return: return KEY_ENTER; + case XK_Escape: return KEY_ESCAPE; + case XK_space: return KEY_SPACE; + case XK_exclam: return KEY_EXCLAMATION_MARK; + case XK_quotedbl: return KEY_QUOTATION_MARK; + case XK_numbersign: return KEY_HASH; + case XK_dollar: return KEY_DOLLAR_SIGN; + case XK_percent: return KEY_PERCENT_SIGN; + case XK_ampersand: return KEY_AMPERSAND; + case XK_apostrophe: return KEY_APOSTROPHE; + case XK_parenleft: return KEY_LEFT_PARENTHESIS; + case XK_parenright: return KEY_RIGHT_PARENTHESIS; + case XK_asterisk: return KEY_ASTERISK; + case XK_plus: return KEY_PLUS; + case XK_comma: return KEY_COMMA; + case XK_minus: return KEY_MINUS; + case XK_period: return KEY_PERIOD; + case XK_slash: return KEY_SLASH; + case XK_colon: return KEY_COLON; + case XK_semicolon: return KEY_SEMICOLON; + case XK_less: return KEY_LESS_THAN; + case XK_equal: return KEY_EQUALS; + case XK_greater: return KEY_GREATER_THAN; + case XK_question: return KEY_QUESTION_MARK; + case XK_at: return KEY_AT_SIGN; + case XK_bracketleft: return KEY_LEFT_BRACKET; + case XK_backslash: return KEY_BACKSLASH; + case XK_bracketright: return KEY_RIGHT_BRACKET; + case XK_asciicircum: return KEY_CARET; + case XK_underscore: return KEY_UNDERSCORE; + case XK_grave: return KEY_GRAVE_ACCENT; + case XK_braceleft: return KEY_LEFT_BRACE; + case XK_bar: return KEY_PIPE; + case XK_braceright: return KEY_RIGHT_BRACE; + case XK_asciitilde: return KEY_TILDE; + case XK_Delete: return KEY_DELETE; + case XK_Up: return KEY_UP; + case XK_Down: return KEY_DOWN; + case XK_Left: return KEY_LEFT; + case XK_Right: return KEY_RIGHT; + case XK_F1: return KEY_F1; + case XK_F2: return KEY_F2; + case XK_F3: return KEY_F3; + case XK_F4: return KEY_F4; + case XK_F5: return KEY_F5; + case XK_F6: return KEY_F6; + case XK_F7: return KEY_F7; + case XK_F8: return KEY_F8; + case XK_F9: return KEY_F9; + case XK_F10: return KEY_F10; + case XK_F11: return KEY_F11; + case XK_F12: return KEY_F12; + case XK_Home: return KEY_HOME; + case XK_End: return KEY_END; + case XK_Page_Up: return KEY_PAGE_UP; + case XK_Page_Down: return KEY_PAGE_DOWN; + case XK_Insert: return KEY_INSERT; + case XK_Caps_Lock: return KEY_CAPS_LOCK; + case XK_Num_Lock: return KEY_NUM_LOCK; + case XK_Scroll_Lock: return KEY_SCROLL_LOCK; + case XK_Print: return KEY_PRINT_SCREEN; + case XK_Pause: return KEY_PAUSE; + case XK_Menu: return KEY_MENU; + case XK_Shift_L: return KEY_LEFT_SHIFT; + case XK_Shift_R: return KEY_RIGHT_SHIFT; + case XK_Control_L: return KEY_LEFT_CTRL; + case XK_Control_R: return KEY_RIGHT_CTRL; + case XK_Alt_L: return KEY_LEFT_ALT; + case XK_Alt_R: return KEY_RIGHT_ALT; + case XK_Super_L: return KEY_LEFT_SUPER; + case XK_Super_R: return KEY_RIGHT_SUPER; + case XK_KP_0: return KEY_KP_0; + case XK_KP_1: return KEY_KP_1; + case XK_KP_2: return KEY_KP_2; + case XK_KP_3: return KEY_KP_3; + case XK_KP_4: return KEY_KP_4; + case XK_KP_5: return KEY_KP_5; + case XK_KP_6: return KEY_KP_6; + case XK_KP_7: return KEY_KP_7; + case XK_KP_8: return KEY_KP_8; + case XK_KP_9: return KEY_KP_9; + case XK_KP_Decimal: return KEY_KP_DECIMAL; + case XK_KP_Divide: return KEY_KP_DIVIDE; + case XK_KP_Multiply: return KEY_KP_MULTIPLY; + case XK_KP_Subtract: return KEY_KP_SUBTRACT; + case XK_KP_Add: return KEY_KP_ADD; + case XK_KP_Enter: return KEY_KP_ENTER; + case XK_KP_Equal: return KEY_KP_EQUALS; + default: return KEY_NONE; + } +} + +int main(void) { + Display *display = XOpenDisplay(NULL); + if (!display) { + return 1; + } + + int screen = DefaultScreen(display); + Window root = RootWindow(display, screen); + + ta_fb.width = WINDOW_WIDTH; + ta_fb.height = WINDOW_HEIGHT; + ta_fb.bpp = WINDOW_BPP; + + ta_main_init(); + + Window win = XCreateSimpleWindow(display, root, 0, 0, ta_fb.width, ta_fb.height, 0, BlackPixel(display, screen), WhitePixel(display, screen)); + + XSizeHints *hints = XAllocSizeHints(); + if (hints) { + hints->flags = PMinSize | PMaxSize; + hints->min_width = WINDOW_WIDTH; + hints->min_height = WINDOW_HEIGHT; + hints->max_width = WINDOW_WIDTH; + hints->max_height = WINDOW_HEIGHT; + + XSetWMNormalHints(display, win, hints); + + XFree(hints); + } + + XSelectInput(display, win, ExposureMask | KeyPressMask | KeyReleaseMask | FocusChangeMask); + XMapWindow(display, win); + + Visual *visual = DefaultVisual(display, screen); + int depth = DefaultDepth(display, screen); + + XImage *ximage = XCreateImage(display, visual, depth, ZPixmap, 0, (char *)ta_fb.buffers[0], ta_fb.width, ta_fb.height, ta_fb.bpp, 0); + + GC gc = DefaultGC(display, screen); + + while (1) { + while (XPending(display) > 0) { + XEvent event; + XNextEvent(display, &event); + if (event.type == KeyPress) { + ta_keyboard_key_down(key_from_keysym(XLookupKeysym(&event.xkey, 0))); + } else if (event.type == KeyRelease) { + ta_keyboard_key_up(key_from_keysym(XLookupKeysym(&event.xkey, 0))); + } else if (event.type == FocusOut) { + ta_keyboard_clear(); + } + } + + ta_main_update(); + + ximage->data = (char *)ta_fb.buffers[0]; + XPutImage(display, win, gc, ximage, 0, 0, 0, 0, ta_fb.width, ta_fb.height); + XFlush(display); + + usleep(FRAME_TIME); + } + + XDestroyWindow(display, win); + XCloseDisplay(display); + return 0; +} + +void *ta_malloc(ta_size size) { + return malloc(size); +} + +void ta_free(void *ptr) { + free(ptr); +} +\ No newline at end of file diff --git a/src/ta_button.c b/src/ta_button.c @@ -0,0 +1,162 @@ +#include "ta_api.h" + +#define TA_BUTTON_ANIMATION_FRAMES 8 +#define TA_BUTTON_FONT_SIZE 32 +#define TA_BUTTON_SPACING 48 +#define TA_BUTTON_COLOR_IDLE 118 +#define TA_BUTTON_COLOR_SELECTED 255 + +void ta_button_lists_init(ta_button_lists *list) { + if (list == 0) { + return; + } + + list->buttons = 0; + list->size = 0; + list->selected = 0; + list->animation_from = 0; + list->animation_frame = 0; + list->animation_active = ta_false; + list->up_was_down = ta_false; + list->down_was_down = ta_false; + list->enter_was_down = ta_false; +} + +ta_bool ta_button_lists_add(ta_button_lists *list, const char *label, ta_button_callback callback) { + if (list == 0 || label == 0 || list->size == (ta_size)-1 || list->size + 1 > (ta_size)-1 / sizeof(ta_button)) { + return ta_false; + } + + ta_size new_size = list->size + 1; + ta_button *buttons = (ta_button *)ta_malloc(new_size * sizeof(ta_button)); + + if (buttons == 0) { + return ta_false; + } + + for (ta_size i = 0; i < list->size; i++) { + buttons[i] = list->buttons[i]; + } + + buttons[list->size].label = label; + buttons[list->size].callback = callback; + + ta_free(list->buttons); + + list->buttons = buttons; + list->size = new_size; + + return ta_true; +} + +void ta_button_lists_update(ta_button_lists *list) { + if (list == 0 || list->size == 0) { + return; + } + + ta_bool up_down = ta_keyboard_is_down(KEY_UP); + ta_bool down_down = ta_keyboard_is_down(KEY_DOWN); + ta_bool enter_down = ta_keyboard_is_down(KEY_ENTER); + ta_bool up_pressed = up_down && !list->up_was_down; + ta_bool down_pressed = down_down && !list->down_was_down; + ta_bool enter_pressed = enter_down && !list->enter_was_down; + list->up_was_down = up_down; + list->down_was_down = down_down; + list->enter_was_down = enter_down; + + if (list->animation_active) { + list->animation_frame++; + + if (list->animation_frame >= TA_BUTTON_ANIMATION_FRAMES) { + list->animation_frame = TA_BUTTON_ANIMATION_FRAMES; + list->animation_active = ta_false; + } + + if (enter_pressed && list->buttons[list->selected].callback != 0) { + list->buttons[list->selected].callback(); + } + + return; + } + + if (down_pressed || up_pressed) { + list->animation_from = list->selected; + if (down_pressed) { + list->selected = (list->selected + 1) % list->size; + } else { + list->selected = list->selected == 0 ? list->size - 1 : list->selected - 1; + } + if (list->selected != list->animation_from) { + list->animation_frame = 0; + list->animation_active = ta_true; + } + } + + if (enter_pressed && list->buttons[list->selected].callback != 0) { + list->buttons[list->selected].callback(); + } +} + +void ta_button_lists_draw(const ta_button_lists *list) { + if (list == 0 || list->size == 0) { + return; + } + + double width_scale = (double)ta_fb.width / 800.0; + double height_scale = (double)ta_fb.height / 600.0; + double scale = width_scale < height_scale ? width_scale : height_scale; + ta_uint32 font_size = (ta_uint32)(TA_BUTTON_FONT_SIZE * scale + 0.5); + ta_int32 spacing = (ta_int32)(TA_BUTTON_SPACING * scale + 0.5); + + if (font_size == 0) { + font_size = 1; + } + + if (spacing == 0) { + spacing = 1; + } + + double progress = 1.0; + + if (list->animation_active) { + progress = (double)list->animation_frame / TA_BUTTON_ANIMATION_FRAMES; + progress = progress * progress * (3.0 - 2.0 * progress); + } + + ta_int32 center_y = (ta_int32)(ta_fb.height / 2); + ta_int32 selected_shift = (ta_int32)list->selected - (ta_int32)list->animation_from; + + for (ta_size i = 0; i < list->size; i++) { + ta_uint32 text_width = 0; + ta_uint32 text_height = 0; + + ta_text_measure(list->buttons[i].label, font_size, &text_width, &text_height); + + ta_int32 row_from = (ta_int32)i - (ta_int32)list->animation_from; + ta_int32 y = center_y + (ta_int32)(row_from * spacing - selected_shift * spacing * progress) - (ta_int32)(text_height / 2); + + if (y < 0 || (ta_size)y >= ta_fb.height || text_width > ta_fb.width) { + continue; + } + + double selection = i == list->animation_from ? 1.0 - progress : 0.0; + + if (i == list->selected) { + selection += progress; + } + + ta_uint8 color = (ta_uint8)(TA_BUTTON_COLOR_IDLE + (TA_BUTTON_COLOR_SELECTED - TA_BUTTON_COLOR_IDLE) * selection + 0.5); + ta_uint32 x = (ta_uint32)(ta_fb.width - text_width) / 2; + + ta_text_draw_outline(list->buttons[i].label, x, (ta_uint32)y, text_width, text_height, color, color, color, 1, 0, 0, 0); + } +} + +void ta_button_lists_destroy(ta_button_lists *list) { + if (list == 0) { + return; + } + + ta_free(list->buttons); + ta_button_lists_init(list); +} +\ No newline at end of file diff --git a/src/ta_fb.c b/src/ta_fb.c @@ -0,0 +1,53 @@ +#include "ta_api.h" + +ta_fbs ta_fb; + +ta_size ta_fb_init(ta_fbs *fb) { + if (fb == 0 || fb->width == 0 || fb->height == 0 || fb->bpp == 0 || fb->bpp % 8 != 0) { + return 0; + } + + ta_size bytes_per_pixel = fb->bpp / 8; + ta_size max_size = (ta_size)-1; + if (fb->width > max_size / bytes_per_pixel || fb->height > max_size / (fb->width * bytes_per_pixel)) { + return 0; + } + + ta_size size = fb->width * fb->height * bytes_per_pixel; + void *first = ta_malloc(size); + if (first == 0) { + return 0; + } + + void *second = ta_malloc(size); + if (second == 0) { + ta_free(first); + return 0; + } + + fb->size = size; + fb->buffers[0] = first; + fb->buffers[1] = second; + return size; +} + +void ta_fb_swap(ta_fbs *fb) { + void *temp = fb->buffers[0]; + fb->buffers[0] = fb->buffers[1]; + fb->buffers[1] = temp; +} + +void ta_fb_clear(ta_fbs *fb) { + ta_size *dest = (ta_size *)fb->buffers[1]; + ta_size num_words = fb->size / sizeof(ta_size); + + for (ta_size i = 0; i < num_words; i++) { + dest[i] = 0; + } + + unsigned char *byte_dest = (unsigned char *)fb->buffers[1]; + ta_size handled_bytes = num_words * sizeof(ta_size); + for (ta_size i = handled_bytes; i < fb->size; i++) { + byte_dest[i] = 0; + } +} diff --git a/src/ta_image.c b/src/ta_image.c @@ -0,0 +1,309 @@ +#include "ta_api.h" + +#define TAIF_FLAG_8BPP 0x01 +#define TAIF_FLAG_TRANSPARENT 0x02 +#define TAIF_FLAG_ANIMATED 0x04 +#define TAIF_WINDOW_SIZE 4096 + +typedef struct { + const ta_uint8 *data; + ta_uint32 length; + ta_uint32 width; + ta_uint32 height; + ta_uint32 stride; + ta_uint32 frame_size; + ta_uint16 frame_count; + ta_uint16 requested_frame; + ta_uint16 palette_entries; + ta_uint8 flags; + ta_uint8 palette[256][3]; + ta_uint8 *pixels; +} ta_image_cache; + +static ta_image_cache cached_image; + +static ta_uint16 read_le16(const ta_uint8 *data) { + return (ta_uint16)((ta_uint16)data[0] | ((ta_uint16)data[1] << 8)); +} + +static ta_bool decode_image(const ta_uint8 *data, ta_uint32 length, ta_uint16 requested_frame, ta_image_cache *image) { + if (data == 0 || length < 11 || data[0] != 'T' || data[1] != 'A' || data[2] != 'I' || data[3] != 'F' || data[4] != 1) { + return ta_false; + } + + ta_uint8 flags = data[5]; + if ((flags & 0xf8) != 0) { + return ta_false; + } + + ta_uint32 width = read_le16(data + 6); + ta_uint32 height = read_le16(data + 8); + ta_uint32 palette_entries = (ta_uint32)data[10] + 1; + ta_bool eight_bpp = (flags & TAIF_FLAG_8BPP) != 0; + ta_bool transparent = (flags & TAIF_FLAG_TRANSPARENT) != 0; + ta_uint32 max_entries = eight_bpp ? (transparent ? 255 : 256) : (transparent ? 15 : 16); + ta_uint32 header_size = 11; + ta_uint16 frame_count = 1; + + if (width == 0 || height == 0 || palette_entries > max_entries) { + return ta_false; + } + + if ((flags & TAIF_FLAG_ANIMATED) != 0) { + if (length < 14 || data[11] == 0) { + return ta_false; + } + + frame_count = read_le16(data + 12); + if (frame_count < 2) { + return ta_false; + } + + header_size = 14; + } + + if (requested_frame >= frame_count) { + return ta_false; + } + + ta_uint64 stride = eight_bpp ? width : (width + 1) / 2; + ta_uint64 frame_size = stride * height; + ta_uint64 max_size = (ta_uint64)(ta_size)-1; + ta_uint64 palette_size = (ta_uint64)palette_entries * 3; + + if (frame_size == 0 || frame_size > max_size || header_size + palette_size > length) { + return ta_false; + } + + const ta_uint8 *palette = data + header_size; + const ta_uint8 *stream = palette + palette_size; + ta_uint32 stream_length = length - (ta_uint32)(header_size + palette_size); + ta_uint8 *previous = (ta_uint8 *)ta_malloc((ta_size)frame_size); + ta_uint8 *current = (ta_uint8 *)ta_malloc((ta_size)frame_size); + ta_uint8 *decoded_frame = (ta_uint8 *)ta_malloc((ta_size)frame_size); + + if (previous == 0 || current == 0 || decoded_frame == 0) { + ta_free(previous); + ta_free(current); + ta_free(decoded_frame); + return ta_false; + } + + for (ta_uint32 i = 0; i < (ta_uint32)frame_size; i++) { + previous[i] = 0; + } + + ta_uint8 window[TAIF_WINDOW_SIZE]; + ta_uint32 stream_position = 0; + ta_uint32 window_position = 0; + ta_uint64 produced = 0; + ta_uint64 total_size = frame_size * frame_count; + ta_bool valid = ta_true; + ta_uint32 frame_position = 0; + ta_uint32 frame_index = 0; + ta_uint32 match_distance = 0; + ta_uint32 match_remaining = 0; + ta_uint8 item_flags = 0; + ta_uint8 flags_remaining = 0; + + while (produced < total_size && valid) { + ta_uint8 value; + if (match_remaining != 0) { + ta_uint32 source_position = (window_position + TAIF_WINDOW_SIZE - match_distance) & (TAIF_WINDOW_SIZE - 1); + value = window[source_position]; + match_remaining--; + } else { + if (flags_remaining == 0) { + if (stream_position >= stream_length) { + valid = ta_false; + break; + } + + item_flags = stream[stream_position++]; + flags_remaining = 8; + } + + ta_bool literal = (item_flags & 1) != 0; + item_flags >>= 1; + flags_remaining--; + + if (literal) { + if (stream_position >= stream_length) { + valid = ta_false; + + break; + } + + value = stream[stream_position++]; + } else { + if (stream_length - stream_position < 2) { + valid = ta_false; + break; + } + + ta_uint8 b0 = stream[stream_position++]; + ta_uint8 b1 = stream[stream_position++]; + match_distance = ((((ta_uint32)b1 >> 4) << 8) | b0) + 1; + match_remaining = (b1 & 0x0f) + 3; + + if (match_distance > produced) { + valid = ta_false; + break; + } + + continue; + } + } + + current[frame_position++] = value; + window[window_position] = value; + window_position = (window_position + 1) & (TAIF_WINDOW_SIZE - 1); + produced++; + + if (frame_position == (ta_uint32)frame_size) { + for (ta_uint32 i = 0; i < (ta_uint32)frame_size; i++) { + if ((flags & TAIF_FLAG_ANIMATED) != 0) { + current[i] ^= previous[i]; + } + } + + for (ta_uint32 row = 0; row < height && valid; row++) { + for (ta_uint32 column = 0; column < width; column++) { + ta_uint8 index; + + if (eight_bpp) { + index = current[row * (ta_uint32)stride + column]; + } else { + ta_uint8 packed = current[row * (ta_uint32)stride + column / 2]; + index = (column & 1) == 0 ? packed >> 4 : packed & 0x0f; + } + + if ((transparent && index != 0 && index > palette_entries) || (!transparent && index >= palette_entries)) { + valid = ta_false; + + break; + } + } + } + + if (!valid) { + break; + } + + if (frame_index == requested_frame) { + for (ta_uint32 i = 0; i < (ta_uint32)frame_size; i++) { + decoded_frame[i] = current[i]; + } + } + + ta_uint8 *swap = previous; + previous = current; + current = swap; + frame_index++; + frame_position = 0; + } + } + + ta_free(previous); + ta_free(current); + + if (!valid || produced != total_size) { + ta_free(decoded_frame); + + return ta_false; + } + + image->data = data; + image->length = length; + image->width = width; + image->height = height; + image->stride = (ta_uint32)stride; + image->frame_size = (ta_uint32)frame_size; + image->frame_count = frame_count; + image->requested_frame = requested_frame; + image->palette_entries = (ta_uint16)palette_entries; + image->flags = flags; + image->pixels = decoded_frame; + + for (ta_uint32 i = 0; i < palette_entries; i++) { + for (ta_uint32 channel = 0; channel < 3; channel++) { + image->palette[i][channel] = palette[i * 3 + channel]; + } + } + + return ta_true; +} + +static ta_bool ensure_cached_image(const ta_uint8 *data, ta_uint32 length, ta_uint16 frame) { + if (cached_image.data == data && cached_image.length == length && cached_image.requested_frame == frame && cached_image.pixels != 0) { + return ta_true; + } + + ta_image_cache image = {0}; + + if (!decode_image(data, length, frame, &image)) { + return ta_false; + } + + ta_free(cached_image.pixels); + cached_image = image; + + return ta_true; +} + +ta_bool ta_image_draw(const ta_uint8 *data, ta_uint32 length, ta_uint32 x, ta_uint32 y, ta_uint32 width, ta_uint32 height, ta_uint16 frame) { + if (data == 0 || length == 0 || width == 0 || height == 0 || ta_fb.buffers[1] == 0 || (ta_fb.bpp != 8 && ta_fb.bpp != 32) || width > ta_fb.width || height > ta_fb.height || !ensure_cached_image(data, length, frame)) { + return ta_false; + } + + for (ta_uint32 image_y = 0; image_y < height; image_y++) { + ta_uint64 screen_y = (ta_uint64)y + image_y; + if (screen_y >= ta_fb.height) { + continue; + } + + ta_uint32 source_y = (ta_uint32)((ta_uint64)image_y * cached_image.height / height); + for (ta_uint32 image_x = 0; image_x < width; image_x++) { + ta_uint64 screen_x = (ta_uint64)x + image_x; + if (screen_x >= ta_fb.width) { + continue; + } + + ta_uint32 source_x = (ta_uint32)((ta_uint64)image_x * cached_image.width / width); + ta_uint8 index; + + if ((cached_image.flags & TAIF_FLAG_8BPP) != 0) { + index = cached_image.pixels[source_y * cached_image.stride + source_x]; + } else { + ta_uint8 packed = cached_image.pixels[source_y * cached_image.stride + source_x / 2]; + index = (source_x & 1) == 0 ? packed >> 4 : packed & 0x0f; + } + + if ((cached_image.flags & TAIF_FLAG_TRANSPARENT) != 0 && index == 0) { + continue; + } + + ta_uint32 palette_index = index - ((cached_image.flags & TAIF_FLAG_TRANSPARENT) != 0 ? 1 : 0); + ta_uint32 red = cached_image.palette[palette_index][0]; + ta_uint32 green = cached_image.palette[palette_index][1]; + ta_uint32 blue = cached_image.palette[palette_index][2]; + ta_size destination_index = (ta_size)screen_y * ta_fb.width + (ta_size)screen_x; + + if (ta_fb.bpp == 32) { + ta_uint32 *pixels = (ta_uint32 *)ta_fb.buffers[1]; + ta_uint32 destination = pixels[destination_index]; + pixels[destination_index] = (destination & 0xff000000u) | (red << 16) | (green << 8) | blue; + } else { + ta_uint8 *pixels = (ta_uint8 *)ta_fb.buffers[1]; + pixels[destination_index] = (ta_uint8)(((red >> 5) << 5) | ((green >> 5) << 2) | (blue >> 6)); + } + } + } + + return ta_true; +} + +void ta_image_release(void) { + ta_free(cached_image.pixels); + cached_image = (ta_image_cache){0}; +} diff --git a/src/ta_keyboard.c b/src/ta_keyboard.c @@ -0,0 +1,47 @@ +#include "ta_keyboard.h" + +ta_keyboards ta_keyboard; + +ta_bool ta_keyboard_is_down(ta_keyboard_keys key) { + if (key == KEY_NONE) { + return ta_false; + } + + for (ta_size i = 0; i < TA_KEYBOARD_MAX_KEYS_DOWN; i++) { + if (ta_keyboard.keys_down[i] == (ta_uint8)key) { + return ta_true; + } + } + + return ta_false; +} + +void ta_keyboard_key_down(ta_keyboard_keys key) { + if (key == KEY_NONE || ta_keyboard_is_down(key)) { + return; + } + + for (ta_size i = 0; i < TA_KEYBOARD_MAX_KEYS_DOWN; i++) { + if (ta_keyboard.keys_down[i] == KEY_NONE) { + ta_keyboard.keys_down[i] = (ta_uint8)key; + + return; + } + } +} + +void ta_keyboard_key_up(ta_keyboard_keys key) { + for (ta_size i = 0; i < TA_KEYBOARD_MAX_KEYS_DOWN; i++) { + if (ta_keyboard.keys_down[i] == (ta_uint8)key) { + ta_keyboard.keys_down[i] = KEY_NONE; + + return; + } + } +} + +void ta_keyboard_clear(void) { + for (ta_size i = 0; i < TA_KEYBOARD_MAX_KEYS_DOWN; i++) { + ta_keyboard.keys_down[i] = KEY_NONE; + } +} diff --git a/src/ta_main.c b/src/ta_main.c @@ -0,0 +1,24 @@ +#include "ta_api.h" +#include "ta_files.h" +#include <stdlib.h> + +static ta_button_lists menu; + +static void quit(void) { + exit(0); +} + +void ta_main_init(void) { + ta_fb_init(&ta_fb); + ta_button_lists_init(&menu); + ta_button_lists_add(&menu, "Play", 0); + ta_button_lists_add(&menu, "Quit", quit); +} + +void ta_main_update(void) { + ta_fb_clear(&ta_fb); + ta_image_draw(ta_file_bg, ta_file_bg_len, 0, 0, ta_fb.width, ta_fb.height, 0); + ta_button_lists_update(&menu); + ta_button_lists_draw(&menu); + ta_fb_swap(&ta_fb); +} diff --git a/src/ta_text.c b/src/ta_text.c @@ -0,0 +1,1023 @@ +#include "ta_api.h" +#include "ta_files.h" + +#define ONE 65536 +#define MAX_COORD 16384 +#define MAX_SIZE 2048 +#define MAX_SEGMENTS 64 + +typedef struct { + ta_int32 x; + ta_int32 y; +} ta_text_pt; + +typedef struct { + ta_int32 clip_x0; + ta_int32 clip_y0; + ta_int32 clip_x1; + ta_int32 clip_y1; + ta_int32 scale; + ta_uint8 red; + ta_uint8 green; + ta_uint8 blue; + ta_uint8 border_thickness; + ta_uint8 border_red; + ta_uint8 border_green; + ta_uint8 border_blue; +} ta_text_ctx; + +typedef struct { + const ta_uint8 *glyf; + const ta_uint8 *loca; + const ta_uint8 *hmtx; + const ta_uint8 *cmap; + ta_uint32 glyf_length; + ta_uint32 cmap_length; + ta_uint32 units_per_em; + ta_uint32 horizontal_metric_count; + ta_uint32 glyph_count; + ta_bool long_loca; + ta_int32 ascent; + ta_int32 descent; + ta_int32 line_gap; +} ta_text_font; + +static ta_text_font font; +static int font_state; + +static ta_uint32 read_u16(const ta_uint8 *data) { + return ((ta_uint32)data[0] << 8) | data[1]; +} + +static ta_int32 read_s16(const ta_uint8 *data) { + ta_uint32 value = read_u16(data); + + if (value >= 0x8000) { + return (ta_int32)value - 0x10000; + } + + return (ta_int32)value; +} + +static ta_uint32 read_u32(const ta_uint8 *data) { + return (read_u16(data) << 16) | read_u16(data + 2); +} + +static ta_uint64 unsigned_divide64(ta_uint64 numerator, ta_uint32 denominator) { +#if TA_BITS == 64 + return numerator / denominator; +#else + if ((numerator >> 32) == 0) { + return (ta_uint32)numerator / denominator; + } + + ta_uint64 quotient = 0; + ta_uint64 remainder = 0; + + for (int bit = 63; bit >= 0; bit--) { + remainder = (remainder << 1) | ((numerator >> bit) & 1); + + if (remainder >= denominator) { + remainder -= denominator; + quotient |= (ta_uint64)1 << bit; + } + } + + return quotient; +#endif +} + +static ta_int64 signed_divide64(ta_int64 numerator, ta_int32 denominator) { + ta_bool negative = ta_false; + ta_uint64 unsigned_numerator; + ta_uint32 unsigned_denominator; + + if (numerator < 0) { + negative = !negative; + unsigned_numerator = (ta_uint64)(-numerator); + } else { + unsigned_numerator = (ta_uint64)numerator; + } + + if (denominator < 0) { + negative = !negative; + unsigned_denominator = (ta_uint32)(-denominator); + } else { + unsigned_denominator = (ta_uint32)denominator; + } + + ta_int64 quotient = (ta_int64)unsigned_divide64(unsigned_numerator, unsigned_denominator); + + return negative ? -quotient : quotient; +} + +static ta_int32 fixed_multiply(ta_int32 a, ta_int32 b) { + return (ta_int32)(((ta_int64)a * b) >> 16); +} + +static const ta_uint8 *find_table(const ta_uint8 *data, ta_uint32 length, const char *tag, ta_uint32 *out_length) { + if (length < 12) { + return 0; + } + + ta_uint32 table_count = read_u16(data + 4); + + if (length < 12 + table_count * 16) { + return 0; + } + + for (ta_uint32 i = 0; i < table_count; i++) { + const ta_uint8 *record = data + 12 + i * 16; + + if (record[0] == (ta_uint8)tag[0] && record[1] == (ta_uint8)tag[1] && record[2] == (ta_uint8)tag[2] && record[3] == (ta_uint8)tag[3]) { + ta_uint32 offset = read_u32(record + 8); + ta_uint32 table_length = read_u32(record + 12); + + if (offset > length || table_length > length - offset) { + return 0; + } + + *out_length = table_length; + + return data + offset; + } + } + + return 0; +} + +static ta_bool font_load(void) { + const ta_uint8 *font_data = ta_file_dejavu_sans; + ta_uint32 font_length = ta_file_dejavu_sans_len; + + ta_uint32 head_length; + ta_uint32 hhea_length; + ta_uint32 maxp_length; + ta_uint32 loca_length; + ta_uint32 hmtx_length; + ta_uint32 cmap_length; + + const ta_uint8 *head = find_table(font_data, font_length, "head", &head_length); + const ta_uint8 *hhea = find_table(font_data, font_length, "hhea", &hhea_length); + const ta_uint8 *maxp = find_table(font_data, font_length, "maxp", &maxp_length); + const ta_uint8 *cmap = find_table(font_data, font_length, "cmap", &cmap_length); + + font.loca = find_table(font_data, font_length, "loca", &loca_length); + font.hmtx = find_table(font_data, font_length, "hmtx", &hmtx_length); + font.glyf = find_table(font_data, font_length, "glyf", &font.glyf_length); + + if (!head || !hhea || !maxp || !font.loca || !font.hmtx || !font.glyf || !cmap) { + return ta_false; + } + + if (head_length < 54 || hhea_length < 36 || maxp_length < 6 || cmap_length < 4) { + return ta_false; + } + + font.units_per_em = read_u16(head + 18); + font.long_loca = read_s16(head + 50) != 0; + font.ascent = read_s16(hhea + 4); + font.descent = read_s16(hhea + 6); + font.line_gap = read_s16(hhea + 8); + font.horizontal_metric_count = read_u16(hhea + 34); + font.glyph_count = read_u16(maxp + 4); + + if (font.units_per_em == 0 || font.horizontal_metric_count == 0 || font.glyph_count == 0) { + return ta_false; + } + + if (font.ascent - font.descent + font.line_gap <= 0) { + return ta_false; + } + + if (loca_length < (font.glyph_count + 1) * (font.long_loca ? 4u : 2u)) { + return ta_false; + } + + if (hmtx_length < font.horizontal_metric_count * 4) { + return ta_false; + } + + const ta_uint8 *best_subtable = 0; + ta_uint32 best_length = 0; + ta_uint32 subtable_count = read_u16(cmap + 2); + + if (cmap_length < 4 + subtable_count * 8) { + return ta_false; + } + + for (ta_uint32 i = 0; i < subtable_count; i++) { + const ta_uint8 *record = cmap + 4 + i * 8; + ta_uint32 platform = read_u16(record); + ta_uint32 encoding = read_u16(record + 2); + ta_uint32 offset = read_u32(record + 4); + + if (offset > cmap_length || cmap_length - offset < 16) { + continue; + } + + if (read_u16(cmap + offset) != 4) { + continue; + } + + if (platform == 3 && encoding == 1) { + best_subtable = cmap + offset; + best_length = cmap_length - offset; + + break; + } + + if (platform == 0 && !best_subtable) { + best_subtable = cmap + offset; + best_length = cmap_length - offset; + } + } + + if (!best_subtable) { + return ta_false; + } + + ta_uint32 segment_count_x2 = read_u16(best_subtable + 6); + + if (best_length < 16 + segment_count_x2 * 4) { + return ta_false; + } + + font.cmap = best_subtable; + font.cmap_length = best_length; + + return ta_true; +} + +static ta_bool font_ready(void) { + if (font_state == 0) { + font_state = font_load() ? 1 : -1; + } + + return font_state == 1; +} + +static ta_uint32 glyph_for(ta_uint32 character) { + if (character > 0xFFFF) { + return 0; + } + + const ta_uint8 *table = font.cmap; + ta_uint32 segment_count_x2 = read_u16(table + 6); + const ta_uint8 *end_codes = table + 14; + const ta_uint8 *start_codes = table + 16 + segment_count_x2; + const ta_uint8 *deltas = table + 16 + segment_count_x2 * 2; + const ta_uint8 *range_offsets = table + 16 + segment_count_x2 * 3; + + for (ta_uint32 i = 0; i < segment_count_x2 / 2; i++) { + if (character > read_u16(end_codes + i * 2)) { + continue; + } + + ta_uint32 start = read_u16(start_codes + i * 2); + + if (character < start) { + return 0; + } + + ta_uint32 delta = read_u16(deltas + i * 2); + ta_uint32 range_offset = read_u16(range_offsets + i * 2); + + if (range_offset == 0) { + return (character + delta) & 0xFFFF; + } + + const ta_uint8 *entry = range_offsets + i * 2 + range_offset + (character - start) * 2; + + if ((ta_size)(entry + 2 - font.cmap) > font.cmap_length) { + return 0; + } + + ta_uint32 glyph = read_u16(entry); + + return glyph ? (glyph + delta) & 0xFFFF : 0; + } + + return 0; +} + +static ta_uint32 advance_of(ta_uint32 glyph_id) { + if (glyph_id >= font.horizontal_metric_count) { + glyph_id = font.horizontal_metric_count - 1; + } + + return read_u16(font.hmtx + glyph_id * 4); +} + +static const ta_uint8 *glyph_data(ta_uint32 glyph_id, ta_uint32 *length) { + if (glyph_id >= font.glyph_count) { + return 0; + } + + ta_uint32 begin; + ta_uint32 end; + + if (font.long_loca) { + begin = read_u32(font.loca + glyph_id * 4); + end = read_u32(font.loca + glyph_id * 4 + 4); + } else { + begin = read_u16(font.loca + glyph_id * 2) * 2; + end = read_u16(font.loca + glyph_id * 2 + 2) * 2; + } + + if (end <= begin || end > font.glyf_length || end - begin < 10) { + return 0; + } + + *length = end - begin; + + return font.glyf + begin; +} + +static void raster_line(ta_int32 *coverage, ta_int32 stride, ta_int32 width, ta_int32 height, + ta_int32 start_x, ta_int32 start_y, ta_int32 end_x, ta_int32 end_y) { + if (start_y == end_y) { + return; + } + + ta_int32 direction = 1; + + if (start_y > end_y) { + ta_int32 temp; + + direction = -1; + + temp = start_x; + start_x = end_x; + end_x = temp; + + temp = start_y; + start_y = end_y; + end_y = temp; + } + + if (end_y <= 0 || start_y >= height * ONE) { + return; + } + + ta_int32 delta_x = end_x - start_x; + ta_int32 delta_y_total = end_y - start_y; + ta_int32 first_row = start_y < 0 ? 0 : (start_y >> 16); + ta_int32 last_row = (end_y + (ONE - 1)) >> 16; + + if (last_row > height) { + last_row = height; + } + + ta_int32 current_x = start_x; + + if (start_y < 0) { + current_x = start_x + (ta_int32)signed_divide64((ta_int64)delta_x * (0 - start_y), delta_y_total); + } + + ta_int32 max_x = width * ONE; + + for (ta_int32 row_index = first_row; row_index < last_row; row_index++) { + ta_int32 row_top = row_index * ONE; + ta_int32 row_bottom = row_top + ONE; + ta_int32 segment_top = start_y > row_top ? start_y : row_top; + ta_int32 segment_bottom = end_y < row_bottom ? end_y : row_bottom; + ta_int32 segment_height = segment_bottom - segment_top; + ta_int32 next_x; + + if (segment_bottom == end_y) { + next_x = end_x; + } else { + next_x = start_x + (ta_int32)signed_divide64((ta_int64)delta_x * (segment_bottom - start_y), delta_y_total); + } + + ta_int32 signed_height = segment_height * direction; + ta_int32 left_x = current_x < next_x ? current_x : next_x; + ta_int32 right_x = current_x < next_x ? next_x : current_x; + + if (left_x < 0) { + left_x = 0; + } + + if (right_x < 0) { + right_x = 0; + } + + if (left_x > max_x) { + left_x = max_x; + } + + if (right_x > max_x) { + right_x = max_x; + } + + ta_int32 first_cell = left_x >> 16; + ta_int32 last_cell = (right_x + (ONE - 1)) >> 16; + ta_int32 *row = coverage + row_index * stride; + + if (last_cell <= first_cell + 1) { + ta_int32 middle_offset = ((left_x + right_x) >> 1) - (first_cell << 16); + + row[first_cell] += signed_height - fixed_multiply(signed_height, middle_offset); + row[first_cell + 1] += fixed_multiply(signed_height, middle_offset); + } else { + ta_int32 span = right_x - left_x; + ta_int32 first_fraction = left_x - (first_cell << 16); + ta_int32 first_remaining = ONE - first_fraction; + ta_int32 last_fraction = right_x - ((last_cell - 1) << 16); + ta_int32 first_area = (ta_int32)unsigned_divide64((ta_uint64)first_remaining * (ta_uint64)first_remaining, (ta_uint32)span * 2); + ta_int32 last_area = (ta_int32)unsigned_divide64((ta_uint64)last_fraction * (ta_uint64)last_fraction, (ta_uint32)span * 2); + + row[first_cell] += fixed_multiply(signed_height, first_area); + + if (last_cell == first_cell + 2) { + row[first_cell + 1] += fixed_multiply(signed_height, ONE - first_area - last_area); + } else { + ta_int32 slope_step = (ta_int32)unsigned_divide64((ta_uint64)ONE << 16, (ta_uint32)span); + ta_int32 second_area = (ta_int32)unsigned_divide64((ta_uint64)(ONE + ONE / 2 - first_fraction) << 16, (ta_uint32)span); + + row[first_cell + 1] += fixed_multiply(signed_height, second_area - first_area); + + for (ta_int32 cell = first_cell + 2; cell < last_cell - 1; cell++) { + row[cell] += fixed_multiply(signed_height, slope_step); + } + + ta_int32 final_area = second_area + (last_cell - first_cell - 3) * slope_step; + + row[last_cell - 1] += fixed_multiply(signed_height, ONE - final_area - last_area); + } + + row[last_cell] += fixed_multiply(signed_height, last_area); + } + + current_x = next_x; + } +} + +static void raster_quad(ta_int32 *coverage, ta_int32 stride, ta_int32 width, ta_int32 height, ta_text_pt start, ta_text_pt control, ta_text_pt end) { + ta_int32 second_diff_x = start.x - 2 * control.x + end.x; + ta_int32 second_diff_y = start.y - 2 * control.y + end.y; + + if (second_diff_x < 0) { + second_diff_x = -second_diff_x; + } + + if (second_diff_y < 0) { + second_diff_y = -second_diff_y; + } + + ta_int32 deviation = (second_diff_x + second_diff_y) >> 2; + ta_int32 segment_count = 1; + + while (segment_count < MAX_SEGMENTS && deviation > (ONE / 16) * segment_count * segment_count) { + segment_count++; + } + + ta_text_pt previous = start; + + for (ta_int32 i = 1; i <= segment_count; i++) { + ta_text_pt current; + + if (i == segment_count) { + current = end; + } else { + ta_int32 t = (ta_int32)((ta_uint32)i * ONE / (ta_uint32)segment_count); + ta_int32 one_minus_t = ONE - t; + ta_int64 weight_start = (ta_int64)one_minus_t * one_minus_t; + ta_int64 weight_ctrl = (ta_int64)2 * one_minus_t * t; + ta_int64 weight_end = (ta_int64)t * t; + + current.x = (ta_int32)((weight_start * start.x + weight_ctrl * control.x + weight_end * end.x) >> 32); + current.y = (ta_int32)((weight_start * start.y + weight_ctrl * control.y + weight_end * end.y) >> 32); + } + + raster_line(coverage, stride, width, height, previous.x, previous.y, current.x, current.y); + previous = current; + } +} + +#define DIV255(t) ((((t) + 128) + (((t) + 128) >> 8)) >> 8) + +static ta_uint32 blend_channel(ta_uint32 destination, ta_uint32 source, ta_uint32 alpha) { + ta_uint32 total = destination * (255 - alpha) + source * alpha; + + return DIV255(total); +} + +static void put_pixel(const ta_text_ctx *ctx, ta_int32 pixel_x, ta_int32 pixel_y, ta_uint32 alpha) { + ta_size index = (ta_size)pixel_y * ta_fb.width + (ta_size)pixel_x; + + if (ta_fb.bpp == 32) { + ta_uint32 *pixel = (ta_uint32 *)ta_fb.buffers[1] + index; + ta_uint32 destination = *pixel; + ta_uint32 red = blend_channel((destination >> 16) & 255, ctx->red, alpha); + ta_uint32 green = blend_channel((destination >> 8) & 255, ctx->green, alpha); + ta_uint32 blue = blend_channel(destination & 255, ctx->blue, alpha); + + *pixel = (destination & 0xFF000000u) | (red << 16) | (green << 8) | blue; + } else { + ta_uint8 *pixel = (ta_uint8 *)ta_fb.buffers[1] + index; + ta_uint32 destination = *pixel; + ta_uint32 dest_red = (destination >> 5) & 7; + ta_uint32 dest_green = (destination >> 2) & 7; + ta_uint32 dest_blue = destination & 3; + + dest_red = (dest_red << 5) | (dest_red << 2) | (dest_red >> 1); + dest_green = (dest_green << 5) | (dest_green << 2) | (dest_green >> 1); + dest_blue = dest_blue * 85; + + ta_uint32 red = blend_channel(dest_red, ctx->red, alpha); + ta_uint32 green = blend_channel(dest_green, ctx->green, alpha); + ta_uint32 blue = blend_channel(dest_blue, ctx->blue, alpha); + + *pixel = (ta_uint8)(((red >> 5) << 5) | ((green >> 5) << 2) | (blue >> 6)); + } +} + +static ta_text_pt transform_point(ta_int32 font_x, ta_int32 font_y, ta_int32 origin_x, ta_int32 origin_y, ta_int32 scale) { + ta_text_pt point; + + point.x = origin_x + (ta_int32)((ta_int64)font_x * scale); + point.y = origin_y - (ta_int32)((ta_int64)font_y * scale); + + return point; +} + +static void draw_glyph(const ta_text_ctx *ctx, ta_uint32 glyph_id, ta_int32 pen_x, ta_int32 baseline_y) { + ta_uint32 glyph_length; + const ta_uint8 *glyph = glyph_data(glyph_id, &glyph_length); + + if (!glyph) { + return; + } + + ta_int32 contour_count = read_s16(glyph); + + if (contour_count <= 0) { + return; + } + + ta_int32 scale = ctx->scale; + ta_int32 glyph_left = pen_x + (ta_int32)((ta_int64)read_s16(glyph + 2) * scale); + ta_int32 glyph_right = pen_x + (ta_int32)((ta_int64)read_s16(glyph + 6) * scale); + ta_int32 glyph_top = baseline_y - (ta_int32)((ta_int64)read_s16(glyph + 8) * scale); + ta_int32 glyph_bottom = baseline_y - (ta_int32)((ta_int64)read_s16(glyph + 4) * scale); + ta_int32 region_x0 = glyph_left >> 16; + ta_int32 region_x1 = (glyph_right + (ONE - 1)) >> 16; + ta_int32 region_y0 = glyph_top >> 16; + ta_int32 region_y1 = (glyph_bottom + (ONE - 1)) >> 16; + + if (region_x0 < ctx->clip_x0) { + region_x0 = ctx->clip_x0; + } + + if (region_y0 < ctx->clip_y0) { + region_y0 = ctx->clip_y0; + } + + if (region_x1 > ctx->clip_x1) { + region_x1 = ctx->clip_x1; + } + + if (region_y1 > ctx->clip_y1) { + region_y1 = ctx->clip_y1; + } + + if (region_x0 >= region_x1 || region_y0 >= region_y1) { + return; + } + + ta_int32 region_width = region_x1 - region_x0; + ta_int32 region_height = region_y1 - region_y0; + ta_int32 stride = region_width + 2; + ta_size coverage_bytes = (ta_size)stride * (ta_size)region_height * sizeof(ta_int32); + const ta_uint8 *end = glyph + glyph_length; + const ta_uint8 *cursor = glyph + 10; + + if ((ta_size)(end - cursor) < (ta_size)contour_count * 2 + 2) { + return; + } + + ta_uint32 point_count = read_u16(cursor + (contour_count - 1) * 2) + 1; + const ta_uint8 *contour_ends = cursor; + + cursor += contour_count * 2; + cursor += 2 + read_u16(cursor); + + if (cursor > end || point_count > 4096) { + return; + } + + ta_size points_bytes = (ta_size)point_count * 5; + void *memory = ta_malloc(points_bytes + coverage_bytes + 8); + + if (!memory) { + return; + } + + ta_int16 *point_x = (ta_int16 *)memory; + ta_int16 *point_y = point_x + point_count; + ta_uint8 *flags = (ta_uint8 *)(point_y + point_count); + ta_int32 *coverage = (ta_int32 *)(((ta_size)(flags + point_count) + 7) & ~(ta_size)7); + ta_bool parse_ok = ta_true; + + for (ta_uint32 i = 0; i < point_count && parse_ok;) { + if (cursor >= end) { + parse_ok = ta_false; + + break; + } + + ta_uint8 flag = *cursor++; + + flags[i++] = flag; + + if (flag & 8) { + if (cursor >= end) { + parse_ok = ta_false; + + break; + } + + ta_uint32 repeat_count = *cursor++; + + while (repeat_count > 0 && i < point_count) { + flags[i++] = flag; + repeat_count--; + } + } + } + + for (ta_uint32 pass = 0; pass < 2 && parse_ok; pass++) { + ta_int16 *output = pass ? point_y : point_x; + ta_uint8 short_bit = pass ? 4 : 2; + ta_uint8 same_bit = pass ? 32 : 16; + ta_int32 value = 0; + + for (ta_uint32 i = 0; i < point_count; i++) { + ta_uint8 flag = flags[i]; + + if (flag & short_bit) { + if (cursor >= end) { + parse_ok = ta_false; + + break; + } + + ta_int32 delta = *cursor++; + + value += (flag & same_bit) ? delta : -delta; + } else if (!(flag & same_bit)) { + if (end - cursor < 2) { + parse_ok = ta_false; + + break; + } + + value += read_s16(cursor); + cursor += 2; + } + + output[i] = (ta_int16)value; + } + } + + if (!parse_ok) { + ta_free(memory); + + return; + } + + for (ta_size i = 0; i < coverage_bytes / sizeof(ta_int32); i++) { + coverage[i] = 0; + } + + ta_int32 origin_x = pen_x - region_x0 * ONE; + ta_int32 origin_y = baseline_y - region_y0 * ONE; + +#define POINT(index) transform_point((ta_int32)point_x[index], (ta_int32)point_y[index], origin_x, origin_y, scale) + + ta_uint32 contour_first = 0; + + for (ta_int32 contour = 0; contour < contour_count; contour++) { + ta_uint32 contour_last = read_u16(contour_ends + contour * 2); + ta_uint32 contour_start = contour_first; + + contour_first = contour_last + 1; + + if (contour_last < contour_start || contour_last >= point_count || contour_last - contour_start < 1) { + continue; + } + + ta_uint32 contour_points = contour_last - contour_start + 1; + + ta_text_pt start_point; + ta_uint32 begin; + ta_uint32 count; + + if (flags[contour_start] & 1) { + start_point = POINT(contour_start); + begin = contour_start + 1; + count = contour_points - 1; + } else if (flags[contour_last] & 1) { + start_point = POINT(contour_last); + begin = contour_start; + count = contour_points - 1; + } else { + ta_text_pt first_point = POINT(contour_start); + ta_text_pt last_point = POINT(contour_last); + start_point.x = (first_point.x >> 1) + (last_point.x >> 1); + start_point.y = (first_point.y >> 1) + (last_point.y >> 1); + begin = contour_start; + count = contour_points; + } + + ta_text_pt current = start_point; + ta_text_pt control = start_point; + ta_bool has_control = ta_false; + + for (ta_uint32 k = 0; k < count; k++) { + ta_uint32 index = begin + k; + ta_text_pt point = POINT(index); + + if (flags[index] & 1) { + if (has_control) { + raster_quad(coverage, stride, region_width, region_height, current, control, point); + } else { + raster_line(coverage, stride, region_width, region_height, current.x, current.y, point.x, point.y); + } + + current = point; + has_control = ta_false; + } else if (has_control) { + ta_text_pt midpoint; + + midpoint.x = (control.x >> 1) + (point.x >> 1); + midpoint.y = (control.y >> 1) + (point.y >> 1); + + raster_quad(coverage, stride, region_width, region_height, current, control, midpoint); + + current = midpoint; + control = point; + } else { + control = point; + has_control = ta_true; + } + } + + if (has_control) { + raster_quad(coverage, stride, region_width, region_height, current, control, start_point); + } else { + raster_line(coverage, stride, region_width, region_height, current.x, current.y, start_point.x, start_point.y); + } + } + +#undef POINT + + for (ta_int32 row_index = 0; row_index < region_height; row_index++) { + ta_int32 running_sum = 0; + const ta_int32 *row = coverage + row_index * stride; + + for (ta_int32 column = 0; column < region_width; column++) { + running_sum += row[column]; + + ta_int32 amount = running_sum < 0 ? -running_sum : running_sum; + + if (amount > ONE) { + amount = ONE; + } + + ta_uint32 alpha = ((ta_uint32)amount * 255 + 0x8000) >> 16; + + if (alpha) { + put_pixel(ctx, region_x0 + column, region_y0 + row_index, alpha); + } + } + } + + ta_free(memory); +} + +static ta_bool printable(ta_uint32 character) { + return character >= 32 && character != 127; +} + +static ta_uint32 line_height_units(void) { + return (ta_uint32)(font.ascent - font.descent + font.line_gap); +} + +static void measure_units(const char *text, ta_uint32 *max_width, ta_uint32 *lines) { + ta_uint32 current_width = 0; + ta_uint32 best_width = 0; + ta_uint32 line_count = 1; + + for (; *text; text++) { + ta_uint32 character = (ta_uint8)*text; + + if (character == '\n') { + if (current_width > best_width) { + best_width = current_width; + } + + current_width = 0; + line_count++; + } else if (printable(character)) { + current_width += advance_of(glyph_for(character)); + } + } + + if (current_width > best_width) { + best_width = current_width; + } + + *max_width = best_width; + *lines = line_count; +} + +static ta_bool fb_usable(void) { + return ta_fb.buffers[1] != 0 && (ta_fb.bpp == 32 || ta_fb.bpp == 8); +} + +static ta_int32 fb_limit(ta_size value) { + return value > MAX_COORD ? MAX_COORD : (ta_int32)value; +} + +static void draw_text_pass(const char *text, ta_int32 x, ta_int32 y, const ta_text_ctx *ctx) { + ta_int32 scale = ctx->scale; + ta_int32 ascent = (ta_int32)((ta_int64)font.ascent * scale); + ta_int32 line_height = (ta_int32)((ta_int64)line_height_units() * scale); + ta_int32 pen_x = x * ONE; + ta_int32 baseline_y = y * ONE + ascent; + + for (; *text; text++) { + ta_uint32 character = (ta_uint8)*text; + + if (character == '\n') { + pen_x = x * ONE; + baseline_y += line_height; + + if (baseline_y - ascent >= ctx->clip_y1 * ONE) { + break; + } + + continue; + } + + if (!printable(character) || pen_x >= ctx->clip_x1 * ONE) { + continue; + } + + ta_uint32 glyph_id = glyph_for(character); + + draw_glyph(ctx, glyph_id, pen_x, baseline_y); + pen_x += (ta_int32)((ta_int64)advance_of(glyph_id) * scale); + } +} + +static void draw_text(const char *text, ta_int32 x, ta_int32 y, const ta_text_ctx *ctx) { + if (ctx->border_thickness != 0) { + ta_text_ctx border_ctx = *ctx; + + border_ctx.red = ctx->border_red; + border_ctx.green = ctx->border_green; + border_ctx.blue = ctx->border_blue; + ta_int32 thickness = ctx->border_thickness; + + for (ta_int32 offset_y = -thickness; offset_y <= thickness; offset_y++) { + for (ta_int32 offset_x = -thickness; offset_x <= thickness; offset_x++) { + if (offset_x == 0 && offset_y == 0) { + continue; + } + + draw_text_pass(text, x + offset_x, y + offset_y, &border_ctx); + } + } + } + + draw_text_pass(text, x, y, ctx); +} + +static void draw_text_fitted(const char *text, ta_uint32 x, ta_uint32 y, ta_uint32 width, ta_uint32 height, ta_uint8 red, ta_uint8 green, ta_uint8 blue, ta_uint8 border_thickness, ta_uint8 border_red, ta_uint8 border_green, ta_uint8 border_blue) { + if (!text || width == 0 || height == 0 || !fb_usable() || !font_ready()) { + return; + } + + ta_int32 frame_width = fb_limit(ta_fb.width); + ta_int32 frame_height = fb_limit(ta_fb.height); + + if (x >= (ta_uint32)frame_width || y >= (ta_uint32)frame_height) { + return; + } + + ta_uint32 text_width; + ta_uint32 lines; + + measure_units(text, &text_width, &lines); + + if (text_width == 0) { + return; + } + + ta_uint64 scale_by_height = unsigned_divide64((ta_uint64)height << 16, lines * line_height_units()); + ta_uint64 scale_by_width = unsigned_divide64((ta_uint64)width << 16, text_width); + ta_uint64 scale_cap = unsigned_divide64((ta_uint64)MAX_SIZE << 16, font.units_per_em); + ta_uint64 scale = scale_by_height < scale_by_width ? scale_by_height : scale_by_width; + + if (scale > scale_cap) { + scale = scale_cap; + } + + if (scale == 0) { + return; + } + + ta_text_ctx ctx; + + ctx.scale = (ta_int32)scale; + ctx.clip_x0 = (ta_int32)x; + ctx.clip_y0 = (ta_int32)y; + ctx.clip_x1 = width > (ta_uint32)frame_width - x ? frame_width : (ta_int32)(x + width); + ctx.clip_y1 = height > (ta_uint32)frame_height - y ? frame_height : (ta_int32)(y + height); + ctx.red = red; + ctx.green = green; + ctx.blue = blue; + ctx.border_thickness = border_thickness; + ctx.border_red = border_red; + ctx.border_green = border_green; + ctx.border_blue = border_blue; + + draw_text(text, (ta_int32)x, (ta_int32)y, &ctx); +} + +void ta_text_draw(const char *text, ta_uint32 x, ta_uint32 y, ta_uint32 width, ta_uint32 height, ta_uint8 red, ta_uint8 green, ta_uint8 blue) { + draw_text_fitted(text, x, y, width, height, red, green, blue, 0, 0, 0, 0); +} + +void ta_text_draw_outline(const char *text, ta_uint32 x, ta_uint32 y, ta_uint32 width, ta_uint32 height, + ta_uint8 red, ta_uint8 green, ta_uint8 blue, ta_uint8 border_thickness, + ta_uint8 border_red, ta_uint8 border_green, ta_uint8 border_blue) { + draw_text_fitted(text, x, y, width, height, red, green, blue, + border_thickness, border_red, border_green, border_blue); +} + +void ta_text_draw_size(const char *text, ta_uint32 x, ta_uint32 y, ta_uint32 size, ta_uint8 red, ta_uint8 green, ta_uint8 blue) { + if (!text || size == 0 || !fb_usable() || !font_ready()) { + return; + } + + if (size > MAX_SIZE) { + size = MAX_SIZE; + } + + ta_int32 frame_width = fb_limit(ta_fb.width); + ta_int32 frame_height = fb_limit(ta_fb.height); + + if (x >= (ta_uint32)frame_width || y >= (ta_uint32)frame_height) { + return; + } + + ta_text_ctx ctx; + + ctx.scale = (ta_int32)unsigned_divide64((ta_uint64)size << 16, font.units_per_em); + ctx.clip_x0 = (ta_int32)x; + ctx.clip_y0 = (ta_int32)y; + ctx.clip_x1 = frame_width; + ctx.clip_y1 = frame_height; + ctx.red = red; + ctx.green = green; + ctx.blue = blue; + ctx.border_thickness = 0; + ctx.border_red = 0; + ctx.border_green = 0; + ctx.border_blue = 0; + + draw_text(text, (ta_int32)x, (ta_int32)y, &ctx); +} + +void ta_text_measure(const char *text, ta_uint32 size, ta_uint32 *width, ta_uint32 *height) { + ta_uint32 measured_width = 0; + ta_uint32 measured_height = 0; + + if (text && font_ready()) { + if (size > MAX_SIZE) { + size = MAX_SIZE; + } + + ta_uint32 units; + ta_uint32 lines; + ta_uint64 scale = unsigned_divide64((ta_uint64)size << 16, font.units_per_em); + + measure_units(text, &units, &lines); + + measured_width = (ta_uint32)(((ta_uint64)units * scale + (ONE - 1)) >> 16); + measured_height = (ta_uint32)(((ta_uint64)lines * line_height_units() * scale + (ONE - 1)) >> 16); + } + + if (width) { + *width = measured_width; + } + + if (height) { + *height = measured_height; + } +}