]> Shamusworld >> Repos - apple2/blobdiff - src/video.cpp
Added floppy #2 saving, statistics to makefile.
[apple2] / src / video.cpp
old mode 100755 (executable)
new mode 100644 (file)
index 082120e..58a630a
-//\r
-// VIDEO.CPP: SDL/local hardware specific video routines\r
-//\r
-// by James L. Hammons\r
-//\r
-// JLH = James L. Hammons <jlhamm@acm.org>\r
-//\r
-// WHO  WHEN        WHAT\r
-// ---  ----------  ------------------------------------------------------------\r
-// JLH  01/04/2006  Added changelog ;-)\r
-// JLH  01/20/2006  Cut out unnecessary buffering\r
-//\r
-\r
-#include "video.h"\r
-\r
-//#include <SDL.h>\r
-#include <string.h>    // Why??? (for memset, etc... Lazy!) Dunno why, but this just strikes me as wrong...\r
-#include <malloc.h>\r
-#include "sdlemu_opengl.h"\r
-#include "log.h"\r
-#include "settings.h"\r
-#include "icon.h"\r
-\r
-#if SDL_BYTEORDER == SDL_BIG_ENDIAN\r
-#define MASK_R 0xFF000000\r
-#define MASK_G 0x00FF0000\r
-#define MASK_B 0x0000FF00\r
-#define MASK_A 0x000000FF\r
-#else\r
-#define MASK_R 0x000000FF\r
-#define MASK_G 0x0000FF00\r
-#define MASK_B 0x00FF0000\r
-#define MASK_A 0xFF000000\r
-#endif\r
-\r
-//#define TEST_ALPHA_BLENDING\r
-\r
-// Exported global variables (actually, these are LOCAL global variables, EXPORTED...)\r
-\r
-SDL_Surface * surface, * mainSurface, * someAlphaSurface;\r
-Uint32 mainSurfaceFlags;\r
-//uint32 scrBuffer[VIRTUAL_SCREEN_WIDTH * VIRTUAL_SCREEN_HEIGHT];\r
-uint32 * scrBuffer = NULL;\r
-SDL_Joystick * joystick;\r
-\r
-//\r
-// Prime SDL and create surfaces\r
-//\r
-bool InitVideo(void)\r
-{\r
-       // Set up SDL library\r
-       if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_AUDIO | SDL_INIT_TIMER | SDL_INIT_NOPARACHUTE) < 0)\r
-       {\r
-               WriteLog("Video: Could not initialize the SDL library: %s\n", SDL_GetError());\r
-               return false;\r
-       }\r
-\r
-       //Set icon (mainly for Win32 target--though seems to work under KDE as well...!)\r
-       SDL_Surface * iconSurf = SDL_CreateRGBSurfaceFrom(icon, 32, 32, 32, 128, \r
-               MASK_R, MASK_G, MASK_B, MASK_A);\r
-       SDL_WM_SetIcon(iconSurf, NULL);\r
-       SDL_FreeSurface(iconSurf);\r
-\r
-       // Get proper info about the platform we're running on...\r
-       const SDL_VideoInfo * info = SDL_GetVideoInfo();\r
-\r
-       if (!info)\r
-       {\r
-               WriteLog("Video: SDL is unable to get the video info: %s\n", SDL_GetError());\r
-               return false;\r
-       }\r
-\r
-       if (settings.useOpenGL)\r
-       {\r
-               mainSurfaceFlags = SDL_HWSURFACE | SDL_HWPALETTE | SDL_DOUBLEBUF | SDL_OPENGL;\r
-               SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);\r
-       }\r
-       else\r
-       {\r
-               mainSurfaceFlags = SDL_DOUBLEBUF;\r
-\r
-               if (info->hw_available)\r
-               {\r
-                       mainSurfaceFlags = SDL_HWSURFACE | SDL_HWPALETTE;\r
-                       WriteLog("Video: Hardware available...\n");\r
-               }\r
-\r
-               if (info->blit_hw)\r
-               {\r
-                       mainSurfaceFlags |= SDL_HWACCEL;\r
-                       WriteLog("Video: Hardware blit available...\n");\r
-               }\r
-       }\r
-\r
-       if (settings.fullscreen)\r
-               mainSurfaceFlags |= SDL_FULLSCREEN;\r
-\r
-       // Create the primary SDL display (32 BPP)\r
-       if (!settings.useOpenGL)\r
-               mainSurface = SDL_SetVideoMode(VIRTUAL_SCREEN_WIDTH, VIRTUAL_SCREEN_HEIGHT, 32, mainSurfaceFlags);\r
-       else\r
-//             mainSurface = SDL_SetVideoMode(VIRTUAL_SCREEN_WIDTH * 2, VIRTUAL_SCREEN_HEIGHT * 2, 32, mainSurfaceFlags);\r
-//             mainSurface = SDL_SetVideoMode(VIRTUAL_SCREEN_WIDTH, VIRTUAL_SCREEN_HEIGHT * 2, 32, mainSurfaceFlags);\r
-               mainSurface = SDL_SetVideoMode(VIRTUAL_SCREEN_WIDTH, VIRTUAL_SCREEN_HEIGHT, 32, mainSurfaceFlags);\r
-\r
-       if (mainSurface == NULL)\r
-       {\r
-               WriteLog("Video: SDL is unable to set the video mode: %s\n", SDL_GetError());\r
-               return false;\r
-       }\r
-\r
-       SDL_WM_SetCaption("Apple 2 SDL", "Apple 2 SDL");\r
-\r
-       // Create the secondary SDL display (32 BPP) that we use directly\r
-       surface = SDL_CreateRGBSurface(SDL_SWSURFACE, VIRTUAL_SCREEN_WIDTH, VIRTUAL_SCREEN_HEIGHT, 32,\r
-               MASK_R, MASK_G, MASK_B, MASK_A);\r
-/*WriteLog("Video: Created secondary surface with attributes:\n\n");\r
-WriteLog("\tWidth, height: %u x %u\n", surface->w, surface->h);\r
-WriteLog("\t        Pitch: %u\n", surface->pitch);\r
-WriteLog("\t      Palette: %08X\n", surface->format->palette);\r
-WriteLog("\t          BPP: %u\n", surface->format->BitsPerPixel);\r
-WriteLog("\t      BytesPP: %u\n", surface->format->BytesPerPixel);\r
-WriteLog("\t        RMask: %08X\n", surface->format->Rmask);\r
-WriteLog("\t        GMask: %08X\n", surface->format->Gmask);\r
-WriteLog("\t        BMask: %08X\n", surface->format->Bmask);\r
-WriteLog("\t        AMask: %08X\n", surface->format->Amask);\r
-WriteLog("\n");//*/\r
-\r
-       if (surface == NULL)\r
-       {\r
-               WriteLog("Video: Could not create secondary SDL surface: %s\n", SDL_GetError());\r
-               return false;\r
-       }\r
-\r
-       if (settings.useOpenGL)\r
-               sdlemu_init_opengl(surface, mainSurface, 1 /*method*/, settings.glFilter /*texture type (linear, nearest)*/,\r
-               0 /* Automatic bpp selection based upon src */);\r
-\r
-       // Initialize Joystick support under SDL\r
-/*     if (settings.useJoystick)\r
-       {\r
-               if (SDL_NumJoysticks() <= 0)\r
-               {\r
-                       settings.useJoystick = false;\r
-                       WriteLog("Video: No joystick(s) or joypad(s) detected on your system. Using keyboard...\n");\r
-               }\r
-               else\r
-               {\r
-                       if ((joystick = SDL_JoystickOpen(settings.joyport)) == 0)\r
-                       {\r
-                               settings.useJoystick = false;\r
-                               WriteLog("Video: Unable to open a Joystick on port: %d\n", (int)settings.joyport);\r
-                       }\r
-                       else\r
-                               WriteLog("Video: Using: %s\n", SDL_JoystickName(settings.joyport));\r
-               }\r
-       }//*/\r
-\r
-       // Set up the scrBuffer\r
-       scrBuffer = (uint32 *)surface->pixels;  // Kludge--And shouldn't have to lock since it's a software surface...\r
-//needed? Dunno. Mebbe an SDL function instead?\r
-//     memset(scrBuffer, 0x00, VIRTUAL_SCREEN_WIDTH * VIRTUAL_SCREEN_HEIGHT * sizeof(uint32));\r
-\r
-#ifdef TEST_ALPHA_BLENDING\r
-//Here's some code to test alpha blending...\r
-//Well whaddya know, it works. :-)\r
-       someAlphaSurface = SDL_CreateRGBSurface(SDL_SWSURFACE, 30, 30, 32,\r
-               MASK_R, MASK_G, MASK_B, MASK_A);\r
-\r
-       for(int i=0; i<30; i++)\r
-       {\r
-               for(int j=0; j<30; j++)\r
-               {\r
-                       uint32 color = (uint32)(((double)(i * j) / (29.0 * 29.0)) * 255.0);\r
-                       color = (color << 24) | 0x00FF00FF;\r
-                       ((uint32 *)someAlphaSurface->pixels)[(j * 30) + i] = color;\r
-               }\r
-       }\r
-//End test code\r
-#endif\r
-\r
-       WriteLog("Video: Successfully initialized.\n");\r
-       return true;\r
-}\r
-\r
-//\r
-// Free various SDL components\r
-//\r
-void VideoDone(void)\r
-{\r
-       if (settings.useOpenGL)\r
-               sdlemu_close_opengl();\r
-\r
-       SDL_JoystickClose(joystick);\r
-       SDL_FreeSurface(surface);\r
-       SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_AUDIO | SDL_INIT_TIMER);\r
-       SDL_Quit();\r
-}\r
-\r
-//\r
-// Render the screen buffer to the primary screen surface\r
-//\r
-//void RenderBackbuffer(void)\r
-void RenderScreenBuffer(void)\r
-{\r
-//WriteLog("Video: Blitting a %u x %u surface to the main surface...\n", surface->w, surface->h);\r
-//Don't need this crapola--why have a separate buffer just to copy it to THIS\r
-//buffer in order to copy it to the main screen? That's what *I* thought!\r
-/*     if (SDL_MUSTLOCK(surface))\r
-               while (SDL_LockSurface(surface) < 0)\r
-                       SDL_Delay(10);\r
-\r
-       memcpy(surface->pixels, scrBuffer, VIRTUAL_SCREEN_WIDTH * VIRTUAL_SCREEN_HEIGHT * sizeof(uint32));\r
-\r
-       if (SDL_MUSTLOCK(surface))\r
-               SDL_UnlockSurface(surface);//*/\r
-#ifdef TEST_ALPHA_BLENDING\r
-SDL_Rect dstRect = { 100, 100, 30, 30 };\r
-SDL_BlitSurface(someAlphaSurface, NULL, surface, &dstRect);\r
-#endif\r
-\r
-       if (settings.useOpenGL)\r
-               sdlemu_draw_texture(mainSurface, surface, 1/*1=GL_QUADS*/);\r
-       else\r
-       {\r
-//             SDL_Rect rect = { 0, 0, surface->w, surface->h };\r
-//             SDL_BlitSurface(surface, &rect, mainSurface, &rect);\r
-               SDL_BlitSurface(surface, NULL, mainSurface, NULL);\r
-               SDL_Flip(mainSurface);\r
-    }\r
-}\r
-\r
-/*\r
-//\r
-// Resize the main SDL screen & scrBuffer\r
-//\r
-void ResizeScreen(uint32 width, uint32 height)\r
-{\r
-       char window_title[256];\r
-\r
-       SDL_FreeSurface(surface);\r
-       surface = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 16,\r
-               0x7C00, 0x03E0, 0x001F, 0);\r
-\r
-       if (surface == NULL)\r
-       {\r
-               WriteLog("Video: Could not create primary SDL surface: %s", SDL_GetError());\r
-               exit(1);\r
-       }\r
-\r
-       if (settings.useOpenGL)\r
-               // This seems to work well for resizing (i.e., changes in the pixel width)...\r
-               sdlemu_resize_texture(surface, mainSurface, settings.glFilter);\r
-       else\r
-       {\r
-               mainSurface = SDL_SetVideoMode(width, height, 16, mainSurfaceFlags);\r
-\r
-               if (mainSurface == NULL)\r
-               {\r
-                       WriteLog("Video: SDL is unable to set the video mode: %s\n", SDL_GetError());\r
-                       exit(1);\r
-               }\r
-       }\r
-\r
-       sWriteLog(window_title, "Virtual Jaguar (%i x %i)", (int)width, (int)height);\r
-       SDL_WM_SetCaption(window_title, window_title);\r
-\r
-       // This seems to work well for resizing (i.e., changes in the pixel width)...\r
-//     if (settings.useOpenGL)\r
-//             sdlemu_resize_texture(surface, mainSurface);\r
-}*/\r
-\r
-/*\r
-//\r
-// Fullscreen <-> window switching\r
-//\r
-//NOTE: This does *NOT* work with OpenGL rendering! !!! FIX !!!\r
-void ToggleFullscreen(void)\r
-{\r
-       settings.fullscreen = !settings.fullscreen;\r
-       mainSurfaceFlags &= ~SDL_FULLSCREEN;\r
-\r
-       if (settings.fullscreen)\r
-               mainSurfaceFlags |= SDL_FULLSCREEN;\r
-\r
-       if (!settings.useOpenGL)\r
-               mainSurface = SDL_SetVideoMode(VIRTUAL_SCREEN_WIDTH, VIRTUAL_SCREEN_HEIGHT, 32, mainSurfaceFlags);\r
-       else\r
-//             mainSurface = SDL_SetVideoMode(VIRTUAL_SCREEN_WIDTH * 2, VIRTUAL_SCREEN_HEIGHT * 2, 32, mainSurfaceFlags);\r
-//             mainSurface = SDL_SetVideoMode(VIRTUAL_SCREEN_WIDTH, VIRTUAL_SCREEN_HEIGHT * 2, 32, mainSurfaceFlags);\r
-               mainSurface = SDL_SetVideoMode(VIRTUAL_SCREEN_WIDTH, VIRTUAL_SCREEN_HEIGHT, 32, mainSurfaceFlags);\r
-//     mainSurface = SDL_SetVideoMode(tom_width, tom_height, 16, mainSurfaceFlags);\r
-\r
-       if (mainSurface == NULL)\r
-       {\r
-               WriteLog("Video: SDL is unable to set the video mode: %s\n", SDL_GetError());\r
-               exit(1);\r
-       }\r
-\r
-       SDL_WM_SetCaption("Apple 2 SDL", "Apple 2 SDL");\r
-}\r
-//*/\r
+//
+// VIDEO.CPP: SDL/local hardware specific video routines
+//
+// by James Hammons
+//
+// JLH = James Hammons <jlhamm@acm.org>
+//
+// WHO  WHEN        WHAT
+// ---  ----------  ------------------------------------------------------------
+// JLH  01/04/2006  Added changelog ;-)
+// JLH  01/20/2006  Cut out unnecessary buffering
+//
+
+#include "video.h"
+#include <SDL2/SDL.h>
+#include <string.h>    // Why??? (for memset, etc... Lazy!) Dunno why, but this just strikes me as wrong...
+#include <malloc.h>
+#include "log.h"
+#include "settings.h"
+
+
+// Exported global variables (actually, these are LOCAL global variables, EXPORTED...)
+
+static SDL_Window * sdlWindow = NULL;
+static SDL_Renderer * sdlRenderer = NULL;
+static SDL_Texture * sdlTexture = NULL;
+uint32_t scrBuffer[VIRTUAL_SCREEN_WIDTH * VIRTUAL_SCREEN_HEIGHT * sizeof(uint32_t)];
+
+
+//
+// Prime SDL and create surfaces
+//
+bool InitVideo(void)
+{
+       if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_AUDIO | SDL_INIT_TIMER | SDL_INIT_NOPARACHUTE) != 0)
+       {
+               WriteLog("Video: Could not initialize the SDL library: %s\n", SDL_GetError());
+               return false;
+       }
+
+       int retVal = SDL_CreateWindowAndRenderer(VIRTUAL_SCREEN_WIDTH * 2, VIRTUAL_SCREEN_HEIGHT * 2, SDL_WINDOW_OPENGL, &sdlWindow, &sdlRenderer);
+
+       if (retVal != 0)
+       {
+               WriteLog("Video: Could not window and/or renderer: %s\n", SDL_GetError());
+               return false;
+       }
+
+       // Make the scaled rendering look smoother.
+       SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "linear");
+//     SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "nearest");
+       SDL_RenderSetLogicalSize(sdlRenderer, VIRTUAL_SCREEN_WIDTH, VIRTUAL_SCREEN_HEIGHT);
+
+       sdlTexture = SDL_CreateTexture(sdlRenderer,
+               SDL_PIXELFORMAT_ABGR8888, SDL_TEXTUREACCESS_STREAMING,
+               VIRTUAL_SCREEN_WIDTH, VIRTUAL_SCREEN_HEIGHT);
+
+       WriteLog("Video: Successfully initialized.\n");
+       return true;
+}
+
+
+//
+// Free various SDL components
+//
+void VideoDone(void)
+{
+       WriteLog("Video: Shutting down SDL...\n");
+       SDL_Quit();
+       WriteLog("Video: Done.\n");
+}
+
+
+//
+// Render the screen buffer to the primary screen surface
+//
+void RenderScreenBuffer(void)
+{
+       SDL_UpdateTexture(sdlTexture, NULL, scrBuffer, VIRTUAL_SCREEN_WIDTH * sizeof(Uint32));
+       SDL_RenderClear(sdlRenderer);
+       SDL_RenderCopy(sdlRenderer, sdlTexture, NULL, NULL);
+       SDL_RenderPresent(sdlRenderer);
+}
+
+
+//
+// Fullscreen <-> window switching
+//
+void ToggleFullScreen(void)
+{
+       settings.fullscreen = !settings.fullscreen;
+
+       int retVal = SDL_SetWindowFullscreen(sdlWindow, (settings.fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0));
+       SDL_ShowCursor(settings.fullscreen ? 0 : 1);
+
+       if (retVal != 0)
+               WriteLog("Video::ToggleFullScreen: SDL error = %s\n", SDL_GetError());
+}
+