diff --git a/Makefile b/Makefile index d9ff64c..91d50fb 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ CFLAGS = -Wall EXE = DarkRP2D LIB_PATH = lib/ INC_PATH = include/ -LIBS = -lmingw32 -lSDL2main -lSDL2 -lbox2d -lentityx +LIBS = -lmingw32 -lSDL2main -lSDL2 -lbox2d -lentityx -lSDL2_image SRCDIR = src BUILDDIR = build diff --git a/dlls/SDL2_image.dll b/dlls/SDL2_image.dll new file mode 100644 index 0000000..cadd2c5 Binary files /dev/null and b/dlls/SDL2_image.dll differ diff --git a/dlls/libjpeg-9.dll b/dlls/libjpeg-9.dll new file mode 100644 index 0000000..d66bd4a Binary files /dev/null and b/dlls/libjpeg-9.dll differ diff --git a/dlls/libpng16-16.dll b/dlls/libpng16-16.dll new file mode 100644 index 0000000..d59e67e Binary files /dev/null and b/dlls/libpng16-16.dll differ diff --git a/dlls/libtiff-5.dll b/dlls/libtiff-5.dll new file mode 100644 index 0000000..015d069 Binary files /dev/null and b/dlls/libtiff-5.dll differ diff --git a/dlls/libwebp-7.dll b/dlls/libwebp-7.dll new file mode 100644 index 0000000..31405fd Binary files /dev/null and b/dlls/libwebp-7.dll differ diff --git a/dlls/zlib1.dll b/dlls/zlib1.dll new file mode 100644 index 0000000..1aa57ce Binary files /dev/null and b/dlls/zlib1.dll differ diff --git a/include/SDL2/SDL_image.h b/include/SDL2/SDL_image.h new file mode 100644 index 0000000..54c940a --- /dev/null +++ b/include/SDL2/SDL_image.h @@ -0,0 +1,161 @@ +/* + SDL_image: An example image loading library for use with SDL + Copyright (C) 1997-2017 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ + +/* A simple library to load images of various formats as SDL surfaces */ + +#ifndef SDL_IMAGE_H_ +#define SDL_IMAGE_H_ + +#include "SDL.h" +#include "SDL_version.h" +#include "begin_code.h" + +/* Set up for C function definitions, even when using C++ */ +#ifdef __cplusplus +extern "C" { +#endif + +/* Printable format: "%d.%d.%d", MAJOR, MINOR, PATCHLEVEL +*/ +#define SDL_IMAGE_MAJOR_VERSION 2 +#define SDL_IMAGE_MINOR_VERSION 0 +#define SDL_IMAGE_PATCHLEVEL 2 + +/* This macro can be used to fill a version structure with the compile-time + * version of the SDL_image library. + */ +#define SDL_IMAGE_VERSION(X) \ +{ \ + (X)->major = SDL_IMAGE_MAJOR_VERSION; \ + (X)->minor = SDL_IMAGE_MINOR_VERSION; \ + (X)->patch = SDL_IMAGE_PATCHLEVEL; \ +} + +/** + * This is the version number macro for the current SDL_image version. + */ +#define SDL_IMAGE_COMPILEDVERSION \ + SDL_VERSIONNUM(SDL_IMAGE_MAJOR_VERSION, SDL_IMAGE_MINOR_VERSION, SDL_IMAGE_PATCHLEVEL) + +/** + * This macro will evaluate to true if compiled with SDL_image at least X.Y.Z. + */ +#define SDL_IMAGE_VERSION_ATLEAST(X, Y, Z) \ + (SDL_IMAGE_COMPILEDVERSION >= SDL_VERSIONNUM(X, Y, Z)) + +/* This function gets the version of the dynamically linked SDL_image library. + it should NOT be used to fill a version structure, instead you should + use the SDL_IMAGE_VERSION() macro. + */ +extern DECLSPEC const SDL_version * SDLCALL IMG_Linked_Version(void); + +typedef enum +{ + IMG_INIT_JPG = 0x00000001, + IMG_INIT_PNG = 0x00000002, + IMG_INIT_TIF = 0x00000004, + IMG_INIT_WEBP = 0x00000008 +} IMG_InitFlags; + +/* Loads dynamic libraries and prepares them for use. Flags should be + one or more flags from IMG_InitFlags OR'd together. + It returns the flags successfully initialized, or 0 on failure. + */ +extern DECLSPEC int SDLCALL IMG_Init(int flags); + +/* Unloads libraries loaded with IMG_Init */ +extern DECLSPEC void SDLCALL IMG_Quit(void); + +/* Load an image from an SDL data source. + The 'type' may be one of: "BMP", "GIF", "PNG", etc. + + If the image format supports a transparent pixel, SDL will set the + colorkey for the surface. You can enable RLE acceleration on the + surface afterwards by calling: + SDL_SetColorKey(image, SDL_RLEACCEL, image->format->colorkey); + */ +extern DECLSPEC SDL_Surface * SDLCALL IMG_LoadTyped_RW(SDL_RWops *src, int freesrc, const char *type); +/* Convenience functions */ +extern DECLSPEC SDL_Surface * SDLCALL IMG_Load(const char *file); +extern DECLSPEC SDL_Surface * SDLCALL IMG_Load_RW(SDL_RWops *src, int freesrc); + +#if SDL_VERSION_ATLEAST(2,0,0) +/* Load an image directly into a render texture. + */ +extern DECLSPEC SDL_Texture * SDLCALL IMG_LoadTexture(SDL_Renderer *renderer, const char *file); +extern DECLSPEC SDL_Texture * SDLCALL IMG_LoadTexture_RW(SDL_Renderer *renderer, SDL_RWops *src, int freesrc); +extern DECLSPEC SDL_Texture * SDLCALL IMG_LoadTextureTyped_RW(SDL_Renderer *renderer, SDL_RWops *src, int freesrc, const char *type); +#endif /* SDL 2.0 */ + +/* Functions to detect a file type, given a seekable source */ +extern DECLSPEC int SDLCALL IMG_isICO(SDL_RWops *src); +extern DECLSPEC int SDLCALL IMG_isCUR(SDL_RWops *src); +extern DECLSPEC int SDLCALL IMG_isBMP(SDL_RWops *src); +extern DECLSPEC int SDLCALL IMG_isGIF(SDL_RWops *src); +extern DECLSPEC int SDLCALL IMG_isJPG(SDL_RWops *src); +extern DECLSPEC int SDLCALL IMG_isLBM(SDL_RWops *src); +extern DECLSPEC int SDLCALL IMG_isPCX(SDL_RWops *src); +extern DECLSPEC int SDLCALL IMG_isPNG(SDL_RWops *src); +extern DECLSPEC int SDLCALL IMG_isPNM(SDL_RWops *src); +extern DECLSPEC int SDLCALL IMG_isSVG(SDL_RWops *src); +extern DECLSPEC int SDLCALL IMG_isTIF(SDL_RWops *src); +extern DECLSPEC int SDLCALL IMG_isXCF(SDL_RWops *src); +extern DECLSPEC int SDLCALL IMG_isXPM(SDL_RWops *src); +extern DECLSPEC int SDLCALL IMG_isXV(SDL_RWops *src); +extern DECLSPEC int SDLCALL IMG_isWEBP(SDL_RWops *src); + +/* Individual loading functions */ +extern DECLSPEC SDL_Surface * SDLCALL IMG_LoadICO_RW(SDL_RWops *src); +extern DECLSPEC SDL_Surface * SDLCALL IMG_LoadCUR_RW(SDL_RWops *src); +extern DECLSPEC SDL_Surface * SDLCALL IMG_LoadBMP_RW(SDL_RWops *src); +extern DECLSPEC SDL_Surface * SDLCALL IMG_LoadGIF_RW(SDL_RWops *src); +extern DECLSPEC SDL_Surface * SDLCALL IMG_LoadJPG_RW(SDL_RWops *src); +extern DECLSPEC SDL_Surface * SDLCALL IMG_LoadLBM_RW(SDL_RWops *src); +extern DECLSPEC SDL_Surface * SDLCALL IMG_LoadPCX_RW(SDL_RWops *src); +extern DECLSPEC SDL_Surface * SDLCALL IMG_LoadPNG_RW(SDL_RWops *src); +extern DECLSPEC SDL_Surface * SDLCALL IMG_LoadPNM_RW(SDL_RWops *src); +extern DECLSPEC SDL_Surface * SDLCALL IMG_LoadSVG_RW(SDL_RWops *src); +extern DECLSPEC SDL_Surface * SDLCALL IMG_LoadTGA_RW(SDL_RWops *src); +extern DECLSPEC SDL_Surface * SDLCALL IMG_LoadTIF_RW(SDL_RWops *src); +extern DECLSPEC SDL_Surface * SDLCALL IMG_LoadXCF_RW(SDL_RWops *src); +extern DECLSPEC SDL_Surface * SDLCALL IMG_LoadXPM_RW(SDL_RWops *src); +extern DECLSPEC SDL_Surface * SDLCALL IMG_LoadXV_RW(SDL_RWops *src); +extern DECLSPEC SDL_Surface * SDLCALL IMG_LoadWEBP_RW(SDL_RWops *src); + +extern DECLSPEC SDL_Surface * SDLCALL IMG_ReadXPMFromArray(char **xpm); + +/* Individual saving functions */ +extern DECLSPEC int SDLCALL IMG_SavePNG(SDL_Surface *surface, const char *file); +extern DECLSPEC int SDLCALL IMG_SavePNG_RW(SDL_Surface *surface, SDL_RWops *dst, int freedst); +extern DECLSPEC int SDLCALL IMG_SaveJPG(SDL_Surface *surface, const char *file, int quality); +extern DECLSPEC int SDLCALL IMG_SaveJPG_RW(SDL_Surface *surface, SDL_RWops *dst, int freedst, int quality); + +/* We'll use SDL for reporting errors */ +#define IMG_SetError SDL_SetError +#define IMG_GetError SDL_GetError + +/* Ends C function definitions when using C++ */ +#ifdef __cplusplus +} +#endif +#include "close_code.h" + +#endif /* SDL_IMAGE_H_ */ diff --git a/lib/libSDL2_image.a b/lib/libSDL2_image.a new file mode 100644 index 0000000..3c3db63 Binary files /dev/null and b/lib/libSDL2_image.a differ diff --git a/lib/libSDL2_image.dll.a b/lib/libSDL2_image.dll.a new file mode 100644 index 0000000..09f3502 Binary files /dev/null and b/lib/libSDL2_image.dll.a differ diff --git a/src/DarkRP2D.cpp b/src/DarkRP2D.cpp index 17148cb..eee6d14 100644 --- a/src/DarkRP2D.cpp +++ b/src/DarkRP2D.cpp @@ -1,17 +1,64 @@ +#include +#include + #include "DarkRP2D.h" +#include "utility/utility.h" - void DarkRP2D::create() { + + + void DarkRP2D::create(SDL_Renderer *gRenderer) { + this->gRenderer = gRenderer; + + //Load PNG texture + gTexture = utility::loadTexture( "assets/police_officer.png", gRenderer); } - void DarkRP2D::loop(float deltaTime) { + unsigned int ups = 0; + unsigned int acc = 0; + void DarkRP2D::loop(unsigned int deltaTime) { + accumulator += deltaTime; + acc += deltaTime; + + //Process Input + + skippedFrames = 0; + while (accumulator >= MS_PER_UPDATE && skippedFrames <= MAX_FRAMESKIP) { + accumulator -= MS_PER_UPDATE; + update(0.0f); + ups++; + skippedFrames++; + } + + if(acc >= 1000) { + std::cout << "Updates per second: " << ups << std::endl; + acc -= 1000; + ups = 0; + } + render(0.0f); + } + + void DarkRP2D::update(float deltaInSec) { } - void DarkRP2D::resize() { + void DarkRP2D::render(float deltaInSec) { + //Clear screen + SDL_RenderClear( gRenderer ); + + //Render texture to screen + SDL_RenderCopy( gRenderer, gTexture, NULL, NULL ); + + //Update screen + SDL_RenderPresent( gRenderer ); + } + + void DarkRP2D::resize(int width, int height) { } void DarkRP2D::dispose() { - + //Free loaded image + SDL_DestroyTexture( gTexture ); + gTexture = NULL; } diff --git a/src/DarkRP2D.h b/src/DarkRP2D.h index 553841d..875c685 100644 --- a/src/DarkRP2D.h +++ b/src/DarkRP2D.h @@ -1,11 +1,28 @@ #ifndef DARKRP2D_H #define DARKRP2D_H +#include + class DarkRP2D { +private: + unsigned int accumulator = 0; + int skippedFrames = 0; + //test texture + SDL_Texture* gTexture = NULL; + public: - void create(); - void loop(float deltaTime); - void resize(); + static const int UPDATES_PER_SECOND = 60; + static const unsigned int MS_PER_UPDATE = 17; // 1 / UPDATES_PER_SECOND ~ 16,777777777 + static const int MAX_FRAMESKIP = 5; + + SDL_Renderer* gRenderer = NULL; + + DarkRP2D() {} + void create(SDL_Renderer *gRenderer); + void loop(unsigned int deltaTime); + void update(float deltaInSec); + void render(float deltaInSec); + void resize(int width, int height); void dispose(); }; diff --git a/src/DesktopLauncher.cpp b/src/DesktopLauncher.cpp index b1032ca..80f7710 100644 --- a/src/DesktopLauncher.cpp +++ b/src/DesktopLauncher.cpp @@ -3,13 +3,162 @@ #include #include "DarkRP2D.h" -const int WIDTH = 800, HEIGHT = 600; +#define GAME_NAME "DarkRP2D" + +const int SCREEN_WIDTH = 800, SCREEN_HEIGHT = 600; + +SDL_Window* gWindow = NULL; //The window we'll be rendering to +SDL_Surface* gScreenSurface = NULL; //The surface contained by the window +SDL_Renderer* gRenderer = NULL; +DarkRP2D* darkRP2D = NULL; + +void PrintEvent(const SDL_Event *event) { + if (event->type == SDL_WINDOWEVENT) { + switch (event->window.event) { + case SDL_WINDOWEVENT_SHOWN: + SDL_Log("Window %d shown", event->window.windowID); + break; + case SDL_WINDOWEVENT_HIDDEN: + SDL_Log("Window %d hidden", event->window.windowID); + break; + case SDL_WINDOWEVENT_EXPOSED: + SDL_Log("Window %d exposed", event->window.windowID); + break; + case SDL_WINDOWEVENT_MOVED: + SDL_Log("Window %d moved to %d,%d", + event->window.windowID, event->window.data1, + event->window.data2); + break; + case SDL_WINDOWEVENT_RESIZED: + SDL_Log("Window %d resized to %dx%d", + event->window.windowID, event->window.data1, + event->window.data2); + break; + case SDL_WINDOWEVENT_SIZE_CHANGED: + SDL_Log("Window %d size changed to %dx%d", + event->window.windowID, event->window.data1, + event->window.data2); + break; + case SDL_WINDOWEVENT_MINIMIZED: + SDL_Log("Window %d minimized", event->window.windowID); + break; + case SDL_WINDOWEVENT_MAXIMIZED: + SDL_Log("Window %d maximized", event->window.windowID); + break; + case SDL_WINDOWEVENT_RESTORED: + SDL_Log("Window %d restored", event->window.windowID); + break; + case SDL_WINDOWEVENT_ENTER: + SDL_Log("Mouse entered window %d", + event->window.windowID); + break; + case SDL_WINDOWEVENT_LEAVE: + SDL_Log("Mouse left window %d", event->window.windowID); + break; + case SDL_WINDOWEVENT_FOCUS_GAINED: + SDL_Log("Window %d gained keyboard focus", + event->window.windowID); + break; + case SDL_WINDOWEVENT_FOCUS_LOST: + SDL_Log("Window %d lost keyboard focus", + event->window.windowID); + break; + case SDL_WINDOWEVENT_CLOSE: + SDL_Log("Window %d closed", event->window.windowID); + break; +#if SDL_VERSION_ATLEAST(2, 0, 5) + case SDL_WINDOWEVENT_TAKE_FOCUS: + SDL_Log("Window %d is offered a focus", event->window.windowID); + break; + case SDL_WINDOWEVENT_HIT_TEST: + SDL_Log("Window %d has a special hit test", event->window.windowID); + break; +#endif + default: + SDL_Log("Window %d got unknown event %d", + event->window.windowID, event->window.event); + break; + } + } +} + +bool init() { + bool success = true; + //Initialize SDL + if( SDL_Init( SDL_INIT_VIDEO ) < 0 ) { + printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() ); + success = false; + } else { + //Create window + gWindow = SDL_CreateWindow( GAME_NAME, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN ); + if( gWindow == NULL ) { + printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() ); + success = false; + } else { + //Get window surface + gScreenSurface = SDL_GetWindowSurface( gWindow ); + if( gScreenSurface == NULL ) + { + printf( "ScreenSurface could not be fetched! SDL Error: %s\n", SDL_GetError() ); + success = false; + } + //Create renderer for window + gRenderer = SDL_CreateRenderer( gWindow, -1, SDL_RENDERER_ACCELERATED ); + if( gRenderer == NULL ) + { + printf( "Renderer could not be created! SDL Error: %s\n", SDL_GetError() ); + success = false; + } + } + } + + darkRP2D = new DarkRP2D(); + darkRP2D->create(gRenderer); + + return success; +} + +void close() { + darkRP2D->dispose(); + delete darkRP2D; + SDL_DestroyRenderer( gRenderer ); + SDL_FreeSurface( gScreenSurface ); + SDL_DestroyWindow( gWindow ); + SDL_Quit(); +} int main(int argc, char *argv[]) { - SDL_Init( SDL_INIT_EVERYTHING ); + //Initialization flag + bool success = true; - DarkRP2D* darkRP2D = new DarkRP2D(); + success = init(); - SDL_Quit( ); - return EXIT_SUCCESS; + SDL_SetWindowResizable(gWindow, SDL_TRUE); + + //Main loop flag + bool running = true; + //Event handler + SDL_Event e; + //Main GameClass + + unsigned int lastTime = SDL_GetTicks(), currentTime, elapsedTime; + //While application is running + while( running ) { + currentTime = SDL_GetTicks(); + elapsedTime = currentTime - lastTime; + //Handle events on queue + while( SDL_PollEvent( &e ) != 0 ) { + //User requests quit + if( e.type == SDL_QUIT ) { + running = false; + } + PrintEvent(&e); + } + darkRP2D->loop(elapsedTime); + lastTime = currentTime; + } + + close(); + + return success; } diff --git a/src/test/temp.cpp b/src/test/temp.cpp deleted file mode 100644 index e69de29..0000000 diff --git a/src/test/test.cpp b/src/test/test.cpp deleted file mode 100644 index d60dbf5..0000000 --- a/src/test/test.cpp +++ /dev/null @@ -1,3 +0,0 @@ -void hello() { - -} diff --git a/src/utility/utility.h b/src/utility/utility.h new file mode 100644 index 0000000..fcf7a96 --- /dev/null +++ b/src/utility/utility.h @@ -0,0 +1,12 @@ +#ifndef UTILITY_H +#define UTILITY_H + +#include +#include +#include + +namespace utility { + SDL_Texture* loadTexture( std::string path, SDL_Renderer* gRenderer ); +} + +#endif // UTILITY_H diff --git a/src/utility/utitlity.cpp b/src/utility/utitlity.cpp new file mode 100644 index 0000000..d8adb5b --- /dev/null +++ b/src/utility/utitlity.cpp @@ -0,0 +1,26 @@ +#include "utility.h" + +namespace utility { + + SDL_Texture* loadTexture( std::string path, SDL_Renderer* gRenderer ) { + //The final texture + SDL_Texture* newTexture = NULL; + + //Load image at specified path + SDL_Surface* loadedSurface = IMG_Load( path.c_str() ); + if( loadedSurface == NULL ) { + printf( "Unable to load image %s! SDL_image Error: %s\n", path.c_str(), IMG_GetError() ); + } + else { + //Create texture from surface pixels + newTexture = SDL_CreateTextureFromSurface( gRenderer, loadedSurface ); + if( newTexture == NULL ) { + printf( "Unable to create texture from %s! SDL Error: %s\n", path.c_str(), SDL_GetError() ); + } + //Get rid of old loaded surface + SDL_FreeSurface( loadedSurface ); + } + return newTexture; + } + +}