Skip to content

Commit

Permalink
Add OAM Screen
Browse files Browse the repository at this point in the history
Signed-off-by: sukhman-sukh <[email protected]>
  • Loading branch information
sukhman-sukh committed Jan 8, 2024
1 parent 6ff8e7d commit 6aa0320
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 24 deletions.
58 changes: 57 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"files.associations": {
"*.cairo": "csharp",
"cstddef": "cpp",
"charconv": "cpp",
"chrono": "cpp",
Expand All @@ -18,6 +19,61 @@
"streambuf": "cpp",
"typeinfo": "cpp",
"variant": "cpp",
"*.inc": "cpp"
"*.inc": "cpp",
"atomic": "cpp",
"bit": "cpp",
"*.tcc": "cpp",
"bitset": "cpp",
"cctype": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"compare": "cpp",
"complex": "cpp",
"concepts": "cpp",
"condition_variable": "cpp",
"cstdarg": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"deque": "cpp",
"forward_list": "cpp",
"list": "cpp",
"map": "cpp",
"set": "cpp",
"string": "cpp",
"unordered_map": "cpp",
"unordered_set": "cpp",
"vector": "cpp",
"exception": "cpp",
"algorithm": "cpp",
"iterator": "cpp",
"memory": "cpp",
"memory_resource": "cpp",
"numeric": "cpp",
"random": "cpp",
"string_view": "cpp",
"fstream": "cpp",
"future": "cpp",
"initializer_list": "cpp",
"iomanip": "cpp",
"iosfwd": "cpp",
"iostream": "cpp",
"istream": "cpp",
"limits": "cpp",
"numbers": "cpp",
"ostream": "cpp",
"semaphore": "cpp",
"span": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"stop_token": "cpp",
"thread": "cpp",
"cfenv": "cpp",
"cinttypes": "cpp",
"valarray": "cpp"
}
}
Binary file added Algerian Regular.ttf
Binary file not shown.
70 changes: 47 additions & 23 deletions src/graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ PPU::PPU()
frameRendered = false;

// Fill renderArray initially with white (lightest color in palette)
std::fill(renderSprites, renderSprites + (160 * 144), bg_colors[0]);
std::fill(renderTiles, renderTiles + (160 * 144), bg_colors[0]);
std::fill(renderArray, renderArray + (160 * 144), bg_colors[0]);
}

Expand Down Expand Up @@ -162,11 +160,12 @@ void PPU::listBgMap()
// Outer loop increments for every new tile to be rendered
// Middle loop increments for every line of tile
// Inner loop increments for every pixel of line of tile.
// We are printing 4 tiles in a row with gap of 2 between each tile { (tileNumber % 4) * 10) + j + 8) }
// We are printing 2 tiles in a row with gap of 2 between each tile { (tileNumber % 2) * 10) + j + 8) }
void PPU::renderOAM()
{
Byte sprite_y, sprite_pixel_col, sprite_palette;

std::fill(renderSprites, renderSprites + (160 * 144), bg_colors[0]);
sprites.clear();
Byte sprite_height = 8;
for (Word i = 0xFE00; i < 0xFEA0; i += 4)
{
Expand All @@ -186,6 +185,7 @@ void PPU::renderOAM()
}

int sprite_count = 0;
metadatas.clear();
for (auto it = sprites.begin(); it != sprites.end(); ++it)
{
sprite_palette = (it->flags & 0x10) ? objPalette1 : objPalette0;
Expand All @@ -196,21 +196,47 @@ void PPU::renderOAM()
sprite_pixel_col = (((*mMap)[0x8000 + (it->tile * 0x10) + (i * 2)] >> (7 - j)) & 0x1) + ((((*mMap)[0x8000 + (it->tile * 0x10) + (i * 2) + 1] >> (7 - j)) & 0x1) * 2);
if (sprite_pixel_col != 0)
{
renderSprites[((static_cast<int>(sprite_count / 4) * 20) + i + 20) * 160 + (((sprite_count % 4) * 20) + j + 8)] = bg_colors[(bgPalette >> (sprite_pixel_col * 2)) & 0x3];
renderSprites[((static_cast<int>(sprite_count / 2) * 30) + i + 20) * 160 + (((sprite_count % 2) * 60) + j + 8)] = bg_colors[(bgPalette >> (sprite_pixel_col * 2)) & 0x3];
}
}
}
char *data = new char[100];
char const *flipFlag;
SpriteMetaData* spriteData = new SpriteMetaData();
spriteData->xCord = (((sprite_count % 2) * 120) + 40); // X coord of box containing metadata
spriteData->yCord = ((static_cast<int>(sprite_count / 2) * 65) + 30); // Y coord of box containing metadata

switch (it->flags & 0x60)
{
case 0x00: // Normal
flipFlag = "NO FLIP";
break;
case 0x20: // Flip X
flipFlag = "X FLIP";
break;
case 0x40: // Flip Y
flipFlag = "Y FLIP";
break;
case 0x60: // Flip X and Y
flipFlag = "X AND Y \nFLIP";
break;
default:
break;
}
sprintf(data, "X: %hu \n\n Y: %hu \n\n %hu\n\n %s", it->x, it->y, it->tile, flipFlag);
spriteData->data = data;
metadatas.push_back(spriteData);
sprite_count++;
}
SDL_UpdateTexture(debugTexture, NULL, renderSprites, 160 * 4);
SDL_RenderClear(debugRenderer);
SDL_RenderCopy(debugRenderer, debugTexture, NULL, NULL);
SDL_RenderPresent(debugRenderer);

render_ttl();
}

void PPU::render_ttl()
{
// Initialize SDL
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
Expand All @@ -223,7 +249,7 @@ void PPU::render_ttl()
return;
}

TTF_Font* font = TTF_OpenFont("../lazy.ttf", 24);
TTF_Font* font = TTF_OpenFont("../timesRoman.ttf", 18u);
if (!font)
{
printf("Error loading font: %s\n", TTF_GetError());
Expand All @@ -233,23 +259,20 @@ void PPU::render_ttl()
SDL_Rect dest;
SDL_Color textColor = { 0, 0, 0 };

SDL_Surface* text_surf = TTF_RenderText_Solid_Wrapped(font, "Hey\nWorld", textColor, 150);
SDL_Texture* text = SDL_CreateTextureFromSurface(debugRenderer, text_surf);

dest.x = 160 - (text_surf->w / 9.0f);
dest.y = 140;
dest.w = text_surf->w;
dest.h = text_surf->h;
SDL_RenderCopy(debugRenderer, text, NULL, &dest);
for (auto spriteData : metadatas)
{
char const *data = spriteData->data;
SDL_Surface* text_surf = TTF_RenderText_Solid_Wrapped(font, data, textColor, 100);
SDL_Texture* text = SDL_CreateTextureFromSurface(debugRenderer, text_surf);

SDL_DestroyTexture(text);
SDL_FreeSurface(text_surf);
dest.x = spriteData->xCord;
dest.y = spriteData->yCord;
dest.w = text_surf->w*3/5;
dest.h = text_surf->h*4/5;
SDL_RenderCopy(debugRenderer, text, NULL, &dest);

SDL_RenderPresent(debugRenderer);
// SDL_RenderClear(debugRenderer);
// SDL_RenderCopy(debugRenderer, debugTexture, NULL, NULL);
// SDL_RenderPresent(debugRenderer);
// surface = TTF_RenderText_Solid(font, text, textColor);
SDL_RenderPresent(debugRenderer);
}
}

// We are reading values from 0x8000 to 0x8fff
Expand All @@ -259,6 +282,7 @@ void PPU::render_ttl()
// We are printing 14 tiles in a row with gap of 2 between each tile { (tileNumber % 14) * 10) + j + 8) }
void PPU::listTiles()
{
std::fill(renderTiles, renderTiles + (160 * 144), bg_colors[0]);
int tileNumber = 0;
Byte pixel_col;
for (Word tileAddr = 0x8000; tileAddr < 0x8FFF; tileAddr += 0x10)
Expand Down
8 changes: 8 additions & 0 deletions src/graphics.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ struct Sprite
Byte flags;
};

struct SpriteMetaData
{
char const *data;
int xCord;
int yCord;
};

class PPU
{
private:
Expand Down Expand Up @@ -120,6 +127,7 @@ class PPU
};

std::vector<Sprite> sprites;
std::vector<SpriteMetaData *> metadatas;

public:
PPU();
Expand Down
Binary file added timesRoman.ttf
Binary file not shown.

0 comments on commit 6aa0320

Please sign in to comment.