From 50a88355a7e71fae9873c40f538388dc7c59d2c9 Mon Sep 17 00:00:00 2001 From: a dinosaur Date: Tue, 1 Jul 2014 14:47:00 +1000 Subject: [PATCH] Initial commit --- Endoomed.sln | 20 ++ Endoomed/Endoomed.vcxproj | 85 +++++++ Endoomed/Endoomed.vcxproj.filters | 22 ++ Endoomed/res/codepage.png | Bin 0 -> 1642 bytes Endoomed/src/main.cpp | 383 ++++++++++++++++++++++++++++++ 5 files changed, 510 insertions(+) create mode 100644 Endoomed.sln create mode 100644 Endoomed/Endoomed.vcxproj create mode 100644 Endoomed/Endoomed.vcxproj.filters create mode 100644 Endoomed/res/codepage.png create mode 100644 Endoomed/src/main.cpp diff --git a/Endoomed.sln b/Endoomed.sln new file mode 100644 index 0000000..7c05243 --- /dev/null +++ b/Endoomed.sln @@ -0,0 +1,20 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 2012 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Endoomed", "Endoomed\Endoomed.vcxproj", "{B710A45C-2D83-42CD-9F6B-8213F2D54931}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Win32 = Debug|Win32 + Release|Win32 = Release|Win32 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {B710A45C-2D83-42CD-9F6B-8213F2D54931}.Debug|Win32.ActiveCfg = Debug|Win32 + {B710A45C-2D83-42CD-9F6B-8213F2D54931}.Debug|Win32.Build.0 = Debug|Win32 + {B710A45C-2D83-42CD-9F6B-8213F2D54931}.Release|Win32.ActiveCfg = Release|Win32 + {B710A45C-2D83-42CD-9F6B-8213F2D54931}.Release|Win32.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/Endoomed/Endoomed.vcxproj b/Endoomed/Endoomed.vcxproj new file mode 100644 index 0000000..5bfa7ba --- /dev/null +++ b/Endoomed/Endoomed.vcxproj @@ -0,0 +1,85 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + {B710A45C-2D83-42CD-9F6B-8213F2D54931} + Endoomed + + + + Application + true + v110 + MultiByte + + + Application + false + v110 + true + MultiByte + + + + + + + + + + + + + bin/win32/ + obj/win32/ + + + bin/win32/ + obj/win32/ + + + + Level4 + Disabled + true + _CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + + + true + SDL2.lib;SDL2main.lib;SDL2_image.lib;%(AdditionalDependencies) + Windows + + + + + Level4 + MaxSpeed + true + true + true + _CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + + + true + true + true + SDL2.lib;SDL2main.lib;SDL2_image.lib;%(AdditionalDependencies) + Windows + + + + + + + + + \ No newline at end of file diff --git a/Endoomed/Endoomed.vcxproj.filters b/Endoomed/Endoomed.vcxproj.filters new file mode 100644 index 0000000..7271fed --- /dev/null +++ b/Endoomed/Endoomed.vcxproj.filters @@ -0,0 +1,22 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + \ No newline at end of file diff --git a/Endoomed/res/codepage.png b/Endoomed/res/codepage.png new file mode 100644 index 0000000000000000000000000000000000000000..ae4f5d941f1e433c59c5921280b484efd57db877 GIT binary patch literal 1642 zcmV-w29^1VP)xWu==sp}1}XAd5#rXc^26%lQK z?{FtR0T}sD?$hbW`V*(DeFOY(62{gF@S#6=NQ=GNpOnzez&MFQDs8RS_W2!e?w>60 zkv|DC1sU~m*#q%xO=tdT1;D06{@c6;?V3Rs!T=y1 zT1*I0PXXj4(;cGFu2wqMvT{usC};*?U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 -0,0 +1,383 @@ +#include +#include +#include +#include +#include + +#define CHARWIDTH 8 +#define CHARHEIGHT 16 + +enum class EColour : unsigned char +{ + BLACK, + BLUE, + GREEN, + CYAN, + RED, + MEGENTA, + BROWN, + LTGREY, + DKGREY, + LTBLUE, + LTGREEN, + LTCYAN, + LTRED, + LTMEGENTA, + YELLOW, + WHITE +}; + +SDL_Colour g_aColourTable[] = +{ + { 0x00, 0x00, 0x00, 0xFF }, // BLACK + { 0x00, 0x00, 0xAA, 0xFF }, // BLUE + { 0x00, 0xAA, 0x00, 0xFF }, // GREEN + { 0x00, 0xAA, 0xAA, 0xFF }, // CYAN + { 0xAA, 0x00, 0x00, 0xFF }, // RED + { 0xAA, 0x00, 0xAA, 0xFF }, // MEGENTA + { 0xAA, 0x55, 0x00, 0xFF }, // BROWN + { 0xAA, 0xAA, 0xAA, 0xFF }, // LTGREY + { 0x55, 0x55, 0x55, 0xFF }, // DKGREY + { 0x55, 0x55, 0xFF, 0xFF }, // BLUE + { 0x55, 0xFF, 0x55, 0xFF }, // GREEN + { 0x55, 0xFF, 0xFF, 0xFF }, // CYAN + { 0xFF, 0x55, 0x55, 0xFF }, // RED + { 0xFF, 0x55, 0xFF, 0xFF }, // MEGENTA + { 0xFF, 0xFF, 0x55, 0xFF }, // YELLOW + { 0xFF, 0xFF, 0xFF, 0xFF } // WHITE +}; + +struct SChar +{ + unsigned char character; + EColour bgColour; + EColour fgColour; + bool blinking; +}; + +inline int clampi ( int a_iVal, int a_iMin, int a_iMax ) +{ + return ( a_iVal < a_iMin ) ? a_iMin : ( a_iVal > a_iMax ) ? a_iMax : a_iVal; +} + +static Uint32 TimerCallback ( Uint32 a_uiInterval, void* a_pParam ) +{ + SDL_Event event; + SDL_UserEvent userEvent; + + userEvent.type = SDL_USEREVENT; + userEvent.code = 0; + userEvent.data1 = nullptr; + userEvent.data2 = nullptr; + + event.type = SDL_USEREVENT; + event.user = userEvent; + + SDL_PushEvent ( &event ); + + return a_uiInterval; +} + +int main ( int argc, char** argv ) +{ + if ( SDL_Init ( SDL_INIT_VIDEO ) < 0 ) + { + return -1; + } + + if ( !( IMG_Init ( IMG_INIT_PNG ) & IMG_INIT_PNG ) ) + { + SDL_Quit (); + return -1; + } + + SDL_Window* pWindow = nullptr; + SDL_Renderer* pRenderer = nullptr; + if ( SDL_CreateWindowAndRenderer ( 640, 400, 0, &pWindow, &pRenderer ) < 0 ) + { + IMG_Quit (); + SDL_Quit (); + return -1; + } + + SDL_SetWindowTitle ( pWindow, "EnDOOMed" ); + + SDL_Texture* pCodepage = IMG_LoadTexture ( pRenderer, "codepage.png" ); + if ( pCodepage == nullptr ) + { + SDL_DestroyRenderer ( pRenderer ); + SDL_DestroyWindow ( pWindow ); + IMG_Quit (); + SDL_Quit (); + return -1; + } + + SChar aDisplay[2000]; + memset ( aDisplay, 0, sizeof(aDisplay) ); + + FILE* pFile = fopen ( "ENDOOM.bin", "rb" ); + if ( pFile == nullptr ) + { + SDL_DestroyTexture ( pCodepage ); + SDL_DestroyRenderer ( pRenderer ); + SDL_DestroyWindow ( pWindow ); + IMG_Quit (); + SDL_Quit (); + return -1; + } + + for ( int i = 0; i < 2000; ++i ) + { + SChar& currentCharacter = aDisplay[i]; + + unsigned char ucSpecial; + fread ( ¤tCharacter.character, sizeof(unsigned char), 1, pFile ); + fread ( &ucSpecial, sizeof(unsigned char), 1, pFile ); + + currentCharacter.bgColour = EColour(( ucSpecial & 112 ) >> 4); + currentCharacter.fgColour = EColour(ucSpecial & 15); + currentCharacter.blinking = ( ucSpecial & 128 > 0 ) ? true : false; + } + + SDL_AddTimer ( 400, TimerCallback, nullptr ); + + SChar leftBrush; + leftBrush.character = 219; + leftBrush.bgColour = EColour::BLACK; + leftBrush.fgColour = EColour::LTGREY; + leftBrush.blinking = false; + + SChar rightBrush; + rightBrush.character = ' '; + rightBrush.bgColour = EColour::BLACK; + rightBrush.fgColour = EColour::LTGREY; + rightBrush.blinking = false; + + bool bMouseLeft = false, bMouseRight = false; + int iMousePrevX = 0, iMousePrevY = 0; + int iMouseX = 0, iMouseY = 0; + bool bMouseInWindow = false; + + bool bValidRender = false; + bool bBlinkState = true; + bool bRunning = true; + SDL_Event event; + while ( bRunning ) + { + iMousePrevX = iMouseX; + iMousePrevY = iMouseY; + + SDL_PumpEvents (); + if ( SDL_WaitEvent ( &event ) ) + { + do + { + switch ( event.type ) + { + case ( SDL_QUIT ): + { + bRunning = false; + break; + } + case ( SDL_USEREVENT ): + { + bBlinkState = !bBlinkState; + bValidRender = false; + break; + } + case ( SDL_MOUSEBUTTONDOWN ): + { + if ( event.button.button == SDL_BUTTON_LEFT ) + { + bMouseLeft = true; + } + else + if ( event.button.button == SDL_BUTTON_RIGHT ) + { + bMouseRight = true; + } + break; + } + case ( SDL_MOUSEBUTTONUP ): + { + if ( event.button.button == SDL_BUTTON_LEFT ) + { + bMouseLeft = false; + } + else + if ( event.button.button == SDL_BUTTON_RIGHT ) + { + bMouseRight = false; + } + break; + } + case ( SDL_MOUSEMOTION ): + { + iMouseX = event.motion.x; + iMouseY = event.motion.y; + break; + } + case ( SDL_WINDOWEVENT ): + { + switch ( event.window.event ) + { + case ( SDL_WINDOWEVENT_LEAVE ): + { + bMouseInWindow = false; + break; + }; + case ( SDL_WINDOWEVENT_ENTER ): + { + bMouseInWindow = true; + break; + }; + }; + + break; + } + } + } + while ( SDL_PollEvent ( &event ) > 0 ); + } + + if ( bMouseInWindow && ( bMouseLeft || bMouseRight ) ) + { + int iFromX = clampi ( iMouseX / CHARWIDTH, 0, 79 ); + int iFromY = clampi ( iMouseY / CHARHEIGHT, 0, 24 ); + int iToX = clampi ( iMousePrevX / CHARWIDTH, 0, 79 ); + int iToY = clampi ( iMousePrevY / CHARHEIGHT, 0, 24 ); + + SChar* srcChar = nullptr; + if ( bMouseLeft ) + { + srcChar = &leftBrush; + } + else + if ( bMouseRight ) + { + srcChar = &rightBrush; + } + + if ( iToX == iFromX && iToY == iFromY ) + { + SChar& dstChar = aDisplay[iToX + iToY * 80]; + dstChar.blinking = srcChar->blinking; + dstChar.bgColour = srcChar->bgColour; + dstChar.fgColour = srcChar->fgColour; + dstChar.character = srcChar->character; + } + else + { + int iTemp; + bool bSteep = labs ( iToY - iFromY ) > labs ( iToX - iFromX ); + if ( bSteep ) + { + iTemp = iFromY; + iFromY = iFromX; + iFromX = iTemp; + + iTemp = iToY; + iToY = iToX; + iToX = iTemp; + } + if ( iFromX > iToX ) + { + iTemp = iToX; + iToX = iFromX; + iFromX = iTemp; + + iTemp = iToY; + iToY = iFromY; + iFromY = iTemp; + } + + int iDeltaX = iToX - iFromX; + int iDeltaY = labs ( iToY - iFromY ); + int iError = iDeltaX / 2; + int iY = iFromY; + int iYStep = ( iFromY < iToY ) ? 1 : -1; + for ( int iX = iFromX; iX <= iToX; ++iX ) + { + if ( bSteep ) + { + SChar& dstChar = aDisplay[iY + iX * 80]; + dstChar.blinking = srcChar->blinking; + dstChar.bgColour = srcChar->bgColour; + dstChar.fgColour = srcChar->fgColour; + dstChar.character = srcChar->character; + } + else + { + SChar& dstChar = aDisplay[iX + iY * 80]; + dstChar.blinking = srcChar->blinking; + dstChar.bgColour = srcChar->bgColour; + dstChar.fgColour = srcChar->fgColour; + dstChar.character = srcChar->character; + } + + iError -= iDeltaY; + if ( iError < 0 ) + { + iY += iYStep; + iError += iDeltaX; + } + } + } + + bValidRender = false; + } + + if ( !bValidRender ) + { + SDL_SetRenderDrawColor ( pRenderer, 48, 48, 48, 255 ); + SDL_RenderClear ( pRenderer ); + + SDL_Rect src, dst; + + src.w = CHARWIDTH; + src.h = CHARHEIGHT; + dst.w = CHARWIDTH; + dst.h = CHARHEIGHT; + + int iX = 0, iY = 0; + for ( int i = 0; i < 2000; ++i ) + { + SChar& currentCharacter = aDisplay[i]; + SDL_Colour& currentBgColour = g_aColourTable[(unsigned char)aDisplay[i].bgColour]; + SDL_Colour& currentFgColour = g_aColourTable[(unsigned char)aDisplay[i].fgColour]; + + src.x = ( currentCharacter.character % 32 ) * CHARWIDTH; + src.y = ( currentCharacter.character / 32 ) * CHARHEIGHT; + dst.x = iX * CHARWIDTH; + dst.y = iY * CHARHEIGHT; + + SDL_SetRenderDrawColor ( pRenderer, currentBgColour.r, currentBgColour.g, currentBgColour.b, currentBgColour.a ); + SDL_RenderFillRect ( pRenderer, &dst ); + SDL_SetRenderDrawColor ( pRenderer, 0, 0, 0, currentFgColour.a ); + + if ( bBlinkState || !currentCharacter.blinking ) + { + SDL_SetTextureColorMod ( pCodepage, currentFgColour.r, currentFgColour.g, currentFgColour.b ); + SDL_RenderCopy ( pRenderer, pCodepage, &src, &dst ); + } + + if ( ++iX >= 80 ) + { + iX = 0; + ++iY; + } + } + + SDL_RenderPresent ( pRenderer ); + bValidRender = true; + } + } + + fclose ( pFile ); + SDL_DestroyTexture ( pCodepage ); + SDL_DestroyRenderer ( pRenderer ); + SDL_DestroyWindow ( pWindow ); + IMG_Quit (); + SDL_Quit (); + return 0; +}