aboutsummaryrefslogtreecommitdiff
path: root/misc.c (follow)
Commit message (Collapse)AuthorAge
* Rename memswap() to swap_regions(). Consolidate duplicate implementations.Franklin Wei2024-07-31
| | | | | | C99 reserves the mem* namespace for future expansion. Some Rockbox targets had issues with memswap() conflicting with another definition, so fix that.
* move_cursor(): handle visible flag; return useful valueBen Harris2023-08-09
| | | | | | | | | | | | | | | | | This adds an extra parameter to move_cursor() that's an optional pointer to a bool indicating whether the cursor is visible. This allows for centralising the common idiom of having the keyboard cursor become visible when a cursor key is pressed. Consistently with the vast majority of existing puzzles, the cursor moves even if it was invisible before, and becomes visible even if it can't move. The function now also returns one of the special constants that can be returned by interpret_move(), so that the caller can correctly return MOVE_UI_UPDATE or MOVE_NO_EFFECT without needing to carefully check for changes itself. Callers are updated only to the extent that they all pass NULL as the new argument. Most of them could now be substantially simplified.
* Move mul_root3 out into misc.c and generalise it.Simon Tatham2023-07-07
| | | | I'm going to want to reuse it for sqrt(5) as well as sqrt(3) soon.
* Add MOVE_NO_EFFECT and MOVE_UNUSED return values from interpret_move()Ben Harris2023-06-11
| | | | | | | | These allow for distinguishing the case where a puzzle doesn't have a use for a key from the case where it just happens to have no effect in the current state of the puzzle. These were both represented by NULL, but that now represents the case where a puzzle hasn't been updated to make the distinction yet.
* Rename UI_UPDATE as MOVE_UI_UPDATEBen Harris2023-06-11
| | | | | | | | All the other constants named UI_* are special key names that can be passed to midend_process_key(), but UI_UPDATE is a special return value from the back-end interpret_move() function instead. This renaming makes the distinction clear and provides a naming convention for future special return values from interpret_move().
* make_prefs_path(): tolerate NULL inputs.Simon Tatham2023-04-23
| | | | | | | | Just noticed that if prefs_dir() returns NULL, we'll already have passed it to this function before the calling functions get round to checking. I think it's less wordy all round to make this helper function propagate NULL than to mess about with lots of extra if statements in between all the calls.
* Support preferences in the GTK frontend.Simon Tatham2023-04-23
| | | | | | | | | | | | | | | | | | | Finally, some user-visible behaviour changes as a payoff for all that preparation work! In this commit, the GTK puzzles get a 'Preferences' option in the menu, which presents a dialog box to configure the preference settings. On closing that dialog box, the puzzle preferences are enacted immediately, and also saved to a configuration file where the next run of the same puzzle will reload them. The default file location is ~/.config/sgt-puzzles/<puzzlename>.conf, although you can override the .config dir via $XDG_CONFIG_HOME or override the Puzzles-specific subdir with $SGT_PUZZLES_DIR. This is the first commit that actually exposes all the new preferences work to the user, and therefore, I've also added documentation of all the current preference options.
* Move per-puzzle ad-hoc getenv preferences into game_ui.Simon Tatham2023-04-23
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Environment variables that set specific settings of particular puzzles, such as SLANT_SWAP_BUTTONS, LIGHTUP_LIT_BLOBS and LOOPY_AUTOFOLLOW, now all affect the game behaviour via fields in game_ui instead of being looked up by getenv in the individual functions that need to know them. The purpose of this refactoring is to put those config fields in a place where other more user-friendly configuration systems will also be able to access them, once I introduce one. However, for the moment, there's no functional change: I haven't _yet_ changed how the user sets those options. They're still set by environment variables alone. All I'm changing here is where the settings are stored inside the code, and exactly when they're read out of the environment to put into the game_ui. Specifically, the getenvs now happen during new_ui(). Or rather, in all the puzzles I've changed here, they happen in a subroutine legacy_prefs_override() called from within new_ui(), after it's set up the default values for the settings, and then gives the environment a chance to override them. Or rather, legacy_prefs_override() only actually calls getenv the first time, and after that, it's cached the answers it got. In order to make the override functions less wordy, I've altered the prototype of getenv_bool so that it returns an int rather than a bool, and takes its default return value in the same form. That way you can set the default to something other than 0 or 1, and find out whether a value was present at all. This commit only touches environment configuration specific to an individual puzzle. The midend also has some standard environment-based config options that apply to all puzzles, such as colour scheme and default presets and preset-menu extension. I haven't handled those yet.
* Fall back to <math.h> if <tgmath.h> doesn't work.Simon Tatham2023-04-06
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This fixes a build failure introduced by commit 2e48ce132e011e8 yesterday. When I saw that commit I expected the most likely problem would be in the NestedVM build, which is currently the thing with the most most out-of-date C implementation. And indeed the NestedVM toolchain doesn't have <tgmath.h> - but much more surprisingly, our _Windows_ builds failed too, with a compile error inside <tgmath.h> itself! I haven't looked closely into the problem yet. Our Windows builds are done with clang, which comes with its own <tgmath.h> superseding the standard Windows one. So you'd _hope_ that clang could make sense of its own header! But perhaps the problem is that this is an unusual compile mode and hasn't been tested. My fix is to simply add a cmake check for <tgmath.h> - which doesn't just check the file's existence, it actually tries compiling a file that #includes it, so it will detect 'file exists but is mysteriously broken' just as easily as 'not there at all'. So this makes the builds start working again, precisely on Ben's theory of opportunistically using <tgmath.h> where possible and falling back to <math.h> otherwise. It looks ugly, though! I'm half tempted to make a new header file whose job is to include a standard set of system headers, just so that that nasty #ifdef doesn't have to sit at the top of almost all the source files. But for the moment this at least gets the build working again.
* Replace <math.h> with <tgmath.h> throughoutBen Harris2023-04-04
| | | | | | | | | | | | | | | C89 provided only double-precision mathematical functions (sin() etc), and so despite using single-precision elsewhere, those are what Puzzles has traditionally used. C99 introduced single-precision equivalents (sinf() etc), and I hope it's been long enough that we can safely use them. Maybe they'll even be faster. Rather than directly use the single-precision functions, though, we use the magic macros from <tgmath.h> that automatically choose the precision of mathematical functions based on their arguments. This has the advantage that we only need to change which header we include, and thus that we can switch back again if some platform has trouble with the new header.
* Treat environment variable values beginning with "T" as trueBen Harris2023-03-22
| | | | | | So a value is true iff it begins with 'T', 't', 'Y', or 'y'. This is mostly so that naively converting JSON "true" to a string will work properly, but it should keep LISPers happy too.
* New shared function, getenv_bool()Ben Harris2023-03-22
| | | | | | | | | | | | | This provides a standard way to get a boolean from an environment variable. It treats the variable as true iff its value begins with 'y' or 'Y', like most of the current implementations. The function takes a default value which it returns if the environment variable is undefined. This replaces the various ad-hoc tests of environment variable scattered around and mostly doesn't change their behaviour. The exceptions are TOWERS_2D in Towers and DEBUG_PUZZLES in the Windows front end. Both of those were treated as true if they were defined at all, but now follow the same rules as other boolean environment variables.
* Expose colour_mix() to backends (and others)Ben Harris2022-12-29
| | | | | | | | Quite a few backends currently generate colours by multiplying the foreground colour by a fraction, effectively mixing it with black. On a black background, this might be reasonably replaced by mixing the background colour with white, but that's rather arithmetically fiddly. Happily, I already have a function for that and just need to expose it.
* Rewrite mkhighlight to handle a wider range of coloursBen Harris2022-12-14
| | | | | | | | | | | | | | Before, it worked by scaling all the colour components, which worked reasonably for colours in the vicinity of white, but failed badly on a black background. Now it generates the highlight and lowlight colours by mixing the background colour with white and black respectively. Where there isn't enough headroom, it adjusts the background by mixing in a negative amount of white or black, which makes sense geometrically if not in paint. There is still a problem that green and yellow colours don't end up with bright enough highlights. I think this is because the code doesn't understand that green is brighter than red or blue.
* mkhighlight: Don't darken the base colour if we don't need a highlightBen Harris2022-12-12
| | | | | | | | | | | The "mkhighlight" functions can now take -1 as the requested highlight or lowlight colour as an indication that no such colour should be returned. If the caller doesn't want a highlight colour, there's not much point in darkening the base colour to make it distinct from the highlight, so don't do that. This means that the background colours in Palisade and Untangle are no longer unnecessarily darkened.
* Adopt C99 bool in misc.c functions.Simon Tatham2018-11-13
| | | | | The 'decode' flag to obfuscate_bitmap and the 'wrap' flag to move_cursor are the only ones affected here.
* misc.c: Fix implementation of free_keys.Lennard Sprong2018-06-14
| | | | The previous version attempted to free the first element multiple times.
* Build fix: stop initialising an auto char array.Simon Tatham2018-04-23
| | | | | | | Checking with the standards, I think this is legal C99, but not legal C89 - and we are compiling in C89 mode. Why _every_ version of gcc didn't object, given all the warning and pedantry options, I'm not sure, but one did, so I should fix it.
* Add a request_keys() function with a midend wrapper.Franklin Wei2018-04-22
| | | | | | | | This function gives the front end a way to find out what keys the back end requires; and as such it is mostly useful for ports without a keyboard. It is based on changes originally found in Chris Boyle's Android port, though some modifications were needed to make it more flexible.
* Move fgetline out into misc.c.Simon Tatham2018-04-22
| | | | I'm about to want to use it outside the GTK front end.
* Assorted char * -> const char * API changes.Simon Tatham2017-10-01
| | | | | | I went through all the char * parameters and return values I could see in puzzles.h by eye and spotted ones that surely ought to have been const all along.
* Use a proper union in struct config_item.Simon Tatham2017-10-01
| | | | | | This allows me to use different types for the mutable, dynamically allocated string value in a C_STRING control and the fixed constant list of option names in a C_CHOICES.
* New name UI_UPDATE for interpret_move's return "".Simon Tatham2017-10-01
| | | | | | | | | | | | | Now midend.c directly tests the returned pointer for equality to this value, instead of checking whether it's the empty string. A minor effect of this is that games may now return a dynamically allocated empty string from interpret_move() and treat it as just another legal move description. But I don't expect anyone to be perverse enough to actually do that! The main purpose is that it avoids returning a string literal from a function whose return type is a pointer to _non-const_ char, i.e. we are now one step closer to being able to make this code base clean under -Wwrite-strings.
* Nitpicks to the previous commit.Simon Tatham2017-04-30
| | | | | | | | | We enforce by assertion that the target buffer size is nonzero before subtracting 1 from it; the call to fatal() is replaced by another assert so that it will give clearer diagnostic information if it fails; the variable holding the return value of strlen should be size_t and its declaration should be in a C90-compatible location. Finally, the reason why the function needs to be exist is clarified.
* Work around non-compliant sprintf().Franklin Wei2017-04-30
| | | | | | | | Rockbox's sprintf() lacks the ability to left-justify a string. Fixed by adding a copy_left_justfied() function to misc.c. This is a new version of this commit, as the previous version broke saving!
* Patch idea from Debian, but modified considerably in implementation:Simon Tatham2010-04-25
| | | | | | | | | | | | introduce a new colour in Slant (COL_FILLEDSQUARE) which is used for the background of any grid square that has a diagonal line in it. This makes it easier to spot the one square on a giant board you forgot to fill in, but on the other hand I found it to look very strange and disconcerting. So I've set the colour to be identical to COL_BACKGROUND by default, and users who like the idea can enable it by environment variable or by local patch. [originally from svn r8930]
* Keyboard control for Dominosa (patch largely due to James H, thoughSimon Tatham2009-12-22
| | | | | | with considerable meddling from me). [originally from svn r8788]
* Patch from James H to add keyboard control in Sixteen and NetslideSimon Tatham2009-01-26
| | | | | | (and also belatedly document the keyboard support in Unequal). [originally from svn r8432]
* Patch from James H to centralise some generally useful cursor-Simon Tatham2008-09-13
| | | | | | handling functionality into misc.c. [originally from svn r8176]
* Add a new misc.c function needed by Slide's colour setup.Simon Tatham2007-05-07
| | | | [originally from svn r7552]
* Substantial infrastructure upheaval. I've separated the drawing APISimon Tatham2005-08-18
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | as seen by the back ends from the one implemented by the front end, and shoved a piece of middleware (drawing.c) in between to permit interchange of multiple kinds of the latter. I've also added a number of functions to the drawing API to permit printing as well as on-screen drawing, and retired print.py in favour of integrated printing done by means of that API. The immediate visible change is that print.py is dead, and each puzzle now does its own printing: where you would previously have typed `print.py solo 2x3', you now type `solo --print 2x3' and it should work in much the same way. Advantages of the new mechanism available right now: - Map is now printable, because the new print function can make use of the output from the existing game ID decoder rather than me having to replicate all those fiddly algorithms in Python. - the new print functions can cope with non-initial game states, which means each puzzle supporting --print also supports --with-solutions. - there's also a --scale option permitting users to adjust the size of the printed puzzles. Advantages which will be available at some point: - the new API should permit me to implement native printing mechanisms on Windows and OS X. [originally from svn r6190]
* Two tiny cleanup patches from James H.Simon Tatham2005-07-17
| | | | [originally from svn r6111]
* Get rid of the malloc in shuffle(), by defining a subfunctionSimon Tatham2005-07-17
| | | | | | | memswap() which declares a fixed-size buffer on the stack and uses it multiple times if necessary. [originally from svn r6107]
* Another game from James H: `Black Box'.Simon Tatham2005-07-17
| | | | [originally from svn r6100]
* Cleanups and memory leak fixes from James H.Simon Tatham2005-07-16
| | | | [originally from svn r6099]
* Introduce a `shuffle' utility function.Simon Tatham2005-07-14
| | | | [originally from svn r6090]
* Refactoring from James H: the highlight and lowlight colour setupSimon Tatham2005-07-06
| | | | | | | common to Fifteen, Sixteen, Twiddle and Pegs is now a utility function in misc.c. [originally from svn r6076]
* `Guess', a Mastermind clone from James Harvey. This checkin alsoSimon Tatham2005-06-23
| | | | | | | | introduces a few new utility functions in misc.c, one of which is the bitmap obfuscator from Mines (which has therefore been moved out of mines.c). [originally from svn r5992]
* The Windows RNG turns out to only give about 16 bits at a time. ThisSimon Tatham2004-05-03
| | | | | | | | | | | | | | | is (a) pretty feeble, and (b) means that although Net seeds transfer between platforms and still generate the same game, there's a suspicious discrepancy in the typical seed _generated_ by each platform. I have a better RNG kicking around in this code base already, so I'll just use it. Each midend has its own random_state, which it passes to new_game_seed() as required. A handy consequence of this is that initial seed data is now passed to midend_new(), which means that new platform implementors are unlikely to forget to seed the RNG because failure to do so causes a compile error! [originally from svn r4187]
* Game configuration box for Windows, by constructing the dialog boxSimon Tatham2004-05-03
| | | | | | | | | | right from scratch without the slightest reference to any dialog templates (meaning that we get to figure out the layout and _then_ choose the window size). I'm rather pleased with that. Also introduced free_cfg(), which is why this checkin touched gtk.c as well. [originally from svn r4184]
* Implemented text and clipping primitives in the frontend, and addedSimon Tatham2004-04-29
two new simple games `fifteen' and `sixteen'. [originally from svn r4173]