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

Supporting Windows SDL #178

Open
wants to merge 2 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
8 changes: 7 additions & 1 deletion src/org/recompile/freej2me/Anbu.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,13 @@ public void start(String args[])
{
try
{
args[0] = "/usr/local/bin/sdl_interface";
if (File.separatorChar == '\\') {
// for windows
args[0] = System.getenv("USERPROFILE") + "\\freej2me\\bin\\sdl_interface.exe";
} else {
args[0] = "/usr/local/bin/sdl_interface";
}

proc = new ProcessBuilder(args).start();

keys = proc.getInputStream();
Expand Down
23 changes: 23 additions & 0 deletions src/sdl2/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
cmake_minimum_required(VERSION 3.1)

project(sdl_interface CXX)

find_package(SDL2 REQUIRED CONFIG REQUIRED COMPONENTS SDL2)
find_package(SDL2 REQUIRED CONFIG COMPONENTS SDL2main)

include_directories(${SDL2_INCLUDE_DIRS})

find_path(FreeImage_INCLUDE_DIR FreeImage.h HINTS ${FREEIMAGE_DIR})
find_library(FreeImage_LIBRARY NAMES FreeImage HINTS ${FREEIMAGE_DIR})

include_directories(${FreeImage_INCLUDE_DIR})

add_executable(sdl_interface anbu.cpp)

target_link_libraries(sdl_interface ${SDL2_LIBRARIES})
target_link_libraries(sdl_interface ${FreeImage_LIBRARY})

install(
TARGETS sdl_interface
RUNTIME DESTINATION bin
)
48 changes: 42 additions & 6 deletions src/sdl2/anbu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,13 @@ along with FreeJ2ME. If not, see http://www.gnu.org/licenses/
#include <assert.h>
#include <map>

#ifdef _WIN32
#include <windows.h>
#include <string>
#else
#include <pthread.h>
#endif

#include <SDL2/SDL.h>
#include <FreeImage.h>

Expand All @@ -38,7 +44,13 @@ along with FreeJ2ME. If not, see http://www.gnu.org/licenses/

using namespace std;

#ifdef _WIN32
DWORD t_capturing;
#else
pthread_t t_capturing;
#endif

string bg_image = "";

int angle = 0;
int source_width = 0, source_height = 0;
Expand Down Expand Up @@ -276,8 +288,13 @@ void init(Uint8 r = 0, Uint8 g = 0, Uint8 b = 0)
SDL_RenderSetLogicalSize(mRenderer, display_width, display_height);
}

void startStreaming(string bg_image)
#ifdef _WIN32
DWORD WINAPI startStreaming(LPVOID lpParam)
{
#else
void *startStreaming(void *args)
{
#endif
SDL_Rect dest = getDestinationRect();

loadBackground(bg_image);
Expand All @@ -295,9 +312,15 @@ void startStreaming(string bg_image)

SDL_DestroyTexture(mTexture);
delete[] frame;

#ifdef _WIN32
return 0;
#else
pthread_exit(NULL);
#endif
}

void *startCapturing(void *args)
void startCapturing()
{
int key;
SDL_JoystickEventState(SDL_ENABLE);
Expand Down Expand Up @@ -364,14 +387,12 @@ void *startCapturing(void *args)
fflush(stdout);
}
}
pthread_exit(NULL);
}

/*********************************************************************** Main */
int main(int argc, char* argv[])
{
int c = 0;
string bg_image = "";
Uint8 r = 44, g = 62, b = 80; // Midnight Blue

while (++c < argc)
Expand Down Expand Up @@ -399,15 +420,30 @@ int main(int argc, char* argv[])

init(r, g, b);
bool initialCursorState = SDL_ShowCursor(0) == 1;
if (pthread_create(&t_capturing, 0, &startCapturing, NULL))

#ifdef _WIN32
HANDLE hThreadCapturing;
if ((hThreadCapturing = CreateThread(NULL, 0, &startStreaming, NULL, 0, &t_capturing)) == NULL) {
std::cerr << "Unable to start thread, exiting ..." << endl;
SDL_Quit();
return 1;
}
#else
if (pthread_create(&t_capturing, 0, &startStreaming, NULL))
{
std::cerr << "Unable to start thread, exiting ..." << endl;
SDL_Quit();
return 1;
}
#endif

startStreaming(bg_image);
startCapturing();
#ifdef _WIN32
WaitForSingleObject(hThreadCapturing, INFINITE);
CloseHandle(hThreadCapturing);
#else
pthread_join(t_capturing, NULL);
#endif
SDL_ShowCursor(initialCursorState);
SDL_Quit();
return 0;
Expand Down