Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sa2: first working version with SDL3 #169

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion source/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -284,4 +284,4 @@ add_custom_command(
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_SOURCE_DIR}/bin/*.SYM ${CMAKE_BINARY_DIR}
)

configure_file(linux/config.h.in linux/config.h)
configure_file(linux/linux_config.h.in linux/linux_config.h)
2 changes: 1 addition & 1 deletion source/frontends/common2/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ if ("${ROOT_PATH}" STREQUAL "")
endif()
file(RELATIVE_PATH SHARE_PATH ${CMAKE_INSTALL_FULL_BINDIR} ${CMAKE_INSTALL_FULL_DATADIR}/applewin)

configure_file(config.h.in config.h)
configure_file(common_config.h.in common_config.h)

install(DIRECTORY ${CMAKE_SOURCE_DIR}/resource
DESTINATION ${CMAKE_INSTALL_DATADIR}/applewin)
2 changes: 1 addition & 1 deletion source/frontends/common2/gnuframe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#endif

#include "Core.h"
#include "config.h"
#include "common_config.h"

namespace
{
Expand Down
2 changes: 1 addition & 1 deletion source/frontends/common2/programoptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ namespace common2
bool imgui = true; // use imgui renderer
std::optional<Geometry> geometry; // must be initialised with defaults
bool aspectRatio = false; // preserve aspect ratio
int glSwapInterval = 1; // SDL_GL_SetSwapInterval
int glSwapInterval = -1; // SDL_GL_SetSwapInterval
std::optional<int> gameControllerIndex;
std::string gameControllerMappingFile;
std::string audioDeviceName;
Expand Down
57 changes: 46 additions & 11 deletions source/frontends/sdl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,48 @@ add_executable(sa2)
set(IMGUI_PATH "imgui/imgui")
set(IMGUI_CLUB_PATH "imgui/imgui_club")

find_package(SDL2 REQUIRED)
# we should use find_package, but Ubuntu does not provide it for SDL2_image
pkg_search_module(SDL2_IMAGE REQUIRED SDL2_image)
option(SA2_SDL3 "Use SDL3" OFF)

if (SA2_SDL3)
message("sa2: using SDL3")
find_package(SDL3 REQUIRED)
find_package(SDL3_image REQUIRED)

target_link_libraries(sa2 PRIVATE
SDL3::SDL3
SDL3_image::SDL3_image
)

target_sources(sa2 PRIVATE
${IMGUI_PATH}/backends/imgui_impl_sdl3.cpp
)

target_compile_definitions(sa2 PRIVATE SDL_ENABLE_OLD_NAMES)
else()
message("sa2: using SDL2")
find_package(SDL2 REQUIRED)
# we should use find_package, but Ubuntu does not provide it for SDL2_image
pkg_search_module(SDL2_IMAGE REQUIRED SDL2_image)

target_include_directories(sa2 PRIVATE
${SDL2_INCLUDE_DIRS}
${SDL2_IMAGE_INCLUDE_DIRS}
)

target_link_libraries(sa2 PRIVATE
${SDL2_LIBRARIES}
${SDL2_IMAGE_LIBRARIES}
)

target_link_directories(sa2 PRIVATE
${SDL2_LIBRARY_DIRS}
${SDL2_IMAGE_LIBRARY_DIRS}
)

target_sources(sa2 PRIVATE
${IMGUI_PATH}/backends/imgui_impl_sdl2.cpp
)
endif()

if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
# only OpenGL supported on MacOS
Expand All @@ -35,6 +73,7 @@ set(SOURCE_FILES
utils.cpp
sdlframe.cpp
processfile.cpp
sdlcompat.cpp
renderer/sdlrendererframe.cpp
)

Expand All @@ -44,21 +83,17 @@ set(HEADER_FILES
utils.h
sdlframe.h
processfile.h
sdlcompat.h
renderer/sdlrendererframe.h
)


target_compile_features(sa2 PUBLIC cxx_std_17)

target_include_directories(sa2 PRIVATE
${SDL2_INCLUDE_DIRS}
${SDL2_IMAGE_INCLUDE_DIRS}
${GLES2_INCLUDE_DIRS}
)

target_link_libraries(sa2 PRIVATE
${SDL2_LIBRARIES}
${SDL2_IMAGE_LIBRARIES}
${GLES2_LIBRARIES}
OpenGL::GL
appleii
Expand All @@ -67,8 +102,6 @@ target_link_libraries(sa2 PRIVATE
)

target_link_directories(sa2 PRIVATE
${SDL2_LIBRARY_DIRS}
${SDL2_IMAGE_LIBRARY_DIRS}
${GLES2_LIBRARY_DIRS}
)

Expand Down Expand Up @@ -104,13 +137,13 @@ target_sources(sa2 PRIVATE
${IMGUI_PATH}/imgui_draw.cpp
${IMGUI_PATH}/imgui_tables.cpp
${IMGUI_PATH}/imgui_widgets.cpp
${IMGUI_PATH}/backends/imgui_impl_sdl2.cpp
${IMGUI_PATH}/backends/imgui_impl_opengl3.cpp

${IMGUI_CLUB_PATH}/imgui_memory_editor/imgui_memory_editor.h
)

target_include_directories(sa2 PRIVATE
${CMAKE_CURRENT_BINARY_DIR} # for config.h
${IMGUI_PATH}
${IMGUI_PATH}/backends
${IMGUI_CLUB_PATH}/imgui_memory_editor
Expand All @@ -120,5 +153,7 @@ target_compile_definitions(sa2 PRIVATE
IMGUI_USER_CONFIG="frontends/sdl/imgui/imconfig.h"
)

configure_file(sa2_config.h.in sa2_config.h)

install(TARGETS sa2
DESTINATION bin)
2 changes: 1 addition & 1 deletion source/frontends/sdl/gamepad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace sa2

std::shared_ptr<Gamepad> Gamepad::create(const std::optional<int> & index, const std::string & mappingFile)
{
if (!index && SDL_NumJoysticks() <= 0)
if (!index && !compat::getNumJoysticks() <= 0)
{
// default and no joysticks available
return std::shared_ptr<Gamepad>();
Expand Down
2 changes: 1 addition & 1 deletion source/frontends/sdl/gamepad.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include "linux/paddle.h"
#include <SDL.h>
#include "frontends/sdl/sdlcompat.h"

#include <vector>
#include <memory>
Expand Down
12 changes: 8 additions & 4 deletions source/frontends/sdl/imgui/glselector.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include "frontends/sdl/sdlcompat.h"

#if defined(IMGUI_IMPL_OPENGL_ES2)

// Pi3 with Fake KMS
Expand Down Expand Up @@ -44,8 +46,6 @@

#elif defined(__APPLE__)

#include <SDL_opengl.h>

#define SA2_CONTEXT_FLAGS SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG
#define SA2_CONTEXT_PROFILE_MASK SDL_GL_CONTEXT_PROFILE_CORE
#define SA2_CONTEXT_MAJOR_VERSION 3
Expand All @@ -56,8 +56,6 @@

#else

#include <SDL_opengl.h>

#define SA2_CONTEXT_FLAGS 0
#define SA2_CONTEXT_PROFILE_MASK SDL_GL_CONTEXT_PROFILE_CORE
#define SA2_CONTEXT_MAJOR_VERSION 3
Expand All @@ -69,7 +67,13 @@
#endif

#include "imgui.h"

#if SDL_VERSION_ATLEAST(3, 0, 0)
#include "imgui_impl_sdl3.h"
#else
#include "imgui_impl_sdl2.h"
#endif

#include "imgui_impl_opengl3.h"

#include "imgui_memory_editor.h"
12 changes: 6 additions & 6 deletions source/frontends/sdl/imgui/sdlimguiframe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ namespace sa2
const SDL_WindowFlags windowFlags = (SDL_WindowFlags)(SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
const common2::Geometry geometry = getGeometryOrDefault(options.geometry);

myWindow.reset(SDL_CreateWindow(g_pAppTitle.c_str(), geometry.x, geometry.y, geometry.width, geometry.height, windowFlags), SDL_DestroyWindow);
myWindow.reset(compat::createWindow(g_pAppTitle.c_str(), geometry, windowFlags), SDL_DestroyWindow);
if (!myWindow)
{
throw std::runtime_error(decorateSDLError("SDL_CreateWindow"));
Expand Down Expand Up @@ -117,7 +117,7 @@ namespace sa2

ImGui::StyleColorsDark();

ImGui_ImplSDL2_InitForOpenGL(myWindow.get(), myGLContext);
ImGui_ImplSDLX_InitForOpenGL(myWindow.get(), myGLContext);
ImGui_ImplOpenGL3_Init();

myDeadTopZone = 0;
Expand All @@ -128,7 +128,7 @@ namespace sa2
{
glDeleteTextures(1, &myTexture);
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplSDL2_Shutdown();
ImGui_ImplSDLX_Shutdown();
ImGui::DestroyContext();
SDL_GL_DeleteContext(myGLContext);
}
Expand Down Expand Up @@ -231,7 +231,7 @@ namespace sa2
{
myPresenting = true;
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplSDL2_NewFrame();
ImGui_ImplSDLX_NewFrame();
ImGui::NewFrame();

if (!myShowMouseCursor)
Expand All @@ -253,7 +253,7 @@ namespace sa2

void SDLImGuiFrame::ProcessSingleEvent(const SDL_Event & event, bool & quit)
{
ImGui_ImplSDL2_ProcessEvent(&event);
ImGui_ImplSDLX_ProcessEvent(&event);

switch (event.type)
{
Expand Down Expand Up @@ -293,7 +293,7 @@ namespace sa2
{
const size_t modifiers = getCanonicalModifiers(key);

switch (key.keysym.sym)
switch (SA2_KEY_CODE(key))
{
case SDLK_F8:
{
Expand Down
8 changes: 7 additions & 1 deletion source/frontends/sdl/imgui/sdlsettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -788,13 +788,19 @@ namespace sa2
ImGui::Text("Debugger %d.%d.%d.%d", nMajor, nMinor, nFixMajor, nFixMinor);

ImGui::Separator();

#if SDL_VERSION_ATLEAST(3, 0, 0)
const int sdlVersion = SDL_GetVersion();
ImGui::Text("SDL version %d", sdlVersion);
#else
SDL_version sdl;
SDL_GetVersion(&sdl);
ImGui::Text("SDL version %d.%d.%d", sdl.major, sdl.minor, sdl.patch);
#endif
ImGui::Text("Dear ImGui %s", ImGui::GetVersion());

ImGui::Separator();
const int glSwap = SDL_GL_GetSwapInterval();
const int glSwap = compat::getGLSwapInterval();
ImGui::Text("GL Swap: %d", glSwap);
ImGuiIO& io = ImGui::GetIO();
ImGui::Text("FPS: %d", int(io.Framerate));
Expand Down
21 changes: 6 additions & 15 deletions source/frontends/sdl/main.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#include <SDL.h>

#include <iostream>
#include <memory>
#include <iomanip>
Expand All @@ -14,6 +12,7 @@
#include "frontends/common2/timer.h"
#include "frontends/sdl/gamepad.h"
#include "frontends/sdl/sdirectsound.h"
#include "frontends/sdl/sdlcompat.h"
#include "frontends/sdl/utils.h"

#include "frontends/sdl/renderer/sdlrendererframe.h"
Expand All @@ -31,16 +30,8 @@ namespace

int getRefreshRate()
{
SDL_DisplayMode current;

const int should_be_zero = SDL_GetCurrentDisplayMode(0, &current);

if (should_be_zero)
{
throw std::runtime_error(sa2::decorateSDLError("SDL_GetCurrentDisplayMode"));
}

return current.refresh_rate ? current.refresh_rate : 60;
const SDL_DisplayMode * current = sa2::compat::getCurrentDisplayMode();
return current->refresh_rate ? current->refresh_rate : 60;
}

struct Data
Expand Down Expand Up @@ -82,7 +73,7 @@ void run_sdl(int argc, const char * argv [])
frame = std::make_shared<sa2::SDLRendererFrame>(options);
}

std::cerr << "Default GL swap interval: " << SDL_GL_GetSwapInterval() << std::endl;
std::cerr << "GL swap interval: " << sa2::compat::getGLSwapInterval() << std::endl;

const common2::CommonInitialisation init(frame, paddle, options);

Expand Down Expand Up @@ -168,8 +159,8 @@ void run_sdl(int argc, const char * argv [])
int main(int argc, const char * argv [])
{
//First we need to start up SDL, and make sure it went ok
const Uint32 flags = SDL_INIT_VIDEO | SDL_INIT_GAMECONTROLLER | SDL_INIT_AUDIO | SDL_INIT_TIMER | SDL_INIT_EVENTS;
if (SDL_Init(flags) != 0)
const Uint32 flags = SDL_INIT_VIDEO | SDL_INIT_GAMEPAD | SDL_INIT_AUDIO | SDL_INIT_EVENTS;
if (!SDL_Init(flags))
{
std::cerr << "SDL_Init Error: " << SDL_GetError() << std::endl;
return 1;
Expand Down
18 changes: 16 additions & 2 deletions source/frontends/sdl/processfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,24 +95,38 @@ namespace
return;
}

SDL_AudioCVT cvt;
// tested with all formats from https://asciiexpress.net/
// 8 bit mono is just enough
// TAPEIN will interpolate so we do not need to resample at a higher frequency
const SDL_AudioFormat format = sizeof(CassetteTape::tape_data_t) == 1 ? AUDIO_S8 : AUDIO_S16SYS;

#if SDL_VERSION_ATLEAST(3, 0, 0)
const SDL_AudioSpec dstSpec = { format, wavSpec.channels, wavSpec.freq };
Uint8 *dstData = NULL;
int dstLen = 0;
if (SDL_ConvertAudioSamples(&wavSpec, wavBuffer, wavLength, &dstSpec, &dstData, &dstLen) < 0)
{
frame->FrameMessageBox("Could not convert wav file", "ERROR", MB_OK);
SDL_free(dstData);
}
std::vector<CassetteTape::tape_data_t> output(dstLen / sizeof(CassetteTape::tape_data_t));
std::memcpy(output.data(), wavBuffer, dstLen);
#else
SDL_AudioCVT cvt;
const int res = SDL_BuildAudioCVT(&cvt, wavSpec.format, wavSpec.channels, wavSpec.freq, format, 1, wavSpec.freq);
cvt.len = wavLength;
std::vector<CassetteTape::tape_data_t> output(cvt.len_mult * cvt.len / sizeof(CassetteTape::tape_data_t));
std::memcpy(output.data(), wavBuffer, cvt.len);
SDL_FreeWAV(wavBuffer);

if (res)
{
cvt.buf = reinterpret_cast<Uint8 *>(output.data());
SDL_ConvertAudio(&cvt);
output.resize(cvt.len_cvt / sizeof(CassetteTape::tape_data_t));
}
#endif

SDL_FreeWAV(wavBuffer);
CassetteTape::instance().setData(filename, output, wavSpec.freq);
}

Expand Down
Loading
Loading