Skip to content

Commit

Permalink
sprite fixes, animation demo
Browse files Browse the repository at this point in the history
fixed sprite glitches, added small animation demo to test sprites
  • Loading branch information
BBQGiraffe authored Oct 13, 2019
1 parent 34a7c80 commit a561439
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 114 deletions.
31 changes: 22 additions & 9 deletions engine.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#include "engine.h"
#include "img.h"
#include "fileNames.h"
#include <tinyfiledialogs\tinyfiledialogs.h>
#include <iostream>

void Engine::startGame() {
//init window stuff
Engine::startGraphics("OpenNitemare3D", 640, 640);
Engine::startGraphics("OpenNitemare3D", 640, 480);
bool quit = false;


Expand All @@ -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:
Expand All @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#define __ENGINE_H__
#include <string>
#include <SDL2/SDL.h>
#undef main

class Engine
{
public:
Expand Down
45 changes: 19 additions & 26 deletions img.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <stdio.h>
#include <tinyfiledialogs/tinyfiledialogs.h>

#include "img.h"

#define APPARENT_OFFSET 1156
Expand Down Expand Up @@ -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,
Expand All @@ -75,17 +66,20 @@ void IMG::GetFileOffsets(FILE* pFile, offsetVector& offsets)
{
int retval;
uint32_t offset;
uint32_t firstOffset = 0;
uint32_t lowestOffset = 0xFFFFFFFF;

do
{
retval = fread(&offset, 4, 1, pFile);
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)
Expand All @@ -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;
}

Expand All @@ -108,7 +102,6 @@ int IMG::LoadImgFile(std::string imgFile)
GetFileOffsets(pFile, fileOffsets);
if (ReadFiles(pFile, fileOffsets) == IMG_NOK)
{

return IMG_NOK;
}

Expand Down
27 changes: 27 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@




#include <string>
#include <tinyfiledialogs\tinyfiledialogs.h>
#include <stdio.h>
Expand All @@ -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;
}
*/
155 changes: 76 additions & 79 deletions pal.cpp
Original file line number Diff line number Diff line change
@@ -1,79 +1,76 @@
#include <stdio.h>
#include <tinyfiledialogs/tinyfiledialogs.h>
#include "pal.h"
#include <string.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;
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 <stdio.h>

#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()
{

}

0 comments on commit a561439

Please sign in to comment.