diff --git a/engine.cpp b/engine.cpp index ebf5254..af4146c 100644 --- a/engine.cpp +++ b/engine.cpp @@ -1,11 +1,11 @@ #include "engine.h" #include "img.h" -#include "fileNames.h" #include +#include void Engine::startGame() { //init window stuff - Engine::startGraphics("OpenNitemare3D", 640, 640); + Engine::startGraphics("OpenNitemare3D", 640, 480); bool quit = false; @@ -15,17 +15,28 @@ void Engine::startGame() { void Engine::startGraphics(const char* Name, int displayWidth, int displayHeight) { IMG img; size_t numImages; - FileNames fileIndex; + //FileNames fileIndex; img.Init("GAME.PAL", "IMG.1"); numImages = img.GetNumImages(); SDL_Init(SDL_INIT_VIDEO); window = SDL_CreateWindow(Name, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, displayWidth, displayHeight, 0); renderer = SDL_CreateRenderer(window, -1, 0); - texture = SDL_CreateTextureFromSurface(renderer, img[fileIndex.getSprite("RED_WALL_SINGLE_PAINTING")]); + texture = SDL_CreateTextureFromSurface(renderer, img[3]); + + int index = 482; while (true) { - SDL_WaitEvent(&event); + SDL_RenderClear(renderer); + texture = SDL_CreateTextureFromSurface(renderer, img[index]); + int img_width = img[index]->h; + int img_height = img[index]->w; + index++; + if (index > 527) { + index = 482; + } + SDL_Delay(250); + SDL_PollEvent(&event); switch (event.type) { case SDL_QUIT: @@ -40,10 +51,12 @@ void Engine::startGraphics(const char* Name, int displayWidth, int displayHeight } SDL_Rect dstrect; - dstrect.x = (displayWidth - displayHeight) / 2 - 1; - dstrect.y = (displayHeight - displayWidth) / 2; - dstrect.w = displayHeight; - dstrect.h = displayWidth; //; // SDL_RenderCopyEx(renderer, texture, nullptr, &dstrect, 90, nullptr, SDL_FLIP_NONE); + //dstrect.x = (displayWidth - displayHeight) / 2 - 1; + //dstrect.y = (displayHeight - displayWidth) / 2; + dstrect.x = displayWidth / 2; + dstrect.y = displayHeight / 2; + dstrect.w = img_height * 3; + dstrect.h = img_width * 3; SDL_RenderCopyEx(renderer, texture, nullptr, &dstrect, 90, nullptr, SDL_FLIP_NONE); diff --git a/engine.h b/engine.h index b39a7fb..8411d38 100644 --- a/engine.h +++ b/engine.h @@ -2,6 +2,8 @@ #define __ENGINE_H__ #include #include +#undef main + class Engine { public: diff --git a/img.cpp b/img.cpp index e7e581b..76f520e 100644 --- a/img.cpp +++ b/img.cpp @@ -1,5 +1,5 @@ #include -#include + #include "img.h" #define APPARENT_OFFSET 1156 @@ -29,33 +29,24 @@ int IMG::ReadFiles(FILE* pFile, offsetVector& offsets) uint8_t pixValue; SDL_Surface* pSurface; - for (size_t offset : offsets) + if (fseek(pFile, offsets[0], SEEK_SET) != 0) + { + printf("Failed to seek to next file in list; malformed?\n"); + return IMG_NOK; + } + + while (feof(pFile) == false) { - if (fseek(pFile, offset, SEEK_SET) != 0) - { - tinyfd_messageBox("Failed to seek to next file in list; malformed?", "Failed to seek to next file in list; malformed?", "ok", "error", 1); - return IMG_NOK; - } fread(&sizeX, 1, 1, pFile); fread(&sizeY, 1, 1, pFile); fseek(pFile, 8, SEEK_CUR); for (int i = 0; i < sizeX * sizeY; i++) { fread(&pixValue, 1, 1, pFile); - if (pixValue != 0xFF) - { - buffer[i * PIXEL_SIZE] = mPal[pixValue].r; - buffer[i * PIXEL_SIZE + 1] = mPal[pixValue].g; - buffer[i * PIXEL_SIZE + 2] = mPal[pixValue].b; - buffer[i * PIXEL_SIZE + 3] = 0xFF; - } - else - { - buffer[i * PIXEL_SIZE] = 0xFF; - buffer[i * PIXEL_SIZE + 1] = 0xFF; - buffer[i * PIXEL_SIZE + 2] = 0xFF; - buffer[i * PIXEL_SIZE + 3] = 0x00; - } + buffer[i * PIXEL_SIZE] = mPal[pixValue].r; + buffer[i * PIXEL_SIZE + 1] = mPal[pixValue].g; + buffer[i * PIXEL_SIZE + 2] = mPal[pixValue].b; + buffer[i * PIXEL_SIZE + 3] = 0xFF; } pSurface = SDL_CreateRGBSurfaceWithFormat(0, sizeY, sizeX, @@ -75,7 +66,7 @@ void IMG::GetFileOffsets(FILE* pFile, offsetVector& offsets) { int retval; uint32_t offset; - uint32_t firstOffset = 0; + uint32_t lowestOffset = 0xFFFFFFFF; do { @@ -83,9 +74,12 @@ void IMG::GetFileOffsets(FILE* pFile, offsetVector& offsets) if (offset != 0) { offsets.push_back(offset); + if (offset < lowestOffset) + { + lowestOffset = offset; + } } - if (firstOffset == 0) { firstOffset = offset; printf("First offset: %x\n", firstOffset); } - } while (ftell(pFile) < firstOffset && retval > 0); + } while (ftell(pFile) <= lowestOffset && retval > 0); } int IMG::LoadImgFile(std::string imgFile) @@ -99,7 +93,7 @@ int IMG::LoadImgFile(std::string imgFile) pFile = fopen(imgFile.c_str(), "rb"); if (pFile == NULL) { - tinyfd_messageBox("could not open IMG file", "could not open IMG file", "ok", "error", 1); + printf("could not open IMG file\n"); return IMG_NOK; } @@ -108,7 +102,6 @@ int IMG::LoadImgFile(std::string imgFile) GetFileOffsets(pFile, fileOffsets); if (ReadFiles(pFile, fileOffsets) == IMG_NOK) { - return IMG_NOK; } diff --git a/main.cpp b/main.cpp index c8e9ad9..b3b6d8a 100644 --- a/main.cpp +++ b/main.cpp @@ -7,6 +7,7 @@ + #include #include #include @@ -21,7 +22,33 @@ int main() { + Engine engine; engine.startGame(); return 0; } + + + + +/* +int main() +{ + + IMG img; + size_t numImages; + std::string name; + img.Init("GAME.PAL", "IMG.1"); + numImages = img.GetNumImages(); + for (int i = 0; i < numImages; i++) + { + name = std::to_string(i) + ".bmp"; + + if (img[i] != NULL) + { + SDL_SaveBMP(img[i], name.c_str()); + } + } + return 0; +} +*/ diff --git a/pal.cpp b/pal.cpp index 36c2e76..0ae0174 100644 --- a/pal.cpp +++ b/pal.cpp @@ -1,79 +1,76 @@ -#include -#include -#include "pal.h" -#include - -#define RED 0 -#define GREEN 1 -#define BLUE 2 - -size_t PAL::GetFileSize(FILE *pFile) -{ - size_t fileLen; - - fseek(pFile, 0, SEEK_END); - fileLen = ftell(pFile); - rewind(pFile); - - return fileLen; -} - -unsigned int PAL::ReadPal(std::string filename, uint32_t offset) -{ - uint8_t buffer[3]; - Palette curPal; - size_t fileLen; - const char* error; - mPalettes.clear(); - - FILE *pFile = fopen(filename.c_str(), "rb"); - if (pFile == NULL) - { - error = "Could not open pallette file!"; - tinyfd_messageBox(error, error, "ok", "error", 1); - return PAL_NOK; - } - - fileLen = GetFileSize(pFile); - - if (offset > fileLen) - { - error = "Offset greater than file size!"; - tinyfd_messageBox(error, error, "ok", "error", 1); - return PAL_NOK; - } - - fseek(pFile, offset, SEEK_SET); - - for (int i = offset; i + 3 < fileLen; i+=3) - { - fread(buffer, 1, 3, pFile); - curPal.r = buffer[RED]; - curPal.g = buffer[GREEN]; - curPal.b = buffer[BLUE]; - - mPalettes.push_back(curPal); - } - - return PAL_OK; -} - -size_t PAL::GetNumPalettes() -{ - return mPalettes.size(); -} - -Palette PAL::operator [](size_t index) -{ - return mPalettes[index]; -} - -PAL::PAL() -{ - -} - -PAL::~PAL() -{ - -} +#include + +#include "pal.h" + +#define RED 0 +#define GREEN 1 +#define BLUE 2 + +size_t PAL::GetFileSize(FILE * pFile) +{ + size_t fileLen; + + fseek(pFile, 0, SEEK_END); + fileLen = ftell(pFile); + rewind(pFile); + + return fileLen; +} + +unsigned int PAL::ReadPal(std::string filename, uint32_t offset) +{ + uint8_t buffer[3]; + Palette curPal; + size_t fileLen; + + mPalettes.clear(); + + FILE* pFile = fopen(filename.c_str(), "rb"); + if (pFile == NULL) + { + printf("File could not be opened\n"); + return PAL_NOK; + } + + fileLen = GetFileSize(pFile); + + if (offset > fileLen) + { + printf("Offset greater than file size\n"); + return PAL_NOK; + } + + fseek(pFile, offset, SEEK_SET); + + for (int i = offset; i <= fileLen; i += 3) + { + fread(buffer, 1, 3, pFile); + curPal.r = buffer[RED]; + curPal.g = buffer[GREEN]; + curPal.b = buffer[BLUE]; + + mPalettes.push_back(curPal); + } + + return PAL_OK; +} + +size_t PAL::GetNumPalettes() +{ + return mPalettes.size(); +} + +Palette PAL::operator [](size_t index) +{ + return mPalettes[index]; +} + +PAL::PAL() +{ + +} + +PAL::~PAL() +{ + +}