mirror of https://github.com/snes9xgit/snes9x.git
GTK: Use C++ memory allocation.
This commit is contained in:
parent
124594d220
commit
f8a0fadfa1
|
@ -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;
|
||||
|
@ -77,6 +84,9 @@ class Resampler
|
|||
|
||||
inline void clear(void)
|
||||
{
|
||||
if (!buffer)
|
||||
return;
|
||||
|
||||
start = 0;
|
||||
size = 0;
|
||||
memset(buffer, 0, buffer_size * 2);
|
||||
|
|
|
@ -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++)
|
||||
{
|
||||
|
@ -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;
|
||||
|
|
|
@ -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);
|
||||
|
@ -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)
|
||||
|
|
|
@ -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*/
|
||||
|
|
|
@ -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);
|
||||
|
@ -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
|
||||
|
|
|
@ -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;
|
||||
|
@ -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)
|
||||
{
|
||||
|
|
|
@ -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);
|
||||
|
@ -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);
|
||||
|
@ -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
|
||||
|
|
|
@ -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()
|
||||
|
@ -65,16 +63,7 @@ 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);
|
||||
}
|
||||
|
||||
|
@ -82,51 +71,40 @@ 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);
|
||||
|
||||
|
|
|
@ -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];
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue