Skip to content

Commit

Permalink
Finished BytePusher audio.
Browse files Browse the repository at this point in the history
  • Loading branch information
bustatu committed Jun 23, 2021
1 parent 855c8fb commit 47f693f
Show file tree
Hide file tree
Showing 10 changed files with 152 additions and 13 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ project(EmuEngine)
set(SOURCE_FILES
src/main.cpp
src/window/window.cpp
src/window/audio.cpp
src/window/renderer.cpp
src/emulators/bytepusher/bytepusher.cpp
)

Expand Down
29 changes: 18 additions & 11 deletions src/emulators/bytepusher/bytepusher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
// Sets surface pixel
void set_pixel(SDL_Surface *surface, int x, int y, uint32_t pixel)
{
uint32_t *const target_pixel = (Uint32*)(
(Uint8*) surface -> pixels + y * surface -> pitch
+ x * surface->format->BytesPerPixel);
*target_pixel = pixel;
}
uint32_t *const target_pixel = (uint32_t*)((uint8_t*) surface -> pixels + y * surface -> pitch + x * surface->format->BytesPerPixel);
*target_pixel = pixel;
}

// Get value from the memory
uint32_t BytePusherEmu::getVal(uint32_t pc, uint32_t length)
Expand Down Expand Up @@ -36,6 +34,9 @@ BytePusherEmu::BytePusherEmu()
// These pixels are black
for(int i = 216; i <= 255; i++)
pallete[i] = 0xFF000000;

// Init sound
sound = new Audio();
}

void BytePusherEmu::load(std::string path)
Expand Down Expand Up @@ -104,11 +105,14 @@ void BytePusherEmu::updateKey(SDL_Keycode key, int value)

void BytePusherEmu::input(SDL_Event event)
{
// Set or unset keys
if(event.type == SDL_KEYDOWN)
updateKey(event.key.keysym.sym, 1);
else if(event.type == SDL_KEYUP)
updateKey(event.key.keysym.sym, 0);
if(!isQuit)
{
// Set or unset keys
if(event.type == SDL_KEYDOWN)
updateKey(event.key.keysym.sym, 1);
else if(event.type == SDL_KEYUP)
updateKey(event.key.keysym.sym, 0);
}
}

void BytePusherEmu::innerLoop()
Expand Down Expand Up @@ -147,6 +151,9 @@ void BytePusherEmu::update(float dt)
set_pixel(result, i, j, pallete[frame[(j << 8) | i]]);
}

// Push audio section to the sound engine
sound -> push((int8_t*)(memory + (getVal(6, 2) << 8)), 256);

// 60 Hz, forced
counter -= 1.0 / 60;
}
Expand All @@ -158,6 +165,6 @@ void BytePusherEmu::draw(Window* win)
if(!isQuit)
{
// Copy the surface to the screen
SDL_BlitSurface(result, NULL, win -> getSurface(), NULL);
win -> getRenderer() -> render(result);
}
}
2 changes: 2 additions & 0 deletions src/emulators/bytepusher/bytepusher.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define BYTEPUSHER_H

#include "../emulator.h"
#include "../../window/audio.h"
#include "../../window/window.h"
#include <SDL.h>
#include <iostream>
Expand All @@ -22,6 +23,7 @@ class BytePusherEmu : public UnthreadedEmulator
// Output
SDL_Surface* result = SDL_CreateRGBSurface(0, 256, 256, 32, 0, 0, 0, 0);
uint32_t pallete[256];
Audio* sound;

// Input
SDL_Keycode keys[0x10] = {
Expand Down
5 changes: 3 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,15 @@ int main(int argc, char* argv[])

// Update the emu
emu -> update(window -> getDelta());
emu -> draw(window);

// If emu stopped, close everything
if(!emu -> isRunning())
window -> quit();

// Update the window
SDL_UpdateWindowSurface(window -> getWin());
window -> getRenderer() -> drawStart();
emu -> draw(window);
window -> getRenderer() -> drawEnd();

// Wait a bit so CPU usage is not high
SDL_Delay(2);
Expand Down
22 changes: 22 additions & 0 deletions src/window/audio.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include "audio.h"

Audio::Audio()
{
if(SDL_Init(SDL_INIT_AUDIO) < 0)
printf("{E}: Audio init failed! %s", SDL_GetError());

SDL_zero(wanted);
wanted.freq = 15360;
wanted.samples = 256;
wanted.format = AUDIO_S8;
wanted.channels = 1;

SDL_OpenAudio(&wanted, NULL);

SDL_PauseAudio(0);
}

void Audio::push(int8_t* where, int32_t size)
{
SDL_QueueAudio(1, where, size);
}
16 changes: 16 additions & 0 deletions src/window/audio.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef AUDIO_H
#define AUDIO_H

#include <SDL.h>
#include <iostream>

class Audio
{
private:
SDL_AudioSpec wanted;
public:
Audio();
void push(int8_t *from, int32_t size);
};

#endif
35 changes: 35 additions & 0 deletions src/window/renderer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include "renderer.h"

void SoftwareRenderer::drawStart()
{
SDL_FillRect(SDL_GetWindowSurface(parentWin), NULL, 0x000000);
}

void SoftwareRenderer::render(SDL_Surface* surf)
{
SDL_BlitSurface(surf, NULL, SDL_GetWindowSurface(parentWin), NULL);
}

void SoftwareRenderer::drawEnd()
{
SDL_UpdateWindowSurface(parentWin);
}

void AcceleratedRenderer::render(SDL_Surface* surf)
{
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surf);

SDL_RenderCopy(renderer, texture, NULL, NULL);

SDL_DestroyTexture(texture);
}

void AcceleratedRenderer::drawStart()
{
SDL_RenderClear(renderer);
}

void AcceleratedRenderer::drawEnd()
{
SDL_RenderPresent(renderer);
}
44 changes: 44 additions & 0 deletions src/window/renderer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#ifndef RENDERER_H
#define RENDERER_H

#include <iostream>
#include <SDL.h>

class Renderer
{
protected:
SDL_Window* parentWin = NULL;
public:
Renderer(SDL_Window* win){
parentWin = win;
}
virtual void render(SDL_Surface* surf) = 0;
virtual void drawStart() = 0;
virtual void drawEnd() = 0;
};

class SoftwareRenderer : public Renderer
{
public:
void render(SDL_Surface* surf) override;
void drawStart() override;
void drawEnd() override;
SoftwareRenderer(SDL_Window* win) : Renderer(win) {};
};

class AcceleratedRenderer : public Renderer
{
private:
SDL_Renderer* renderer = nullptr;
public:
void render(SDL_Surface* surf) override;
void drawStart() override;
void drawEnd() override;
AcceleratedRenderer(SDL_Window* win) : Renderer(win) {
renderer = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if(renderer == nullptr)
printf("{E}: SDL_CreateRenderer Error: %s\n", SDL_GetError());
}
};

#endif
7 changes: 7 additions & 0 deletions src/window/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Window::Window()

window = SDL_CreateWindow("EmuEngine", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 256, 256, 0);

renderer = new SoftwareRenderer(window);

deltaTime = 0;
NOW = SDL_GetPerformanceCounter();
LAST = 0;
Expand All @@ -20,6 +22,11 @@ SDL_Window* Window::getWin()
return window;
}

Renderer* Window::getRenderer()
{
return renderer;
}

SDL_Surface* Window::getSurface()
{
return SDL_GetWindowSurface(window);
Expand Down
3 changes: 3 additions & 0 deletions src/window/window.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <cstdlib>
#include <iostream>
#include <SDL.h>
#include "renderer.h"

class Window
{
Expand All @@ -12,6 +13,7 @@ class Window
double deltaTime;
SDL_Window* window = NULL;
bool appQuit = false;
Renderer* renderer;

public:
Window();
Expand All @@ -21,6 +23,7 @@ class Window
void quit();
void setTitle(std::string name = "EmuEngine");
SDL_Surface* getSurface();
Renderer* getRenderer();
SDL_Window* getWin();
};

Expand Down

0 comments on commit 47f693f

Please sign in to comment.