ScummVM API documentation
osys_n64.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef __OSYS_N64_H__
23 #define __OSYS_N64_H__
24 
25 #include "common/rect.h"
26 #include "common/config-manager.h"
27 #include "common/events.h"
28 
29 #include "backends/base-backend.h"
30 
31 #include "base/main.h"
32 
33 #include "graphics/surface.h"
34 #include "graphics/paletteman.h"
35 #include "graphics/pixelformat.h"
36 
37 #include "audio/mixer_intern.h"
38 
39 #include <libn64.h>
40 #include <n64utils.h>
41 
42 #define DEFAULT_SOUND_SAMPLE_RATE 8000 // 8 kHz
43 //#define DEFAULT_SOUND_SAMPLE_RATE 11025 // 11 kHz
44 
45 #define N64_PAL_FPS 25
46 #define N64_NTSC_FPS 30
47 
48 typedef int (*TimerProc)(int interval);
49 
50 // Interrupt callback functions
51 void vblCallback(void);
52 void sndCallback(void);
53 void refillAudioBuffers(void);
54 
55 // External utility functions
56 void enableAudioPlayback(void);
57 void disableAudioPlayback(void);
58 void checkTimers(void);
59 int timer_handler(int t);
60 
61 static volatile bool _audioEnabled = false; // Used by interrupt callbacks
62 
63 /* Graphic mode identifiers */
64 enum GraphicModeID {
65  OVERS_NTSC_340X240,
66  NORM_NTSC_320X240,
67  NORM_PAL_320X240,
68  OVERS_PAL_340X240,
69  NORM_MPAL_320X240,
70  OVERS_MPAL_340X240
71 };
72 
73 class OSystem_N64 : virtual public BaseBackend, public Common::EventSource, public PaletteManager {
74 protected:
75  enum TransactionMode {
76  kTransactionNone = 0,
77  kTransactionActive = 1,
78  kTransactionRollback = 2
79  };
80 
84  TransactionMode _transactionMode;
85 
86  //
87  // Transaction support
88  //
89  struct VideoState {
90  constexpr VideoState() : width(0), height(0), format(),
91  graphicsMode(OVERS_NTSC_340X240) {
92  }
93 
94  uint width, height;
95  Graphics::PixelFormat format;
96  int graphicsMode;
97  };
98 
103 
108 
113 
114  Audio::MixerImpl *_mixer;
115 
116  struct display_context * _dc; // Display context for N64 on screen buffer switching
117 
118  Graphics::Surface _framebuffer;
119 
120  uint16 *_offscreen_hic; // Offscreen converted to 16bit surface
121  uint8 *_offscreen_pal; // Offscreen with palette indexes
122  uint16 *_overlayBuffer; // Offscreen for the overlay (16 bit)
123 
124  uint16 *_screenPalette; // Array for palette entries (256 colors max)
125 
126 #ifndef N64_EXTREME_MEMORY_SAVING
127  uint8 *_screenExactPalette; // Array for palette entries, as received by setPalette(), no precision loss
128 #endif
129  uint16 _cursorPalette[256]; // Palette entries for the cursor
130 
131  uint16 _screenWidth, _screenHeight;
132  uint16 _frameBufferWidth; // Width of framebuffer in N64 memory
133  uint8 _offscrPixels; // Pixels to skip on each line before start drawing, used to center image
134  uint8 _maxFps; // Max frames-per-second which can be shown on screen
135 
136  int _shakeXOffset;
137  int _shakeYOffset;
138 
139  uint8 *_cursor_pal; // Cursor buffer, palettized
140  uint16 *_cursor_hic; // Cursor buffer, 16bit
141  bool _cursorPaletteDisabled;
142  bool _dirtyPalette;
143 
144  uint _cursorWidth, _cursorHeight;
145 
146  int _cursorKeycolor;
147 
148  uint16 _overlayHeight, _overlayWidth;
149  bool _overlayVisible;
150  bool _overlayInGUI;
151 
152  bool _disableFpsLimit; // When this is enabled, the system doesn't limit screen updates
153 
154  bool _mouseVisible;
155  volatile int _mouseX, _mouseY;
156  volatile float _tempMouseX, _tempMouseY;
157  volatile int _mouseMaxX, _mouseMaxY;
158  int _mouseHotspotX, _mouseHotspotY;
159 
160  int8 _controllerPort;
161  int8 _mousePort;
162  bool _controllerHasRumble; // Gets enabled if rumble-pak is detected
163 
164  bool _dirtyOffscreen;
165 
166 public:
167 
168  /* These have to be accessed by interrupt callbacks */
169  uint16 _audioBufferSize;
170  uint32 _viClockRate; // Clock rate of video system, depending on VI mode
171 
172  uint32 _timerCallbackNext;
173  uint32 _timerCallbackTimer;
174  TimerProc _timerCallback;
175  /* *** */
176 
177  OSystem_N64();
178  virtual ~OSystem_N64();
179 
180  virtual void initBackend();
181 
182  virtual bool hasFeature(Feature f);
183  virtual void setFeatureState(Feature f, bool enable);
184  virtual bool getFeatureState(Feature f);
185  virtual const GraphicsMode *getSupportedGraphicsModes() const;
186  virtual int getDefaultGraphicsMode() const;
187  virtual bool setGraphicsMode(int mode, uint flags = OSystem::kGfxModeNoFlags);
188  virtual int getGraphicsMode() const;
189  virtual void beginGFXTransaction();
191  virtual int getScreenChangeID() const;
192  virtual void initSize(uint width, uint height, const Graphics::PixelFormat *format);
193  virtual int16 getHeight();
194  virtual int16 getWidth();
195 
196  virtual PaletteManager *getPaletteManager() { return this; }
197 protected:
198  // PaletteManager API
199  virtual void setPalette(const byte *colors, uint start, uint num);
200  virtual void grabPalette(byte *colors, uint start, uint num) const;
201 
202 public:
203  virtual void copyRectToScreen(const void *buf, int pitch, int x, int y, int w, int h);
204  virtual void updateScreen();
205  virtual Graphics::Surface *lockScreen();
206  virtual void unlockScreen();
207  virtual void setShakePos(int shakeXOffset, int shakeYOffset);
208 
209  virtual void showOverlay(bool inGUI);
210  virtual void hideOverlay();
211  virtual bool isOverlayVisible() const { return _overlayVisible; }
212  virtual void clearOverlay();
213  virtual void grabOverlay(Graphics::Surface &surface);
214  virtual void copyRectToOverlay(const void *buf, int pitch, int x, int y, int w, int h);
215  virtual int16 getOverlayHeight() const;
216  virtual int16 getOverlayWidth() const;
218  return Graphics::PixelFormat(2, 5, 5, 5, 0, 11, 6, 1, 0);
219  }
220 
221  virtual bool showMouse(bool visible);
222 
223  virtual void warpMouse(int x, int y);
224  virtual void setMouseCursor(const void *buf, uint w, uint h, int hotspotX, int hotspotY, uint32 keycolor, bool dontScale, const Graphics::PixelFormat *format, const byte *mask);
225  virtual void setCursorPalette(const byte *colors, uint start, uint num);
226 
227  virtual bool pollEvent(Common::Event &event);
228  virtual uint32 getMillis(bool skipRecord = false);
229  virtual void delayMillis(uint msecs);
230 
231  virtual Common::MutexInternal *createMutex(void);
232 
233  virtual void quit();
234 
235  virtual Audio::Mixer *getMixer();
236  virtual void getTimeAndDate(TimeDate &t, bool skipRecord = false) const;
237  virtual void setTimerCallback(TimerProc callback, int interval);
238  virtual void logMessage(LogMessageType::Type type, const char *message);
239 
240  void rebuildOffscreenGameBuffer(void);
241  void rebuildOffscreenMouseBuffer(void);
242  void switchGraphicModeId(int mode);
243 
244  void setupMixer(void);
245 
246  void detectControllers(void);
247  void readControllerAnalogInput(void); // read controller analog nub position
248 };
249 
250 #endif /* __OSYS_N64_H__ */
virtual void initSize(uint width, uint height, const Graphics::PixelFormat *format)
Definition: system.h:110
virtual OSystem::TransactionError endGFXTransaction()
virtual Graphics::PixelFormat getOverlayFormat() const
Definition: osys_n64.h:217
Definition: surface.h:67
Definition: system.h:779
virtual int16 getHeight()
virtual void grabOverlay(Graphics::Surface &surface)
virtual void beginGFXTransaction()
virtual void getTimeAndDate(TimeDate &t, bool skipRecord=false) const
Definition: pixelformat.h:138
Definition: system.h:730
Feature
Definition: system.h:414
virtual void setPalette(const byte *colors, uint start, uint num)
virtual void hideOverlay()
virtual bool hasFeature(Feature f)
TransactionMode _transactionMode
Definition: osys_n64.h:84
virtual void warpMouse(int x, int y)
virtual void unlockScreen()
virtual bool setGraphicsMode(int mode, uint flags=OSystem::kGfxModeNoFlags)
Definition: osys_n64.h:89
virtual Common::MutexInternal * createMutex(void)
virtual void setShakePos(int shakeXOffset, int shakeYOffset)
Type
Definition: log.h:33
virtual int getDefaultGraphicsMode() const
Definition: mixer_intern.h:58
virtual void copyRectToScreen(const void *buf, int pitch, int x, int y, int w, int h)
virtual void updateScreen()
Definition: mixer.h:70
virtual const GraphicsMode * getSupportedGraphicsModes() const
virtual bool isOverlayVisible() const
Definition: osys_n64.h:211
VideoState _currentState
Definition: osys_n64.h:102
virtual void setMouseCursor(const void *buf, uint w, uint h, int hotspotX, int hotspotY, uint32 keycolor, bool dontScale, const Graphics::PixelFormat *format, const byte *mask)
Definition: base-backend.h:31
virtual void showOverlay(bool inGUI)
TransactionError
Definition: system.h:1177
Definition: events.h:210
virtual void clearOverlay()
virtual Audio::Mixer * getMixer()
VideoState _oldState
Definition: osys_n64.h:107
int _screenChangeID
Definition: osys_n64.h:112
virtual void quit()
virtual void initBackend()
virtual bool pollEvent(Common::Event &event)
virtual int getGraphicsMode() const
virtual void copyRectToOverlay(const void *buf, int pitch, int x, int y, int w, int h)
virtual void setCursorPalette(const byte *colors, uint start, uint num)
virtual PaletteManager * getPaletteManager()
Definition: osys_n64.h:196
virtual uint32 getMillis(bool skipRecord=false)
virtual int getScreenChangeID() const
virtual void logMessage(LogMessageType::Type type, const char *message)
virtual void delayMillis(uint msecs)
Definition: events.h:270
Definition: mutex.h:40
Definition: osys_n64.h:73
virtual Graphics::Surface * lockScreen()
Definition: paletteman.h:47
virtual int16 getOverlayWidth() const
virtual void grabPalette(byte *colors, uint start, uint num) const
virtual int16 getOverlayHeight() const
virtual bool getFeatureState(Feature f)
virtual int16 getWidth()
virtual void setFeatureState(Feature f, bool enable)
virtual bool showMouse(bool visible)