├── .gitignore
├── COPYING
├── Makefile
├── README.md
├── ezsdl.h
├── sdlbook.c
└── topaz.h
/.gitignore:
--------------------------------------------------------------------------------
1 | *.o
2 | *.out
3 | *.rcb
4 | rcb.*.mak
5 | config.mak
6 | sdlbook
7 |
8 |
--------------------------------------------------------------------------------
/COPYING:
--------------------------------------------------------------------------------
1 | GNU GENERAL PUBLIC LICENSE
2 |
3 | Version 3, 29 June 2007
4 |
5 | Copyright © 2007 Free Software Foundation, Inc.
6 |
7 | Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed.
8 | Preamble
9 |
10 | The GNU General Public License is a free, copyleft license for software and other kinds of works.
11 |
12 | 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
13 | 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
14 | your programs, too.
15 |
16 | 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
17 | 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.
18 |
19 | 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
20 | respect the freedom of others.
21 |
22 | 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
23 | these terms so they know their rights.
24 |
25 | 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.
26 |
27 | 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
28 | attributed erroneously to authors of previous versions.
29 |
30 | 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.
31 | 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
32 | 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.
33 |
34 | 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
35 | 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.
36 |
37 | The precise terms and conditions for copying, distribution and modification follow.
38 | TERMS AND CONDITIONS
39 | 0. Definitions.
40 |
41 | “This License” refers to version 3 of the GNU General Public License.
42 |
43 | “Copyright” also means copyright-like laws that apply to other kinds of works, such as semiconductor masks.
44 |
45 | “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.
46 |
47 | 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
48 | earlier work.
49 |
50 | A “covered work” means either the unmodified Program or a work based on the Program.
51 |
52 | 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
53 | includes copying, distribution (with or without modification), making available to the public, and in some countries other activities as well.
54 |
55 | 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.
56 |
57 | 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
58 | 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
59 | the list meets this criterion.
60 | 1. Source Code.
61 |
62 | 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.
63 |
64 | 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
65 | that language.
66 |
67 | 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
68 | 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
69 | 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.
70 |
71 | 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
72 | 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
73 | 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
74 | those subprograms and other parts of the work.
75 |
76 | The Corresponding Source need not include anything that users can regenerate automatically from other parts of the Corresponding Source.
77 |
78 | The Corresponding Source for a work in source code form is that same work.
79 | 2. Basic Permissions.
80 |
81 | 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
82 | 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.
83 |
84 | 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
85 | 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
86 | 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.
87 |
88 | Conveying under any other circumstances is permitted solely under the conditions stated below. Sublicensing is not allowed; section 10 makes it unnecessary.
89 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
90 |
91 | 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
92 | circumvention of such measures.
93 |
94 | 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
95 | 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.
96 | 4. Conveying Verbatim Copies.
97 |
98 | 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
99 | 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.
100 |
101 | 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.
102 | 5. Conveying Modified Source Versions.
103 |
104 | 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:
105 | a) The work must carry prominent notices stating that you modified it, and giving a relevant date.
106 | 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”.
107 | 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,
108 | 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.
109 | 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.
110 |
111 | 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
112 | 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
113 | aggregate does not cause this License to apply to the other parts of the aggregate.
114 | 6. Conveying Non-Source Forms.
115 |
116 | 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:
117 | 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.
118 | 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
119 | 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
120 | 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.
121 | 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
122 | accord with subsection 6b.
123 | 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
124 | 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,
125 | 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
126 | satisfy these requirements.
127 | 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.
128 |
129 | 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.
130 |
131 | 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
132 | 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
133 | 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
134 | non-consumer uses, unless such uses represent the only significant mode of use of the product.
135 |
136 | “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
137 | 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.
138 |
139 | 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
140 | 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
141 | 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).
142 |
143 | 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
144 | 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.
145 |
146 | 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
147 | password or key for unpacking, reading or copying.
148 | 7. Additional Terms.
149 |
150 | “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
151 | 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
152 | without regard to the additional permissions.
153 |
154 | 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.)
155 | You may place additional permissions on material, added by you to a covered work, for which you have or can give appropriate copyright permission.
156 |
157 | 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:
158 | a) Disclaiming warranty or limiting liability differently from the terms of sections 15 and 16 of this License; or
159 | 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
160 | 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
161 | d) Limiting the use for publicity purposes of names of licensors or authors of the material; or
162 | e) Declining to grant rights under trademark law for use of some trade names, trademarks, or service marks; or
163 | 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
164 | directly impose on those licensors and authors.
165 |
166 | 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
167 | 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
168 | document, provided that the further restriction does not survive such relicensing or conveying.
169 |
170 | 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.
171 |
172 | 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.
173 | 8. Termination.
174 |
175 | 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
176 | granted under the third paragraph of section 11).
177 |
178 | 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
179 | the copyright holder fails to notify you of the violation by some reasonable means prior to 60 days after the cessation.
180 |
181 | 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
182 | work) from that copyright holder, and you cure the violation prior to 30 days after your receipt of the notice.
183 |
184 | 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
185 | new licenses for the same material under section 10.
186 | 9. Acceptance Not Required for Having Copies.
187 |
188 | 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
189 | 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
190 | your acceptance of this License to do so.
191 | 10. Automatic Licensing of Downstream Recipients.
192 |
193 | 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
194 | this License.
195 |
196 | 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
197 | 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
198 | work from the predecessor in interest, if the predecessor has it or can get it with reasonable efforts.
199 |
200 | 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
201 | 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.
202 | 11. Patents.
203 |
204 | 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”.
205 |
206 | 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
207 | 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
208 | consistent with the requirements of this License.
209 |
210 | 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
211 | version.
212 |
213 | 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”
214 | such a patent license to a party means to make such an agreement or commitment not to enforce a patent against the party.
215 |
216 | 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
217 | 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
218 | 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
219 | covered work in a country, would infringe one or more identifiable patents in that country that you have reason to believe are valid.
220 |
221 | 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,
222 | 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.
223 |
224 | 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
225 | 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
226 | 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
227 | 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.
228 |
229 | 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.
230 | 12. No Surrender of Others' Freedom.
231 |
232 | 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
233 | 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
234 | 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.
235 | 13. Use with the GNU Affero General Public License.
236 |
237 | 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.
238 | 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.
239 | 14. Revised Versions of this License.
240 |
241 | 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.
242 |
243 | 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
244 | 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.
245 |
246 | 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.
247 |
248 | 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.
249 | 15. Disclaimer of Warranty.
250 |
251 | 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,
252 | 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
253 | NECESSARY SERVICING, REPAIR OR CORRECTION.
254 | 16. Limitation of Liability.
255 |
256 | 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
257 | 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
258 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
259 | 17. Interpretation of Sections 15 and 16.
260 |
261 | 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
262 | connection with the Program, unless a warranty or assumption of liability accompanies a copy of the Program in return for a fee.
263 |
264 | END OF TERMS AND CONDITIONS
265 | How to Apply These Terms to Your New Programs
266 |
267 | 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.
268 |
269 | 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
270 | full notice is found.
271 |
272 | Copyright (C)
273 |
274 | This program is free software: you can redistribute it and/or modify
275 | it under the terms of the GNU General Public License as published by
276 | the Free Software Foundation, either version 3 of the License, or
277 | (at your option) any later version.
278 |
279 | This program is distributed in the hope that it will be useful,
280 | but WITHOUT ANY WARRANTY; without even the implied warranty of
281 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
282 | GNU General Public License for more details.
283 |
284 | You should have received a copy of the GNU General Public License
285 | along with this program. If not, see .
286 |
287 | Also add information on how to contact you by electronic and paper mail.
288 |
289 | If the program does terminal interaction, make it output a short notice like this when it starts in an interactive mode:
290 | Copyright (C)
291 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
292 | This is free software, and you are welcome to redistribute it
293 | under certain conditions; type `show c' for details.
294 |
295 | 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”.
296 |
297 | 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
298 | .
299 |
300 | 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
301 | you want to do, use the GNU Lesser General Public License instead of this License. But first, please read .
302 |
--------------------------------------------------------------------------------
/Makefile:
--------------------------------------------------------------------------------
1 | #Makefile autogenerated by RcB2
2 | prefix = /usr/local
3 | bindir = $(prefix)/bin
4 |
5 | PROG = sdlbook
6 | SRCS = sdlbook.c
7 | OBJS = $(SRCS:.c=.o)
8 |
9 | -include config.mak
10 |
11 | ifeq ($(SDL2),)
12 | SDL_LIBS = -lSDL
13 | CPPF_SDL =
14 | else
15 | SDL_LIBS = -lSDL2
16 | CPPF_SDL = -DUSE_SDL2
17 | endif
18 |
19 | LIBS = -ldjvulibre -lmupdf $(SDL_LIBS) -lm
20 |
21 | CFLAGS_N =
22 | CPPFLAGS_N = $(CPPF_SDL)
23 | LDFLAGS_N =
24 |
25 | MAKEFILE := $(firstword $(MAKEFILE_LIST))
26 |
27 | all: $(PROG)
28 |
29 | clean:
30 | rm -f $(PROG)
31 | rm -f $(OBJS)
32 |
33 | rebuild:
34 | $(MAKE) -f $(MAKEFILE) clean && $(MAKE) -f $(MAKEFILE) all
35 |
36 | install: $(PROG)
37 | install -d $(DESTDIR)/$(bindir)
38 | install -D -m 755 $(PROG) $(DESTDIR)/$(bindir)/
39 |
40 | src: $(SRCS)
41 | $(CC) $(CPPFLAGS_N) $(CPPFLAGS) $(CFLAGS_N) $(CFLAGS) -o $(PROG) $^ $(LDFLAGS_N) $(LDFLAGS) $(LIBS)
42 |
43 | sdlbook.o: ezsdl.h
44 |
45 | %.o: %.c
46 | $(CC) $(CPPFLAGS_N) $(CPPFLAGS) $(CFLAGS_N) $(CFLAGS) -c -o $@ $<
47 |
48 | $(PROG): $(OBJS)
49 | $(CC) $(CFLAGS_N) $(CFLAGS) $(LDFLAGS_N) $(LDFLAGS) $(OBJS) $(LIBS) -o $@
50 |
51 | .PHONY: all clean rebuild install src
52 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | SDLBook - a tiny djvu/pdf eBook reader
2 | ======================================
3 |
4 | ## What?
5 |
6 | Tiny eBook reader using only `SDL` or `SDL2`, `djvulibre`, and `libmupdf`.
7 |
8 | ## Why?
9 |
10 | Since there are no djvu readers with reasonable dependency requirements
11 | (i.e. GTK+2, X11, SDL1 or 2, framebuffer) available, I decided to write
12 | my own, by taking a firm glance at what djvulibre's `ddjvu` utility does
13 | and rendering to a video buffer instead of a file. I'm not interested in
14 | installing heavy-weight GUI toolkits such as QT4,5,GTK+3,4 etc just for
15 | a simple eBook reader. Nor in installing python3 just to run someone's
16 | meson build recipe.
17 |
18 | As I dislike the interruptive page jumping of `mupdf-x11`, I've added
19 | mupdf support too so it can take advantage of the smooth page border
20 | scrolling mechanism of SDLBook. Therefore it now also supports all
21 | the formats supported by mupdf, namely `pdf`, `epub`, `fb2`, `xps`,
22 | `openxps`, `cbz`, `cbr`.
23 |
24 | ## How?
25 |
26 | - Install `SDL` and `djvulibre` libraries including development headers.
27 | - Compile using `gnu make` by running `make CFLAGS=-O2`.
28 | if you want to use SDL2, run `make SDL2=1 CFLAGS=-O2` instead.
29 | - Install as root user by doing `make install prefix=/usr`.
30 | - Run like `sdlbook /path/to/file.djvu`
31 | - Press F1 to see available keyboard shortcuts
32 | - Have fun reading.
33 |
--------------------------------------------------------------------------------
/ezsdl.h:
--------------------------------------------------------------------------------
1 | #ifndef EZSDL_H
2 | #define EZSDL_H
3 |
4 | #ifdef USE_SDL2
5 | #include
6 | #else
7 | #include
8 | #endif
9 | #include
10 | #include
11 | #include
12 |
13 | #ifndef EZSDL_BITDEPTH
14 | #define EZSDL_BITDEPTH 32
15 | #endif
16 |
17 | #if EZSDL_BITDEPTH == 32
18 | #define EZSDL_PIXEL_FMT SDL_PIXELFORMAT_ARGB8888
19 | #else
20 | #define EZSDL_PIXEL_FMT SDL_PIXELFORMAT_RGB565
21 | #endif
22 |
23 | #ifdef USE_SDL2
24 | #pragma RcB2 LINK "-lSDL2"
25 | #else
26 | #pragma RcB2 LINK "-lSDL"
27 | #endif
28 |
29 | #define SDL_RGB_LSHIFT 8
30 | #define RGB_RED(X) 0 | ((X) << (16+SDL_RGB_LSHIFT))
31 | #define RGB_GREEN(X) 0 | ((X) << (8+SDL_RGB_LSHIFT))
32 | #define RGB_BLUE(X) (X << (0+SDL_RGB_LSHIFT))
33 | #define RGB(R,G,B) (RGB_RED(R) | RGB_GREEN(G) | RGB_BLUE(B))
34 |
35 | #define ARGB_RED(X) 0 | ((X) << 16)
36 | #define ARGB_GREEN(X) 0 | ((X) << 8)
37 | #define ARGB_BLUE(X) (X << 0)
38 | #define ARGB(R,G,B) (ARGB_RED(R) | ARGB_GREEN(G) | ARGB_BLUE(B))
39 |
40 | #ifndef MAX
41 | #define MAX(A, B) (((A) > (B)) ? (A) : (B))
42 | #define MIN(A, B) (((A) < (B)) ? (A) : (B))
43 | #endif
44 |
45 | #define SCALEUP(V, S) (V*S/100)
46 | #define SCALEDOWN(V, S) (V*100/S)
47 | #define SCALECALC(OLDV, NEWV) (NEWV*100/OLDV)
48 | #define NEWSCALE(OLDW, NEWW, OLDH, NEWH) MAX(SCALECALC(OLDW, NEWW), SCALECALC(OLDH, NEWH))
49 |
50 | static inline unsigned rgba_to_argb(unsigned col) { return col >> 8; }
51 | static inline unsigned argb_to_rgba(unsigned col) { return col << 8; }
52 |
53 | typedef struct bmp4 {
54 | unsigned width, height;
55 | unsigned *data;
56 | } bmp4;
57 |
58 | static inline bmp4* bmp4_new(unsigned width, unsigned height) {
59 | size_t wxh = (size_t)width * (size_t)height;
60 | bmp4* res = malloc((sizeof *res)+(wxh*sizeof(*res->data)));
61 | if(!res) return 0;
62 | res->width=width;
63 | res->height=height;
64 | res->data=(void*)(res+1);
65 | return res;
66 | }
67 |
68 | static inline bmp4* bmp4_from_filestream(FILE *f) {
69 | unsigned w, h;
70 | fread(&w, 4, 1, f);
71 | fread(&h, 4, 1, f);
72 | bmp4* b = bmp4_new(w, h);
73 | if(!b) goto ext;
74 | size_t i, wxh = (size_t)w * (size_t)h;
75 | unsigned *p = b->data;
76 | for(i=0; iwidth*(size_t)b->height;
91 | unsigned *d = b->data;
92 | for(;idata[y * b->width];
103 | }
104 |
105 | typedef struct bmp3 {
106 | unsigned width, height;
107 | unsigned char *data;
108 | } bmp3;
109 |
110 | static inline bmp3* bmp3_new(unsigned width, unsigned height) {
111 | bmp3* res = malloc((sizeof *res)+(width*height*3));
112 | res->width=width;
113 | res->height=height;
114 | res->data=(void*)(res+1);
115 | return res;
116 | }
117 |
118 | static inline unsigned char* bmp3_scanline(bmp3* b, int y) {
119 | return &b->data[3*y*b->width];
120 | }
121 |
122 | static inline void bmp3_to_bmp4(bmp3* in, bmp4 *out) {
123 | assert(in->width == out->width && in->height == out->height);
124 | size_t i = 0;
125 | size_t max = (size_t)in->width*(size_t)in->height;
126 | unsigned *d = out->data;
127 | unsigned char* id = in->data;
128 | #define ALPHA (0xff << 24)
129 | for(;iwidth == out->width && in->height == out->height);
135 | size_t i = 0;
136 | size_t max = (size_t)in->width*(size_t)in->height;
137 | unsigned char *d = out->data;
138 | unsigned* id = in->data;
139 | for(;i> 16) & 255;
141 | d[1] = (*id >> 8 ) & 255;
142 | d[2] = (*id >> 0 ) & 255;
143 | }
144 | }
145 |
146 | typedef struct bmp1 {
147 | unsigned width, height;
148 | unsigned char *data;
149 | } bmp1;
150 |
151 | static inline bmp1* bmp1_new(unsigned width, unsigned height) {
152 | bmp1 *res = malloc((sizeof *res)+(width*height*1));
153 | res->width=width;
154 | res->height=height;
155 | res->data=(void*)(res+1);
156 | return res;
157 | }
158 |
159 | static inline unsigned char* bmp1_scanline(bmp1* b, int y) {
160 | return &b->data[y*b->width];
161 | }
162 |
163 | static inline unsigned colget8(unsigned char col, int which) {
164 | switch (which) {
165 | case 0: /* red */
166 | return (col >> 5) * 36;
167 | case 1: /* green */
168 | return ((col >> 3) & 3) * 85;
169 | case 2: /* blue */
170 | return (col & 7) * 36;
171 | default:
172 | return 0;
173 | }
174 | }
175 |
176 | static inline void bmp1_to_bmp4(bmp1* in, bmp4 *out, unsigned palette[256]) {
177 | assert(in->width == out->width && in->height == out->height);
178 | size_t i = 0;
179 | size_t max = (size_t)in->width*(size_t)in->height;
180 | unsigned *d = out->data;
181 | unsigned char* id = in->data;
182 | #define ALPHA (0xff << 24)
183 | if(!palette)
184 | for(;irm = rm;
212 | }
213 |
214 | /* display pointed to is assumed to be zero-filled */
215 | /* hwscale uses SDL2 hardware scaling to render. it's provided as a percentage.
216 | so 100 means scale 1. the width and height provided need to be the
217 | UNSCALED dimensions. so if you code for example an emulator for a machine
218 | with 320x240 pixels, and want 2x scale, set width=320, height=240, hwscale=200
219 | and the actual window will be 640x480. however, this works transparently,
220 | so you can do all calculations within the 1x scale boundaries, including
221 | mouse movements. when you call display_get_width(), it will return 320. */
222 | static inline void display_init(display *d, unsigned width, unsigned height, unsigned hwscale, int flags) {
223 | static int init_done;
224 | int sw = SCALEUP(width, hwscale);
225 | int sh = SCALEUP(height, hwscale);
226 | #ifndef USE_SDL2
227 | SDL_Surface *old = d->surface;
228 | if(!flags) flags = SDL_HWPALETTE;
229 | assert("sorry, SDL1 support for hwscale other than 100 not implemented!" && hwscale == 100);
230 | #endif
231 | if(!init_done) SDL_Init(SDL_INIT_VIDEO);
232 | init_done = 1;
233 | #ifdef USE_SDL2
234 | if(!d->win) {
235 | d->win = SDL_CreateWindow("ezsdl", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, sw, sh, SDL_WINDOW_OPENGL|SDL_WINDOW_RESIZABLE);
236 | SDL_DisplayMode dm = {.format = EZSDL_PIXEL_FMT, .w = sw, .h = sh };
237 | SDL_SetWindowDisplayMode(d->win, &dm);
238 | /* SDL2 defaults to inhibit the screensaver, which makes sense
239 | for an action game using a joystick, but we use it
240 | for an ebook reader that's often minimized or backgrounded
241 | and disabling the screensaver for that is confusing at best. */
242 | SDL_EnableScreenSaver();
243 | }
244 | if(d->tex)
245 | SDL_DestroyTexture(d->tex);
246 | if(d->ren)
247 | SDL_DestroyRenderer(d->ren);
248 | d->ren = SDL_CreateRenderer(d->win, -1, SDL_RENDERER_ACCELERATED|SDL_RENDERER_TARGETTEXTURE);
249 | d->tex = SDL_CreateTexture(d->ren, EZSDL_PIXEL_FMT, SDL_TEXTUREACCESS_STREAMING, width, height);
250 | #else
251 | d->surface = SDL_SetVideoMode(width, height, EZSDL_BITDEPTH, flags);
252 | if(old && old != d->surface) SDL_FreeSurface(old);
253 | #endif
254 | d->width = width;
255 | d->height = height;
256 | d->fs = 0;
257 | d->flags = flags;
258 | d->hwscale = hwscale;
259 | }
260 |
261 | static inline void display_toggle_fullscreen_i(display *d, int update);
262 | static inline void display_shutdown(display *d) {
263 | if(d->fs) display_toggle_fullscreen_i(d, 0);
264 | SDL_QuitSubSystem(SDL_INIT_VIDEO);
265 | }
266 |
267 | static inline void display_get_vram_and_pitch(display *d, void** pixels, unsigned *pitch)
268 | {
269 | #ifdef USE_SDL2
270 | SDL_LockTexture(d->tex, 0, pixels, pitch);
271 | #else
272 | *pitch = d->surface->pitch;
273 | *pixels = d->surface->pixels;
274 | #endif
275 | }
276 |
277 | static inline void display_release_vram(display *d) {
278 | #ifdef USE_SDL2
279 | SDL_UnlockTexture(d->tex);
280 | #endif
281 | }
282 |
283 | static inline unsigned display_get_width(display *d) {
284 | return d->width;
285 | }
286 |
287 | static inline unsigned display_get_height(display *d) {
288 | return d->height;
289 | }
290 |
291 | static inline bmp4 *display_get_screenshot(display *d) {
292 | bmp4 *r = bmp4_new(d->width, d->height);
293 | if(!r) return 0;
294 | unsigned pitch, *out = r->data, *in;
295 | void *pixels;
296 | display_get_vram_and_pitch(d, &pixels, &pitch);
297 | in = pixels;
298 | size_t i, l = (size_t)d->width*(size_t)d->height;
299 | for(i=0;ibitmap = b;
312 | result->sprite_w = sprite_w;
313 | result->sprite_h = sprite_h;
314 | result->sprite_count = (b->height * b->width) / (sprite_h * sprite_w);
315 | result->sprites_per_row = b->width / sprite_w;
316 | if((b->height * b->width) % (sprite_h * sprite_w)) return 0; /* picture dimensions are no multiple of the sprite dimensions*/
317 | return 1;
318 | }
319 |
320 | static inline unsigned spritesheet_getspritestart(struct spritesheet *ss, unsigned sprite_nr, unsigned row_nr) {
321 | unsigned sprite_row = sprite_nr / ss->sprites_per_row;
322 | unsigned row_off = (row_nr * ss->sprite_w * ss->sprites_per_row);
323 | unsigned res = (sprite_row * ss->sprite_w * ss->sprite_h * ss->sprites_per_row) + row_off
324 | + ((sprite_nr % ss->sprites_per_row) * ss->sprite_w);
325 | return res;
326 | }
327 |
328 | static inline void display_draw_sprite(display *d, struct spritesheet* ss, unsigned sprite_no, unsigned sx, unsigned sy, unsigned scale) {
329 | if(!scale) scale = 1;
330 | if(!(d->width >= sx+ss->sprite_w*scale && d->height >= sy+ss->sprite_h*scale))
331 | return;
332 | unsigned x,xx,y,yd,ys,pitch;
333 | unsigned xscale,yscale;
334 | unsigned transp_col = ss->bitmap->data[0];
335 | unsigned sprite_pitch = ss->sprites_per_row * ss->sprite_w;
336 | void *pixels;
337 | display_get_vram_and_pitch(d, &pixels, &pitch);
338 | pitch /= sizeof(unsigned);
339 |
340 | for(y=0,yd=sy*pitch, ys=spritesheet_getspritestart(ss, sprite_no, y); y < ss->sprite_h; y++,ys+=sprite_pitch)
341 | for(yscale=0; yscalesprite_w; x++)
343 | for(xscale = 0; xscale < scale; xscale++,xx++)
344 | if(ss->bitmap->data[ys+x] != transp_col)
345 | ((unsigned*)pixels)[yd + xx] = rgba_to_argb(ss->bitmap->data[ys+x]);
346 | display_release_vram(d);
347 | }
348 |
349 | static inline void display_draw(display *d, bmp4* b, unsigned sx, unsigned sy, unsigned scale) {
350 | if(!scale) scale = 1;
351 | assert(d->width >= sx+b->width*scale && d->height >= sy+b->height*scale);
352 | unsigned x,xx,y,yd,ys,pitch;
353 | unsigned xscale,yscale;
354 | void* pixels;
355 | display_get_vram_and_pitch(d, &pixels, &pitch);
356 | pitch /= sizeof(unsigned);
357 | for(y=0,yd=sy*pitch, ys=0; y < b->height; y++,ys+=b->width)
358 | for(yscale=0; yscalewidth; x++)
360 | for(xscale = 0; xscale < scale; xscale++,xx++)
361 | ((unsigned*)pixels)[yd + xx] = rgba_to_argb(b->data[ys+x]);
362 | display_release_vram(d);
363 | //SDL_UpdateRect(d->surface, sx, sy, b->width*scale, b->height*scale);
364 | }
365 |
366 | static inline void display_draw_vline(display *d, unsigned sx, unsigned sy, unsigned height, unsigned color, unsigned scale) {
367 | void *pixels;
368 | if(!scale) scale = 1;
369 | assert(d->width >= sx && d->height >= sy+height*scale);
370 | unsigned y,yd,yscale,xscale,pitch;
371 | display_get_vram_and_pitch(d, &pixels, &pitch);
372 | pitch /= sizeof(unsigned);
373 | for(y = sy, yd=sy*pitch; y < sy+height; y++) for(yscale=0;yscalewidth >= sx+width*scale && d->height >= sy);
383 | unsigned x,yd,yscale,pitch;
384 | display_get_vram_and_pitch(d, &pixels, &pitch);
385 | pitch /= sizeof(unsigned);
386 | for(yscale = 0, yd=sy*pitch; yscale < scale; yscale++,yd+=pitch)
387 | for(x=sx;xwidth >= sx+width*scale && d->height >= sy+height*scale);
396 | unsigned x,xx,y,yd,pitch;
397 | unsigned xscale,yscale;
398 | display_get_vram_and_pitch(d, &pixels, &pitch);
399 | pitch /= sizeof(unsigned);
400 | for(y=0,yd=sy*pitch; y < height; y++)
401 | for(yscale=0; yscalehwscale),
414 | .y = SCALEUP(y, d->hwscale),
415 | .w = SCALEUP(w, d->hwscale),
416 | .h = SCALEUP(h, d->hwscale)};
417 | SDL_RenderCopy(d->ren, d->tex, &sarea, &darea);
418 | SDL_RenderPresent(d->ren);
419 | #else
420 | SDL_UpdateRect(d->surface, x, y, w, h);
421 | #endif
422 | }
423 |
424 | static inline void display_refresh(display *d) {
425 | display_update_region(d, 0, 0, d->width, d->height);
426 | }
427 |
428 | static inline void display_clear(display *d) {
429 | void *pixels;
430 | unsigned *ptr;
431 | unsigned pitch;
432 | display_get_vram_and_pitch(d, &pixels, &pitch);
433 | ptr = pixels;
434 | pitch /= sizeof(unsigned);
435 | unsigned x, y;
436 | for(y = 0; y < d->height; y++) for (x = 0; x < d->width; x++)
437 | ptr[y*pitch + x] = 0;
438 | display_release_vram(d);
439 | }
440 |
441 | static inline void display_toggle_fullscreen_i(display *d, int update) {
442 | #ifdef USE_SDL2
443 | SDL_SetWindowFullscreen(d->win, d->fs ? 0 : SDL_WINDOW_FULLSCREEN_DESKTOP);
444 | d->fs = !d->fs;
445 | #else
446 | d->fs = !d->fs;
447 | SDL_WM_ToggleFullScreen(d->surface);
448 | if(!update) return;
449 | SDL_Delay(1);
450 | //display_clear(d);
451 | SDL_UpdateRect(d->surface,0,0,d->width,d->height);
452 | SDL_Delay(1);
453 | #endif
454 | }
455 |
456 | static inline void display_toggle_fullscreen(display *d) {
457 | display_toggle_fullscreen_i(d, 1);
458 | }
459 |
460 |
461 | #if 0
462 | static inline void display_hline(display *d, unsigned color, unsigned x, unsigned y, unsigned len, unsigned scale) {
463 | unsigned y, x;
464 | }
465 | #endif
466 |
467 | enum eventtypes {
468 | EV_NONE = 0,
469 | EV_KEYDOWN,
470 | EV_KEYUP,
471 | EV_MOUSEMOVE,
472 | EV_MOUSEDOWN,
473 | EV_MOUSEUP,
474 | EV_MOUSEWHEEL,
475 | EV_JOYMOVE,
476 | EV_JOYDOWN,
477 | EV_JOYUP,
478 | EV_QUIT,
479 | EV_RESIZE,
480 | EV_NEEDREDRAW,
481 | EV_HANDLED,
482 | EV_MAX
483 | };
484 |
485 | enum cbtypes {
486 | CB_GAMETICK = 0,
487 | CB_KEYDOWN,
488 | CB_KEYUP,
489 | CB_MOUSEMOVE,
490 | CB_MOUSEDOWN,
491 | CB_MOUSEUP,
492 | CB_MOUSEWHEEL,
493 | CB_JOYMOVE,
494 | CB_JOYDOWN,
495 | CB_JOYUP,
496 | CB_TIMER,
497 | CB_RESIZE,
498 | CB_MAX
499 | };
500 |
501 | typedef struct event {
502 | int which, mod;
503 | int xval, yval;
504 | } ezevent;
505 |
506 | typedef int(*eventcallbackfunc)(void*data, struct event* ev);
507 | struct callback {
508 | void* data;
509 | eventcallbackfunc cb;
510 | };
511 |
512 | typedef struct inp {
513 | struct callback callbacks[CB_MAX];
514 | struct { int x, y; } mouse;
515 | } inp;
516 |
517 | static inline void inp_setcb(struct inp *in, enum cbtypes type, eventcallbackfunc cb, void* data) {
518 | in->callbacks[type].data = data;
519 | in->callbacks[type].cb = cb;
520 | }
521 |
522 | typedef void (*event_process_func)(struct inp* inp, SDL_Event* sdl_event, struct event *myevent);
523 | static void event_process_mousemove(struct inp* inp, SDL_Event* sdl_event, struct event *myevent) {
524 | myevent->xval = sdl_event->motion.x;
525 | myevent->yval = sdl_event->motion.y;
526 | inp->mouse.x = sdl_event->motion.x;
527 | inp->mouse.y = sdl_event->motion.y;
528 | }
529 |
530 | static void event_process_mousedown(struct inp* inp, SDL_Event* sdl_event, struct event *myevent) {
531 | myevent->xval = sdl_event->button.x;
532 | myevent->yval = sdl_event->button.y;
533 | myevent->which = sdl_event->button.button;
534 | inp->mouse.x = sdl_event->motion.x;
535 | inp->mouse.y = sdl_event->motion.y;
536 | }
537 |
538 | static void event_process_mousewheel(struct inp* inp, SDL_Event* sdl_event, struct event *myevent) {
539 | #ifdef USE_SDL2
540 | myevent->yval = sdl_event->wheel.y * -1;
541 | #else
542 | if(sdl_event->button.button == SDL_BUTTON_WHEELDOWN)
543 | myevent->yval = 1;
544 | else
545 | myevent->yval = -1;
546 | #endif
547 | }
548 |
549 | static void event_process_keydown(struct inp* inp, SDL_Event* sdl_event, struct event *myevent) {
550 | myevent->which = sdl_event->key.keysym.sym;
551 | myevent->mod = sdl_event->key.keysym.mod;
552 | }
553 |
554 | static void event_process_resize(struct inp* inp, SDL_Event* sdl_event, struct event *myevent) {
555 | #ifdef USE_SDL2
556 | myevent->xval = sdl_event->window.data1;
557 | myevent->yval = sdl_event->window.data2;
558 | #else
559 | myevent->xval = sdl_event->resize.w;
560 | myevent->yval = sdl_event->resize.h;
561 | #endif
562 | }
563 |
564 | static long long ezsdl_getutime64(void) {
565 | struct timeval t;
566 | gettimeofday(&t, NULL);
567 | return (t.tv_sec * 1000LL * 1000LL) + t.tv_usec;
568 | }
569 |
570 | static struct ezsdl {
571 | struct display disp;
572 | struct inp inp;
573 | } ezsdl;
574 |
575 | static inline void ezsdl_init(unsigned width, unsigned height, unsigned hwscale, int flags) {
576 | display_init(&ezsdl.disp, width, height, hwscale, flags);
577 | }
578 |
579 | static inline void ezsdl_shutdown(void) {
580 | display_shutdown(&ezsdl.disp);
581 | }
582 |
583 | static inline void ezsdl_set_resize_method(enum resize_method rm) {
584 | display_set_resize_method(&ezsdl.disp, rm);
585 | }
586 |
587 | static inline void ezsdl_set_title(const char* text) {
588 | #ifdef USE_SDL2
589 | SDL_SetWindowTitle(ezsdl.disp.win, text);
590 | #else
591 | SDL_WM_SetCaption(text, 0);
592 | #endif
593 | }
594 |
595 | static inline void ezsdl_draw(bmp4*b, unsigned x, unsigned y, int scale) {
596 | display_draw(&ezsdl.disp, b, x, y, scale);
597 | }
598 |
599 | static inline void ezsdl_draw_vline(unsigned sx, unsigned sy, unsigned height, unsigned color, unsigned scale) {
600 | display_draw_vline(&ezsdl.disp, sx, sy, height, color, scale);
601 | }
602 |
603 | static inline void ezsdl_draw_hline(unsigned sx, unsigned sy, unsigned width, unsigned color, unsigned scale) {
604 | display_draw_hline(&ezsdl.disp, sx, sy, width, color, scale);
605 | }
606 |
607 | static inline void ezsdl_fill_rect(unsigned sx, unsigned sy, unsigned width, unsigned height, unsigned color, unsigned scale) {
608 | display_fill_rect(&ezsdl.disp, sx, sy, width, height, color, scale);
609 | }
610 |
611 | static inline void ezsdl_draw_sprite(struct spritesheet* ss, unsigned sprite_no, unsigned x, unsigned y, unsigned scale) {
612 | display_draw_sprite(&ezsdl.disp, ss, sprite_no, x, y, scale);
613 | }
614 |
615 | static inline bmp4* ezsdl_get_screenshot(void) {
616 | return display_get_screenshot(&ezsdl.disp);
617 | }
618 |
619 | static inline void ezsdl_clear(void) {
620 | display_clear(&ezsdl.disp);
621 | }
622 |
623 | static inline void ezsdl_toggle_fullscreen(void) {
624 | display_toggle_fullscreen(&ezsdl.disp);
625 | }
626 |
627 | static inline void ezsdl_refresh(void) {
628 | display_refresh(&ezsdl.disp);
629 | }
630 |
631 | static inline void ezsdl_update_region(unsigned x, unsigned y, unsigned w, unsigned h) {
632 | display_update_region(&ezsdl.disp, x, y, w, h);
633 | }
634 |
635 | static inline void ezsdl_setcb(enum cbtypes type, eventcallbackfunc cb, void* data) {
636 | inp_setcb(&ezsdl.inp, type, cb, data);
637 | }
638 |
639 | static inline void ezsdl_sleep(unsigned ms) {
640 | SDL_Delay(ms);
641 | }
642 |
643 | static inline void ezsdl_get_vram_and_pitch(void **pixels, unsigned *pitch) {
644 | display_get_vram_and_pitch(&ezsdl.disp, pixels, pitch);
645 | }
646 |
647 | static inline void ezsdl_release_vram(void) {
648 | display_release_vram(&ezsdl.disp);
649 | }
650 |
651 | static inline unsigned ezsdl_get_width(void) {
652 | return display_get_width(&ezsdl.disp);
653 | }
654 |
655 | static inline unsigned ezsdl_get_height(void) {
656 | return display_get_height(&ezsdl.disp);
657 | }
658 |
659 |
660 | /* return event without blocking, call callbacks if so defined. */
661 | static inline enum eventtypes ezsdl_getevent(struct event *myevent) {
662 | static const event_process_func event_processors[CB_MAX] = {
663 | [CB_MOUSEMOVE] = event_process_mousemove,
664 | [CB_MOUSEDOWN] = event_process_mousedown,
665 | [CB_MOUSEWHEEL] = event_process_mousewheel,
666 | [CB_MOUSEUP] = event_process_mousedown,
667 | [CB_KEYDOWN] = event_process_keydown,
668 | [CB_KEYUP] = event_process_keydown,
669 | [CB_RESIZE] = event_process_resize,
670 | };
671 | struct inp *in = &ezsdl.inp;
672 | SDL_Event sdl_event;
673 | enum eventtypes e = EV_NONE;
674 | if (SDL_PollEvent(&sdl_event)) {
675 | enum cbtypes t = CB_MAX;
676 | switch (sdl_event.type) {
677 | case SDL_MOUSEMOTION:
678 | if(sdl_event.motion.x < 0) sdl_event.motion.x = 0;
679 | if(sdl_event.motion.y < 0) sdl_event.motion.y = 0;
680 | sdl_event.motion.x = SCALEDOWN(sdl_event.motion.x, ezsdl.disp.hwscale);
681 | sdl_event.motion.y = SCALEDOWN(sdl_event.motion.y, ezsdl.disp.hwscale);
682 | e = EV_MOUSEMOVE;
683 | t = CB_MOUSEMOVE;
684 | break;
685 | #ifdef USE_SDL2
686 | case SDL_MOUSEWHEEL:
687 | e = EV_MOUSEWHEEL;
688 | t = CB_MOUSEWHEEL;
689 | break;
690 | #endif
691 | case SDL_MOUSEBUTTONDOWN:
692 | #ifndef USE_SDL2
693 | if(sdl_event.button.button == SDL_BUTTON_WHEELDOWN ||
694 | sdl_event.button.button == SDL_BUTTON_WHEELUP) {
695 | e = EV_MOUSEWHEEL;
696 | t = CB_MOUSEWHEEL;
697 | } else
698 | #endif
699 | {
700 | sdl_event.button.x = SCALEDOWN(sdl_event.button.x, ezsdl.disp.hwscale);
701 | sdl_event.button.y = SCALEDOWN(sdl_event.button.y, ezsdl.disp.hwscale);
702 | e = EV_MOUSEDOWN;
703 | t = CB_MOUSEDOWN;
704 | }
705 | break;
706 | case SDL_MOUSEBUTTONUP:
707 | #ifndef USE_SDL2
708 | if(sdl_event.button.button == SDL_BUTTON_WHEELDOWN ||
709 | sdl_event.button.button == SDL_BUTTON_WHEELUP) {
710 | e = EV_MOUSEWHEEL;
711 | t = CB_MOUSEWHEEL;
712 | } else
713 | #endif
714 | {
715 | sdl_event.button.x = SCALEDOWN(sdl_event.button.x, ezsdl.disp.hwscale);
716 | sdl_event.button.y = SCALEDOWN(sdl_event.button.y, ezsdl.disp.hwscale);
717 | e = EV_MOUSEUP;
718 | t = CB_MOUSEUP;
719 | }
720 | break;
721 | case SDL_QUIT:
722 | return EV_QUIT;
723 | case SDL_KEYDOWN:
724 | if (sdl_event.key.keysym.sym == SDLK_RETURN &&
725 | (sdl_event.key.keysym.mod & KMOD_LALT) ||
726 | (sdl_event.key.keysym.mod & KMOD_RALT)) {
727 | ezsdl_toggle_fullscreen();
728 | SDL_Delay(1);
729 | e = EV_NEEDREDRAW;
730 | } else {
731 | e = EV_KEYDOWN;
732 | t = CB_KEYDOWN;
733 | }
734 | break;
735 | case SDL_KEYUP:
736 | e = EV_KEYUP;
737 | t = CB_KEYUP;
738 | break;
739 | #ifndef USE_SDL2
740 | case SDL_VIDEORESIZE:
741 | // TODO : SDL1 hw scaling
742 | display_init(&ezsdl.disp, sdl_event.resize.w, sdl_event.resize.w, 100, ezsdl.disp.flags);
743 | e = EV_RESIZE;
744 | t = CB_RESIZE;
745 | break;
746 | #else
747 | case SDL_WINDOWEVENT:
748 | if(sdl_event.window.event == SDL_WINDOWEVENT_RESIZED) {
749 | int s;
750 | if(ezsdl.disp.rm == RM_WINDOW) {
751 | s = ezsdl.disp.hwscale;
752 | sdl_event.window.data1 = SCALEDOWN(sdl_event.window.data1, s);
753 | sdl_event.window.data2 = SCALEDOWN(sdl_event.window.data2, s);
754 | } else {
755 | s = NEWSCALE(ezsdl.disp.width, sdl_event.window.data1, ezsdl.disp.height, sdl_event.window.data2);
756 | sdl_event.window.data1 = ezsdl.disp.width;
757 | sdl_event.window.data2 = ezsdl.disp.height;
758 | }
759 | display_init(&ezsdl.disp, sdl_event.window.data1, sdl_event.window.data2, s, ezsdl.disp.flags);
760 | e = EV_RESIZE;
761 | t = CB_RESIZE;
762 | } else if (sdl_event.window.event == SDL_WINDOWEVENT_EXPOSED) {
763 | e = EV_NEEDREDRAW;
764 | }
765 | break;
766 | #endif
767 | }
768 | if(t != CB_MAX) {
769 | if(event_processors[t]) event_processors[t](in, &sdl_event, myevent);
770 | if(in->callbacks[t].cb)
771 | if(in->callbacks[t].cb(in->callbacks[t].data, myevent))
772 | e = EV_NEEDREDRAW;
773 | else
774 | e = EV_HANDLED;
775 | }
776 | }
777 | return e;
778 | }
779 |
780 | static inline void ezsdl_start(void) {
781 | struct event myevent;
782 | while(1) {
783 | unsigned need_redraw = 0;
784 | unsigned ms_passed = 0;
785 | do {
786 | enum eventtypes e;
787 | while((e = ezsdl_getevent(&myevent)) != EV_NONE)
788 | if(e == EV_QUIT) return;
789 | else if(e == EV_NEEDREDRAW) need_redraw = 1;
790 | SDL_Delay(1);
791 | } while (++ms_passed < 20);
792 |
793 | long long tm = ezsdl_getutime64();
794 | myevent.which = need_redraw;
795 | struct inp* in = &ezsdl.inp;
796 | if(in->callbacks[CB_GAMETICK].cb)
797 | (void) in->callbacks[CB_GAMETICK].cb(in->callbacks[CB_GAMETICK].data, &myevent);
798 |
799 | ms_passed = (ezsdl_getutime64() - tm)/1000;
800 | }
801 | }
802 |
803 | #endif
804 |
--------------------------------------------------------------------------------
/sdlbook.c:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2019-202 rofl0r. see COPYING for LICENSE details.
3 | */
4 |
5 | #include
6 | #include
7 | #include
8 | #include
9 | #include
10 | #include
11 | #include
12 | #include
13 | #include
14 | #include
15 | #include
16 | #include "ezsdl.h"
17 | #include "topaz.h"
18 |
19 | #pragma RcB2 LINK "-ldjvulibre" "-lSSL" "-lmupdf"
20 |
21 | static struct config_data {
22 | int w, h;
23 | int scale;
24 | } config_data;
25 |
26 | enum be_type {
27 | BE_DJVU = 0,
28 | BE_MUPDF,
29 | };
30 | static struct doc {
31 | enum be_type be;
32 | union {
33 | struct djvu_doc {
34 | ddjvu_context_t *ctx;
35 | ddjvu_document_t *doc;
36 | } ddoc;
37 | struct pdf_doc {
38 | fz_context *ctx;
39 | fz_document *doc;
40 | } pdoc;
41 | } u;
42 | } doc;
43 |
44 | #define DDOC doc.u.ddoc
45 | #define PDOC doc.u.pdoc
46 | #define IS_DJVU (doc.be == BE_DJVU)
47 |
48 | static const char* filename;
49 | static int page_count, curr_page;
50 | static bmp4* bmp_font;
51 | static struct spritesheet ss_font;
52 | static unsigned long tickcounter;
53 | static int scroll_line_v;
54 | static int scroll_line_h;
55 | static const int fps = 64;
56 | static ddjvu_rect_t page_dims;
57 | static unsigned *image_data;
58 |
59 | static void update_title(void) {
60 | char buf[64];
61 | snprintf(buf, sizeof buf, "SDLBook [%d/%d] (%d%%) %s",
62 | curr_page, page_count, config_data.scale, filename);
63 | ezsdl_set_title(buf);
64 | }
65 |
66 |
67 | FILE* cfg_open(const char *fn, const char* mode) { return fopen(fn, mode ? mode : "r"); }
68 |
69 | #define cfg_close(F) do { fclose(F); F = 0; } while(0)
70 |
71 | char* cfg_getstr(FILE *f, const char *key, char* buf, size_t bufsize) {
72 | fseek(f, 0, SEEK_SET);
73 | size_t l = strlen(key);
74 | while(fgets(buf, bufsize, f)) {
75 | if(!strncmp(buf, key, l) && buf[l] == '=') {
76 | size_t x = l;
77 | while(buf[++x] && buf[x] != '\n');
78 | buf[x] = 0;
79 | memmove(buf, buf + l + 1, x - l);
80 | return buf;
81 | }
82 | }
83 | *buf = 0;
84 | return 0;
85 | }
86 |
87 | int cfg_getint(FILE *f, const char *key) {
88 | char buf[64];
89 | char *res = cfg_getstr(f, key, buf, sizeof buf);
90 | if(res) return atoi(res);
91 | return 0;
92 | }
93 |
94 | static void read_write_config(int doread) {
95 | FILE *config;
96 | char fbuf[512];
97 | snprintf(fbuf, sizeof fbuf, "%s/.sdlbook.cfg", getenv("HOME"));
98 |
99 | if(doread)
100 | config = cfg_open(fbuf, "r");
101 | else
102 | config = cfg_open(fbuf, "w");
103 |
104 | if(config) {
105 | if(doread) {
106 | config_data.w = cfg_getint(config, "w");
107 | config_data.h = cfg_getint(config, "h");
108 | config_data.scale = cfg_getint(config, "scale");
109 | } else {
110 | fprintf(config, "w=%d\nh=%d\nscale=%d\n",
111 | ezsdl_get_width(),
112 | ezsdl_get_height(),
113 | config_data.scale);
114 | }
115 | cfg_close(config);
116 | }
117 | if(doread) {
118 | if(!config_data.w) config_data.w = 640;
119 | if(!config_data.h) config_data.h = 480;
120 | if(!config_data.scale) config_data.scale = 100;
121 | }
122 | }
123 |
124 | static void handle(int);
125 | static int cleanup(void);
126 |
127 | static void die(const char *fmt, ...)
128 | {
129 | handle(FALSE);
130 | va_list args;
131 | va_start(args, fmt);
132 | vfprintf(stderr, fmt, args);
133 | va_end(args);
134 | fprintf(stderr,"\n");
135 | exit(cleanup());
136 | }
137 |
138 | #define FONT_W 8
139 | #define FONT_H 8
140 | static void init_gfx() {
141 | bmp_font = bmp4_new(128, 128);
142 | memcpy(bmp_font->data, topaz_font+8, 128*128*4);
143 | if(!spritesheet_init(&ss_font, bmp_font, FONT_W, FONT_H)) dprintf(2, "oops\n");
144 | }
145 |
146 | static int get_font_width(char letter) {
147 | return ss_font.sprite_w;
148 | }
149 |
150 | static unsigned get_font_render_length(const char* text) {
151 | unsigned x=0;
152 | for(;*text && *text != '\n';x+=get_font_width(*(text++)));
153 | return x;
154 | }
155 |
156 | static void draw_font(const char* text, struct spritesheet *font, unsigned x, unsigned y, unsigned scale) {
157 | for(;*text && *text != '\n';x+=scale*get_font_width(*(text++)))
158 | ezsdl_draw_sprite(font, *text, x, y, scale);
159 | }
160 |
161 | #ifndef MIN
162 | #define MIN(x, y) ((x) < (y) ? (x) : (y))
163 | #endif
164 |
165 | #ifndef MAX
166 | #define MAX(x, y) ((x) > (y) ? (x) : (y))
167 | #endif
168 |
169 | #define ISDIGIT(c) ((c) >= '0' && (c) <= '9')
170 |
171 | static inline unsigned get_image_pixel(int x, int y) {
172 | return image_data[y * page_dims.w + x];
173 | }
174 |
175 | static void draw() {
176 | int x, y, yline;
177 | void *pixels;
178 | unsigned *ptr;
179 | unsigned pitch;
180 | int xoff = MAX((int)(ezsdl_get_width() - page_dims.w)/2, 0);
181 | int xmax = page_dims.w, ymax = page_dims.h*2;
182 | if(scroll_line_v > ymax) return;
183 | ymax = MIN(ezsdl_get_height(), ymax-scroll_line_v),
184 | xmax = MIN(ezsdl_get_width(), xmax-scroll_line_h);
185 | ezsdl_get_vram_and_pitch(&pixels, &pitch);
186 | ptr = pixels;
187 | pitch/=4;
188 | for(y = 0; y < ymax; y++) {
189 | yline = y*pitch + xoff;
190 | for (x = 0; x < xmax; x++)
191 | ptr[yline + x] = get_image_pixel(x+scroll_line_h, y+scroll_line_v);
192 | }
193 | ezsdl_release_vram();
194 | }
195 |
196 | static void draw_borders() {
197 | int x, y, yline, xoff = MAX((int)(ezsdl_get_width() - page_dims.w)/2, 0);
198 | int ymax = ezsdl_get_height();
199 | if (!xoff) return;
200 | void* pixels;
201 | unsigned *vram, *ptr;
202 | unsigned pitch;
203 | ezsdl_get_vram_and_pitch(&pixels, &pitch);
204 | vram = pixels;
205 | pitch /= 4;
206 | for(y = 0, yline = 0; y < ymax; y++, yline+=pitch)
207 | for(x = 0, ptr=vram+yline; x < xoff; x++, ptr++)
208 | *ptr = ARGB(0,0,0);
209 | for(y = 0, yline = 0; y < ymax; y++, yline+=pitch)
210 | for(x = 0, ptr=vram+yline+xoff+page_dims.w; x < xoff; x++, ptr++)
211 | *ptr = ARGB(0,0,0);
212 | ezsdl_release_vram();
213 | }
214 |
215 | static void draw_bottom() {
216 | int x, y, yline;
217 | int xmax, ymax, ymin;
218 | ymin = page_dims.h*2-scroll_line_v;
219 | if(ymin < 0) return;
220 | void *pixels;
221 | unsigned *ptr;
222 | unsigned pitch;
223 | ezsdl_get_vram_and_pitch(&pixels, &pitch);
224 | ptr = pixels;
225 | pitch /= 4;
226 | int xoff = MAX((int)(ezsdl_get_width() - page_dims.w)/2, 0);
227 | ymax = ezsdl_get_height();
228 | xmax = MIN(ezsdl_get_width(), page_dims.w);
229 | for(y = ymin; y < ymax; y++) {
230 | yline = y*pitch + xoff;
231 | for (x = 0; x < xmax; x++)
232 | ptr[yline + x] = ARGB(0,0,0);
233 | }
234 | ezsdl_release_vram();
235 | }
236 |
237 |
238 | static int game_tick(int need_redraw) {
239 | long long ms_used = 0;
240 | if(need_redraw) {
241 | long long tstamp = ezsdl_getutime64();
242 | draw();
243 | if(need_redraw & 2) draw_borders();
244 | draw_bottom();
245 | ezsdl_refresh();
246 | ms_used = ezsdl_getutime64() - tstamp;
247 | }
248 | long sleepms = 1000/fps - ms_used;
249 | if(sleepms >= 0) ezsdl_sleep(sleepms);
250 |
251 | tickcounter++;
252 | return 0;
253 | }
254 |
255 | static unsigned* convert_rgb24_to_rgba(char* image, int w, int h, unsigned* new) {
256 | bmp3 in = {
257 | .width = w,
258 | .height = h,
259 | .data = image,
260 | };
261 | bmp4 out = {
262 | .width = w,
263 | .height = h,
264 | .data = new,
265 | };
266 | bmp3_to_bmp4(&in, &out);
267 | return new;
268 | }
269 |
270 | static void prepare_rect(ddjvu_rect_t *prect, ddjvu_rect_t* desired_rect,
271 | double iw, double ih, int dpi)
272 | {
273 | int enforce_aspect_ratio = 1;
274 |
275 | prect->x = 0;
276 | prect->y = 0;
277 | if (desired_rect) {
278 | prect->w = desired_rect->w;
279 | prect->h = desired_rect->h;
280 | enforce_aspect_ratio = 0;
281 | } else if (config_data.scale > 0) {
282 | prect->w = (unsigned int) (iw * (double)config_data.scale) / dpi;
283 | prect->h = (unsigned int) (ih * (double)config_data.scale) / dpi;
284 | } else {
285 | prect->w = (iw * 100) / dpi;
286 | prect->h = (ih * 100) / dpi;
287 | }
288 | if (enforce_aspect_ratio) {
289 | double dw = iw / prect->w;
290 | double dh = ih / prect->h;
291 | if (dw > dh)
292 | prect->h = (int)(ih / dw);
293 | else
294 | prect->w = (int)(iw / dh);
295 | }
296 | }
297 |
298 | static void* render_pdf_page(int pageno, ddjvu_rect_t *res_rect, ddjvu_rect_t *desired_rect)
299 | {
300 | ddjvu_rect_t prect;
301 | /* mupdf platform/x11/pdfapp.c */
302 | fz_page *page;
303 | fz_rect bounds;
304 | fz_try(PDOC.ctx) {
305 | page = fz_load_page(PDOC.ctx, PDOC.doc, pageno);
306 | bounds = fz_bound_page(PDOC.ctx, page);
307 | }
308 | fz_catch(PDOC.ctx) {
309 | die("failed to load page %d\n", pageno);
310 | }
311 |
312 | double iw = bounds.x1 - bounds.x0;
313 | double ih = bounds.y1 - bounds.y0;
314 | int dpi = 72;
315 |
316 | prepare_rect(&prect, desired_rect, iw, ih, dpi);
317 |
318 | int rowsize = prect.w * 3;
319 | void *image;
320 | if(!(image = malloc(rowsize * prect.h)))
321 | die("Cannot allocate image buffer for page %d", pageno);
322 |
323 | fz_matrix ctm;
324 | fz_pixmap *pix;
325 |
326 | ctm = fz_scale((double) prect.w / iw, (double) prect.h / ih);
327 | pix = fz_new_pixmap_from_page(PDOC.ctx, page, ctm, fz_device_rgb(PDOC.ctx), 0);
328 |
329 | fz_drop_page(PDOC.ctx, page);
330 |
331 | if (!pix) {
332 | free(image);
333 | return NULL;
334 | }
335 | assert(pix->w >= prect.w && pix->h >= prect.h);
336 |
337 | unsigned char* out, *s;
338 | unsigned x,y,xn;
339 | for (y = 0, out = image; y < prect.h; y++) {
340 | s = &pix->samples[y * pix->stride];
341 | xn = 0;
342 | for (x = 0; x < prect.w; x++, xn += pix->n) {
343 | *(out++) = s[xn + 0];
344 | *(out++) = s[xn + 1];
345 | *(out++) = s[xn + 2];
346 | }
347 | }
348 | fz_drop_pixmap(PDOC.ctx, pix);
349 |
350 | *res_rect = prect;
351 | return image;
352 | }
353 |
354 |
355 | static char* render_page(ddjvu_page_t *page, int pageno, ddjvu_rect_t *res_rect, ddjvu_rect_t *desired_rect)
356 | {
357 | ddjvu_rect_t prect; // pixels of image
358 | ddjvu_rect_t rrect; // pixels of segment (info_segment)
359 | ddjvu_format_style_t style;
360 | ddjvu_render_mode_t mode;
361 | ddjvu_format_t *fmt;
362 | int iw = ddjvu_page_get_width(page);
363 | int ih = ddjvu_page_get_height(page);
364 | int dpi = ddjvu_page_get_resolution(page);
365 | ddjvu_page_type_t type = ddjvu_page_get_type(page);
366 | char *image = 0;
367 | char white = 0xFF;
368 | int rowsize;
369 |
370 | prepare_rect(&prect, desired_rect, iw, ih, dpi);
371 |
372 | rrect = prect;
373 | #if 0
374 | // show only section/segment of rendered image
375 | if (segment_given > 0) {
376 | rrect = segment_rect;
377 | if (rrect.x < 0)
378 | rrect.x = prect.w - rrect.w + rrect.x;
379 | if (rrect.y < 0)
380 | rrect.y = prect.h - rrect.h + rrect.y;
381 | }
382 | #endif
383 |
384 | mode = DDJVU_RENDER_COLOR;
385 | style = DDJVU_FORMAT_RGB24;
386 |
387 | if (!(fmt = ddjvu_format_create(style, 0, 0)))
388 | die("Cannot determine pixel style for page %d", pageno);
389 |
390 | ddjvu_format_set_row_order(fmt, 1);
391 |
392 | if (style == DDJVU_FORMAT_MSBTOLSB) {
393 | rowsize = (rrect.w + 7) / 8;
394 | white = 0;
395 | } else if (style == DDJVU_FORMAT_GREY8)
396 | rowsize = rrect.w;
397 | else
398 | rowsize = rrect.w * 3;
399 | if(!(image = malloc(rowsize * rrect.h)))
400 | die("Cannot allocate image buffer for page %d", pageno);
401 |
402 | /* fill image with white in case rendering fails */
403 | if(!ddjvu_page_render(page, mode, &prect, &rrect, fmt, rowsize, image))
404 | memset(image, white, rowsize * rrect.h);
405 |
406 | ddjvu_format_release(fmt);
407 | *res_rect = rrect;
408 | return image;
409 | }
410 |
411 | static void* prep_page(int pageno, ddjvu_rect_t *res_rect, ddjvu_rect_t *desired_rect)
412 | {
413 | if(pageno >= page_count) return 0;
414 | if(!IS_DJVU)
415 | return render_pdf_page(pageno, res_rect, desired_rect);
416 |
417 | ddjvu_page_t *page;
418 | if (!(page = ddjvu_page_create_by_pageno(DDOC.doc, pageno)))
419 | die("Can't access page %d.", pageno);
420 | while (! ddjvu_page_decoding_done(page))
421 | handle(TRUE);
422 | if (ddjvu_page_decoding_error(page)) {
423 | handle(FALSE);
424 | die("Can't decode page %d", pageno);
425 | }
426 | void *image = render_page(page, pageno, res_rect, desired_rect);
427 | ddjvu_page_release(page);
428 | return image;
429 | }
430 |
431 | static void* prep_pages(int *need_redraw) {
432 | ddjvu_rect_t p1rect, p2rect;
433 | static int last_page = -1, last_scale = -1;
434 | if(curr_page == last_page && last_scale == config_data.scale)
435 | return image_data;
436 | last_page = curr_page;
437 | last_scale = config_data.scale;
438 | if(need_redraw) *need_redraw = 1;
439 | char *p1data = prep_page(curr_page, &p1rect, 0);
440 | char *p2data = prep_page(curr_page+1, &p2rect, 0);
441 | if(!p2data) {
442 | /* probably last page hit */
443 | p2data = calloc(3,p1rect.w*p1rect.h);
444 | p2rect = p1rect;
445 | }
446 | if(p1rect.w != p2rect.w || p1rect.h != p2rect.h) {
447 | /* sometimes the start page of a book has a different format */
448 | free(p1data);
449 | p1data = prep_page(curr_page, &p1rect, &p2rect);
450 | if(need_redraw) *need_redraw = 2;
451 | }
452 | if(!(p1data && p2data)) return NULL;
453 | assert(p1rect.w == p2rect.w && p1rect.h == p2rect.h);
454 |
455 | size_t one_pic = p1rect.w*p1rect.h;
456 | unsigned* imgbuf = malloc(4 * one_pic * 2);
457 |
458 | convert_rgb24_to_rgba(p1data, p1rect.w, p1rect.h, imgbuf);
459 | convert_rgb24_to_rgba(p2data, p2rect.w, p2rect.h, imgbuf+one_pic);
460 | page_dims = p1rect;
461 | free(p1data);
462 | free(p2data);
463 | return imgbuf;
464 | }
465 |
466 | static void handle(int wait) {
467 | const ddjvu_message_t *msg;
468 | if (!IS_DJVU || !DDOC.ctx)
469 | return;
470 | if (wait)
471 | msg = ddjvu_message_wait(DDOC.ctx);
472 | while ((msg = ddjvu_message_peek(DDOC.ctx))) {
473 | switch(msg->m_any.tag) {
474 | case DDJVU_ERROR:
475 | fprintf(stderr,"ddjvu: %s\n", msg->m_error.message);
476 | if (msg->m_error.filename)
477 | fprintf(stderr,"ddjvu: '%s:%d'\n",
478 | msg->m_error.filename, msg->m_error.lineno);
479 | default:
480 | break;
481 | }
482 | ddjvu_message_pop(DDOC.ctx);
483 | }
484 | }
485 |
486 | static void swap_image(void *new) {
487 | void *old = image_data;
488 | if(old == new) return;
489 | image_data = new;
490 | free(old);
491 | }
492 |
493 | static int set_page(int no) {
494 | int need_redraw;
495 | if(no >= page_count) no = page_count-1;
496 | if(no < 0) curr_page = 0;
497 | else curr_page = no;
498 | swap_image(prep_pages(&need_redraw));
499 | update_title();
500 | return need_redraw;
501 | }
502 |
503 | static int change_page(int incr) {
504 | int need_redraw;
505 | if(curr_page + incr < 0)
506 | curr_page = 0;
507 | else if(curr_page + incr >= page_count)
508 | curr_page = page_count -1;
509 | else
510 | curr_page += incr;
511 | swap_image(prep_pages(&need_redraw));
512 | update_title();
513 | return need_redraw;
514 | }
515 |
516 | static int change_scale(int incr) {
517 | int need_redraw;
518 | if (config_data.scale + incr <= 999 && config_data.scale + incr > 0)
519 | config_data.scale += incr;
520 | else return 0;
521 | swap_image(prep_pages(&need_redraw));
522 | update_title();
523 | if(scroll_line_h + ezsdl_get_width() > page_dims.w) {
524 | scroll_line_h = MAX((int)(page_dims.w - ezsdl_get_width()), 0);
525 | return 2;
526 | }
527 | return incr < 0 ? 2 : need_redraw;
528 | }
529 |
530 | static int change_scroll_v(int incr) {
531 | int ph = page_dims.h;
532 | scroll_line_v += incr;
533 | int pages = scroll_line_v / ph;
534 | if(scroll_line_v < 0) --pages;
535 | scroll_line_v -= pages * ph;
536 | if(curr_page + pages < 0) {
537 | pages = -curr_page;
538 | scroll_line_v = 0;
539 | } else if(curr_page + pages >= page_count-1) {
540 | pages = page_count-1 - curr_page;
541 | scroll_line_v = MIN(scroll_line_v, abs(ph - ezsdl_get_height()));
542 | }
543 | if(pages) change_page(pages);
544 | if(scroll_line_v < 0) scroll_line_v += ph;
545 | return 1;
546 | }
547 |
548 | static int change_scroll_h(int incr) {
549 | int sw = ezsdl_get_width(), pw = page_dims.w;
550 | int old_scroll = scroll_line_h;
551 | if (scroll_line_h + incr <= 0)
552 | scroll_line_h = 0;
553 | else if(incr < 0 || (sw <= pw && scroll_line_h + incr <= pw - sw))
554 | scroll_line_h += incr;
555 | return old_scroll != scroll_line_h;
556 | }
557 |
558 | #define HELP_TEXT \
559 | "HELP SCREEN - HIT ANY KEY TO EXIT\n" \
560 | "UP, DOWN ARROW - SCROLL 32 PIX\n" \
561 | "CTRL + UP, DOWN ARROW - SCROLL 96 PIX\n" \
562 | "PAGE_UP/DOWN - SCROLL ONE PAGE\n" \
563 | "KEYPAD +/- OR CTRL-WHEEL - ZOOM\n" \
564 | "G - ENTER PAGE NUMBER\n" \
565 | "Q/ESC - QUIT\n" \
566 | "F1 - SHOW HELP SCREEN\n"
567 |
568 | static int get_return_count(const char* text) {
569 | int count = 0;
570 | while(*text) if(*(text++) == '\n') count++;
571 | return count;
572 | }
573 |
574 | static void draw_font_lines(const char* text, struct spritesheet *font,
575 | unsigned x, unsigned y, unsigned scale)
576 | {
577 | const char *p = text;
578 | unsigned yy = y;
579 | do {
580 | draw_font(p, font, x, yy, scale);
581 | yy += 10*2;
582 | } while((p = strchr(p, '\n')), p++);
583 | }
584 | enum input_flags {
585 | INPUT_LOOP_RET,
586 | INPUT_LOOP_NUMERIC
587 | };
588 |
589 | static void input_loop(const char* title, char *result, enum input_flags flags)
590 | {
591 | int ret_count = get_return_count(title);
592 | if(!ret_count) ret_count = 1;
593 | int desired_height = (ret_count+2) * 10 * 2;
594 | ezsdl_fill_rect(0,0, ezsdl_get_width(), MIN(desired_height, ezsdl_get_height()), RGB(0xff,0x00,0x00), 1);
595 | draw_font_lines(title, &ss_font, 8, 8, 2);
596 | ezsdl_update_region(0, 0, ezsdl_get_width(), MIN(desired_height, ezsdl_get_height()));
597 | char* p = result;
598 | *p = 0;
599 | struct event event;
600 | while(1) {
601 | enum eventtypes e;
602 | while((e = ezsdl_getevent(&event)) == EV_NONE) ezsdl_sleep(1);
603 | switch(e) {
604 | case EV_QUIT:
605 | case EV_KEYUP:
606 | switch(event.which) {
607 | case SDLK_BACKSPACE:
608 | if(p > result) p--;
609 | *p = 0;
610 | goto drawit;
611 | case SDLK_RETURN: case SDLK_ESCAPE:
612 | out:;
613 | *p = 0;
614 | ezsdl_clear();
615 | return;
616 | default:
617 | if(flags == INPUT_LOOP_RET)
618 | goto out;
619 | else if(flags == INPUT_LOOP_NUMERIC && ISDIGIT(event.which) && (p - result < 20)) {
620 | *(p++) = event.which;
621 | *p = 0;
622 | }
623 | drawit:
624 | ezsdl_fill_rect(8, desired_height - 10*2, ezsdl_get_width() -8, MIN(desired_height, ezsdl_get_height()), RGB(0xff,0x00,0x00), 1);
625 | draw_font(result, &ss_font, 8, desired_height - 10*2, 2);
626 | ezsdl_update_region(0, 0, ezsdl_get_width(), MIN(desired_height, ezsdl_get_height()));
627 | break;
628 | }
629 | break;
630 | default: break;
631 | }
632 | }
633 | }
634 |
635 | static void djvu_cleanup(void) {
636 | if(!IS_DJVU) return;
637 | if(DDOC.doc)
638 | ddjvu_document_release(DDOC.doc);
639 | if(DDOC.ctx)
640 | ddjvu_context_release(DDOC.ctx);
641 | }
642 |
643 | static void pdf_cleanup(void) {
644 | if(IS_DJVU) return;
645 | if(PDOC.doc)
646 | fz_drop_document(PDOC.ctx, PDOC.doc);
647 | if(PDOC.ctx)
648 | fz_drop_context(PDOC.ctx);
649 | }
650 |
651 | static int cleanup(void) {
652 | djvu_cleanup();
653 | pdf_cleanup();
654 |
655 | read_write_config(0);
656 |
657 | ezsdl_shutdown();
658 |
659 | return 1;
660 | }
661 |
662 | static int open_djvu(char* app, const char *fn) {
663 | doc.be = BE_DJVU;
664 | if(!(DDOC.ctx = ddjvu_context_create(app)))
665 | return 0;
666 | if(!(DDOC.doc = ddjvu_document_create_by_filename(DDOC.ctx, fn, TRUE))) {
667 | djvu_cleanup();
668 | return 0;
669 | }
670 | return 1;
671 | }
672 |
673 | static void decode_doc(void) {
674 | if(IS_DJVU) {
675 | while (! ddjvu_document_decoding_done(DDOC.doc))
676 | handle(TRUE);
677 |
678 | if (ddjvu_document_decoding_error(DDOC.doc))
679 | die("can't decode document");
680 | }
681 | }
682 |
683 | static int open_pdf(const char *fn) {
684 | doc.be = BE_MUPDF;
685 | PDOC.ctx = fz_new_context(NULL, NULL, FZ_STORE_DEFAULT);
686 | fz_register_document_handlers(PDOC.ctx);
687 | fz_try (PDOC.ctx) {
688 | PDOC.doc = fz_open_document(PDOC.ctx, fn);
689 | } fz_catch (PDOC.ctx) {
690 | fz_drop_context(PDOC.ctx);
691 | PDOC.ctx = 0;
692 | return 0;
693 | }
694 | return 1;
695 | }
696 |
697 | int main(int argc, char **argv) {
698 | if(argc != 2)
699 | die("need djvu filename as argv[1]");
700 |
701 | filename = argv[1];
702 |
703 | char *p = strrchr(filename, '.');
704 | if(p && !strcasecmp(p, ".djvu")) {
705 | if(!open_djvu(argv[0], filename))
706 | die("can't open djvu document '%s'", filename);
707 | } else if(!open_pdf(filename))
708 | die("can't open mupdf document '%s'", filename);
709 |
710 | decode_doc();
711 |
712 | if(strrchr(filename, '/')) filename = strrchr(filename, '/')+1;
713 |
714 |
715 | page_count = IS_DJVU ?
716 | ddjvu_document_get_pagenum(DDOC.doc) :
717 | fz_count_pages(PDOC.ctx, PDOC.doc);
718 |
719 | curr_page = 0;
720 |
721 | config_data.scale = 100;
722 |
723 | read_write_config(1);
724 |
725 | ezsdl_init(config_data.w, config_data.h, 100,
726 | #ifndef USE_SDL2
727 | SDL_HWPALETTE | SDL_RESIZABLE
728 | #else
729 | 0
730 | #endif
731 | );
732 | ezsdl_set_resize_method(RM_WINDOW);
733 |
734 | image_data = prep_pages(NULL);
735 |
736 | init_gfx();
737 |
738 | SDL_ShowCursor(1);
739 | #ifndef USE_SDL2
740 | SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
741 | #endif
742 | struct event event;
743 |
744 | update_title();
745 |
746 | game_tick(1);
747 |
748 | unsigned left_ctrl_pressed = 0;
749 | unsigned right_ctrl_pressed = 0;
750 | unsigned mb_left_down = 0;
751 | unsigned mouse_y = 0;
752 | unsigned mouse_x = 0;
753 |
754 | while(1) {
755 | unsigned need_redraw = 0;
756 | int scroll_dist_v = 0;
757 | int scroll_dist_h = 0;
758 | int scale_dist = 0;
759 | enum eventtypes e;
760 | while((e = ezsdl_getevent(&event)) != EV_NONE) {
761 | need_redraw = 0;
762 | switch (e) {
763 | case EV_MOUSEDOWN:
764 | if(event.which == SDL_BUTTON_LEFT) mb_left_down = 1;
765 | break;
766 | case EV_MOUSEUP:
767 | if(event.which == SDL_BUTTON_LEFT) mb_left_down = 0;
768 | break;
769 | case EV_MOUSEMOVE:
770 | if(mb_left_down && mouse_y != event.yval)
771 | scroll_dist_v += mouse_y-event.yval;
772 | if(mb_left_down && mouse_x != event.xval)
773 | scroll_dist_h += mouse_x-event.xval;
774 | mouse_y = event.yval;
775 | mouse_x = event.xval;
776 | break;
777 | case EV_MOUSEWHEEL:
778 | if(left_ctrl_pressed || right_ctrl_pressed)
779 | scale_dist += event.yval*-10;
780 | else
781 | scroll_dist_v += event.yval*64;
782 | break;
783 | case EV_NEEDREDRAW: case EV_RESIZE:
784 | need_redraw = 1;
785 | break;
786 | case EV_QUIT:
787 | goto dun_goofed;
788 | case EV_KEYDOWN:
789 | switch(event.which) {
790 | case SDLK_LCTRL:
791 | left_ctrl_pressed = 1;
792 | break;
793 | case SDLK_RCTRL:
794 | right_ctrl_pressed = 1;
795 | break;
796 | case SDLK_q:
797 | goto dun_goofed;
798 | case SDLK_KP_PLUS:
799 | need_redraw = change_scale(+10);
800 | break;
801 | case SDLK_KP_MINUS:
802 | need_redraw = change_scale(-10);
803 | break;
804 | case SDLK_PAGEDOWN:
805 | scroll_dist_v += page_dims.h;
806 | break;
807 | case SDLK_PAGEUP:
808 | scroll_dist_v -= page_dims.h;
809 | break;
810 | case SDLK_UP:
811 | if((event.mod & KMOD_LCTRL) || (event.mod & KMOD_RCTRL))
812 | scroll_dist_v += -96;
813 | else
814 | scroll_dist_v += -32;
815 | break;
816 | case SDLK_DOWN:
817 | if((event.mod & KMOD_LCTRL) || (event.mod & KMOD_RCTRL))
818 | scroll_dist_v += +96;
819 | else
820 | scroll_dist_v += +32;
821 | break;
822 | case SDLK_LEFT:
823 | if((event.mod & KMOD_LCTRL) || (event.mod & KMOD_RCTRL))
824 | need_redraw = change_scroll_h(-96);
825 | else
826 | need_redraw = change_scroll_h(-32);
827 | break;
828 | case SDLK_RIGHT:
829 | if((event.mod & KMOD_LCTRL) || (event.mod & KMOD_RCTRL))
830 | need_redraw = change_scroll_h(+96);
831 | else
832 | need_redraw = change_scroll_h(+32);
833 | break;
834 | case SDLK_RETURN:
835 | if((event.mod & KMOD_LALT) ||
836 | (event.mod & KMOD_RALT)) {
837 | ezsdl_toggle_fullscreen();
838 | ezsdl_sleep(1);
839 | game_tick(1);
840 | need_redraw = 1;
841 | }
842 | break;
843 | default:
844 | break;
845 | }
846 | break;
847 | case EV_KEYUP:
848 | switch(event.which) {
849 | case SDLK_UP:
850 | case SDLK_DOWN:
851 | case SDLK_LEFT:
852 | case SDLK_RIGHT:
853 | case SDLK_PAGEUP:
854 | case SDLK_PAGEDOWN:
855 | need_redraw = 1;
856 | break;
857 | case SDLK_LCTRL:
858 | left_ctrl_pressed = 0;
859 | break;
860 | case SDLK_RCTRL:
861 | right_ctrl_pressed = 0;
862 | break;
863 | case SDLK_F1:
864 | {
865 | char buf[32];
866 | buf[0] = 0;
867 | input_loop(HELP_TEXT, buf, INPUT_LOOP_RET);
868 | need_redraw = 1;
869 | }
870 | break;
871 |
872 | case SDLK_g:
873 | {
874 | char buf[32];
875 | buf[0] = 0;
876 | input_loop("enter page no", buf, INPUT_LOOP_NUMERIC);
877 | if(*buf) need_redraw = set_page(atoi(buf));
878 | else need_redraw = 1;
879 | }
880 | break;
881 | case SDLK_c:
882 | ezsdl_clear();
883 | ezsdl_refresh();
884 | need_redraw = 1;
885 | break;
886 | case SDLK_ESCAPE:
887 | goto dun_goofed;
888 | default:
889 | break;
890 | }
891 | default:
892 | break;
893 | }
894 | if(need_redraw) game_tick(need_redraw);
895 | }
896 | if(scroll_dist_v) need_redraw |= change_scroll_v(scroll_dist_v);
897 | if(scroll_dist_h) need_redraw |= change_scroll_h(scroll_dist_h);
898 | if(scale_dist) need_redraw |= change_scale(scale_dist);
899 |
900 | if(game_tick(need_redraw)) {
901 | }
902 | }
903 |
904 | dun_goofed:
905 |
906 | cleanup();
907 | return 0;
908 | }
909 |
--------------------------------------------------------------------------------