Skip to content

Commit

Permalink
GTK: Use C++ memory allocation.
Browse files Browse the repository at this point in the history
  • Loading branch information
bearoso committed May 14, 2019
1 parent 124594d commit f8a0fad
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 76 deletions.
10 changes: 10 additions & 0 deletions apu/resampler.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ class Resampler
return (a0 * b) + (a1 * m0) + (a2 * m1) + (a3 * c);
}

Resampler()
{
this->buffer_size = 0;
buffer = NULL;
r_step = 1.0;
}

Resampler(int num_samples)
{
this->buffer_size = num_samples;
Expand All @@ -77,6 +84,9 @@ class Resampler

inline void clear(void)
{
if (!buffer)
return;

start = 0;
size = 0;
memset(buffer, 0, buffer_size * 2);
Expand Down
12 changes: 6 additions & 6 deletions gtk/src/gtk_control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -569,9 +569,9 @@ JoyDevice::JoyDevice (unsigned int device_num)

num_axes = SDL_JoystickNumAxes (filedes);
num_hats = SDL_JoystickNumHats (filedes);
axis = (int *) malloc (sizeof (int) * num_axes);
hat = (int *) malloc (sizeof (int) * num_hats);
calibration = (Calibration *) malloc (sizeof (Calibration) * num_axes);
axis = new int[num_axes];
hat = new int[num_hats];
calibration = new Calibration[num_axes];

for (int i = 0; i < num_axes; i++)
{
Expand All @@ -595,9 +595,9 @@ JoyDevice::~JoyDevice ()
if (enabled)
{
SDL_JoystickClose (filedes);
free (axis);
free (hat);
free (calibration);
delete[] axis;
delete[] hat;
delete[] calibration;
}

enabled = false;
Expand Down
10 changes: 5 additions & 5 deletions gtk/src/gtk_display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1533,10 +1533,10 @@ void S9xDeinitDisplay ()

void S9xReinitDisplay ()
{
uint16 *buffer = NULL;
int width, height;
uint8_t *buffer = NULL;
int width, height;

buffer = (uint16 *) malloc (S9xDisplayDriver::image_size);
buffer = new uint8_t[S9xDisplayDriver::image_size];
memmove (buffer,
driver->get_current_buffer (),
S9xDisplayDriver::image_size);
Expand All @@ -1551,9 +1551,9 @@ void S9xReinitDisplay ()
top_level->last_width = width;
top_level->last_height = height;

driver->push_buffer (buffer);
driver->push_buffer ((uint16_t *)buffer);

free (buffer);
delete[] buffer;
}

bool8 S9xContinueUpdate (int width, int height)
Expand Down
4 changes: 2 additions & 2 deletions gtk/src/gtk_display_driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ class S9xDisplayDriver
Snes9xWindow *window;
Snes9xConfig *config;
GtkWidget *drawing_area;
void *padded_buffer[4];
void *buffer[4];
uint8_t *padded_buffer[4];
uint8_t *buffer[4];
};

#endif /* __GTK_DISPLAY_DRIVER_H*/
12 changes: 6 additions & 6 deletions gtk/src/gtk_display_driver_gtk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,11 @@ S9xGTKDisplayDriver::output (void *src,
int
S9xGTKDisplayDriver::init ()
{
buffer[0] = malloc (image_padded_size);
buffer[1] = malloc (scaled_padded_size);
buffer[0] = new uint8_t[image_padded_size];
buffer[1] = new uint8_t[scaled_padded_size];

padded_buffer[0] = (void *) (((uint8 *) buffer[0]) + image_padded_offset);
padded_buffer[1] = (void *) (((uint8 *) buffer[1]) + scaled_padded_offset);
padded_buffer[0] = &buffer[0][image_padded_offset];
padded_buffer[1] = &buffer[1][scaled_padded_offset];

memset (buffer[0], 0, image_padded_size);
memset (buffer[1], 0, scaled_padded_size);
Expand All @@ -137,8 +137,8 @@ S9xGTKDisplayDriver::deinit ()
padded_buffer[0] = NULL;
padded_buffer[1] = NULL;

free (buffer[0]);
free (buffer[1]);
delete[] buffer[0];
delete[] buffer[1];
}

void
Expand Down
12 changes: 6 additions & 6 deletions gtk/src/gtk_display_driver_opengl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -618,13 +618,13 @@ int S9xOpenGLDisplayDriver::init ()
return -1;
}

buffer[0] = malloc (image_padded_size);
buffer[1] = malloc (scaled_padded_size);
buffer[0] = new uint8_t[image_padded_size];
buffer[1] = new uint8_t[scaled_padded_size];

clear_buffers ();

padded_buffer[0] = (void *) (((uint8 *) buffer[0]) + image_padded_offset);
padded_buffer[1] = (void *) (((uint8 *) buffer[1]) + scaled_padded_offset);
padded_buffer[0] = &buffer[0][image_padded_offset];
padded_buffer[1] = &buffer[1][scaled_padded_offset];

GFX.Screen = (uint16 *) padded_buffer[0];
GFX.Pitch = image_width * image_bpp;
Expand Down Expand Up @@ -680,8 +680,8 @@ void S9xOpenGLDisplayDriver::deinit ()
padded_buffer[0] = NULL;
padded_buffer[1] = NULL;

free (buffer[0]);
free (buffer[1]);
delete[] buffer[0];
delete[] buffer[1];

if (using_pbos)
{
Expand Down
14 changes: 7 additions & 7 deletions gtk/src/gtk_display_driver_xv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -448,11 +448,11 @@ S9xXVDisplayDriver::init ()
desired_width = scaled_max_width;
desired_height = scaled_max_width;

buffer[0] = malloc (image_padded_size);
buffer[1] = malloc (scaled_padded_size);
buffer[0] = new uint8_t[image_padded_size];
buffer[1] = new uint8_t[scaled_padded_size];

padded_buffer[0] = (void *) (((uint8 *) buffer[0]) + image_padded_offset);
padded_buffer[1] = (void *) (((uint8 *) buffer[1]) + scaled_padded_offset);
padded_buffer[0] = &buffer[0][image_padded_offset];
padded_buffer[1] = &buffer[1][scaled_padded_offset];

memset (buffer[0], 0, image_padded_size);
memset (buffer[1], 0, scaled_padded_size);
Expand All @@ -477,8 +477,8 @@ S9xXVDisplayDriver::deinit ()
XFreeColormap (display, xcolormap);
XFree (vi);

free (buffer[0]);
free (buffer[1]);
delete[] buffer[0];
delete[] buffer[1];

shmctl (shm.shmid, IPC_RMID, 0);
shmdt (shm.shmaddr);
Expand Down Expand Up @@ -559,7 +559,7 @@ S9xXVDisplayDriver::get_current_buffer ()
void
S9xXVDisplayDriver::push_buffer (uint16 *src)
{
memmove (GFX.Screen, src, image_size);
memmove (GFX.Screen, src, image_size * image_bpp);
}

void
Expand Down
62 changes: 20 additions & 42 deletions gtk/src/gtk_sound_driver_sdl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,35 +25,33 @@ void S9xSDLSoundDriver::samples_available()
if (Settings.SoundSync && !Settings.TurboMode && !Settings.Mute)
{
mutex.lock();
int samples = buffer->space_empty();
int samples = buffer.space_empty();
mutex.unlock();

while (samples < snes_samples_available)
{
usleep(100);
mutex.lock();
samples = buffer->space_empty();
samples = buffer.space_empty();
mutex.unlock();
}
}

mutex.lock();
buffer->push(temp, snes_samples_available);
buffer.push(temp, snes_samples_available);
mutex.unlock();
}

void S9xSDLSoundDriver::mix(unsigned char *output, int bytes)
{
mutex.lock();
if (buffer->avail() >= bytes >> 1)
buffer->read((int16_t *)output, bytes >> 1);
if (buffer.avail() >= bytes >> 1)
buffer.read((int16_t *)output, bytes >> 1);
mutex.unlock();
}

S9xSDLSoundDriver::S9xSDLSoundDriver()
{
buffer = NULL;
audiospec = NULL;
}

void S9xSDLSoundDriver::init()
Expand All @@ -65,68 +63,48 @@ void S9xSDLSoundDriver::init()
void S9xSDLSoundDriver::terminate()
{
stop();

if (audiospec)
{
SDL_CloseAudio();
if (buffer)
delete buffer;
free(audiospec);
audiospec = NULL;
}

SDL_CloseAudio();
SDL_QuitSubSystem(SDL_INIT_AUDIO);
}

void S9xSDLSoundDriver::start()
{
if (!gui_config->mute_sound)
{
if (audiospec)
{
SDL_PauseAudio(0);
}
SDL_PauseAudio(0);
}
}

void S9xSDLSoundDriver::stop()
{
if (audiospec)
{
SDL_PauseAudio(1);
}
SDL_PauseAudio(1);
}

bool S9xSDLSoundDriver::open_device()
{
audiospec = (SDL_AudioSpec *)malloc(sizeof(SDL_AudioSpec));

audiospec->freq = Settings.SoundPlaybackRate;
audiospec->channels = 2;
audiospec->format = AUDIO_S16SYS;
audiospec->samples = (gui_config->sound_buffer_size * audiospec->freq / 1000) >> 2;
audiospec->callback = sdl_audio_callback;
audiospec->userdata = this;
audiospec = {};
audiospec.freq = Settings.SoundPlaybackRate;
audiospec.channels = 2;
audiospec.format = AUDIO_S16SYS;
audiospec.samples = (gui_config->sound_buffer_size * audiospec.freq / 1000) >> 2;
audiospec.callback = sdl_audio_callback;
audiospec.userdata = this;

printf("SDL sound driver initializing...\n");
printf(" --> (Frequency: %dhz, Latency: %dms)...",
audiospec->freq,
(audiospec->samples * 1000 / audiospec->freq));
audiospec.freq,
(audiospec.samples * 1000 / audiospec.freq));

if (SDL_OpenAudio(audiospec, NULL) < 0)
if (SDL_OpenAudio(&audiospec, NULL) < 0)
{
printf("Failed\n");

free(audiospec);
audiospec = NULL;

return false;
}

printf("OK\n");

buffer = new Resampler(gui_config->sound_buffer_size * audiospec->freq / 500);
buffer->time_ratio(1.0);
buffer.resize(gui_config->sound_buffer_size * audiospec.freq / 500);
buffer.time_ratio(1.0);

S9xSetSamplesAvailableCallback(c_samples_available, this);

Expand Down
4 changes: 2 additions & 2 deletions gtk/src/gtk_sound_driver_sdl.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ class S9xSDLSoundDriver : public S9xSoundDriver
void samples_available();

private:
SDL_AudioSpec *audiospec;
Resampler *buffer;
SDL_AudioSpec audiospec;
Resampler buffer;
std::mutex mutex;
int16_t temp[512];
};
Expand Down

0 comments on commit f8a0fad

Please sign in to comment.