Type prefixes (#34)

* update documentation

* Fix typo

* Added some examples

* changed window names

* Minor fix

* Added mfb_update_events to all platforms. Checked on Windows, X11 and Wayland

* simplify CMake

* Upgrade to CMake 3.5, simplify script and generalize it
Now the users only have to add_directory and link_libraries(minifb) in CMake

* Renamed typo scrool by scroll
Added some checks
Removed some warnings

* working Windows, X11 and Wayland

* fix issue 32

Co-authored-by: Carlos Aragones <>
Co-authored-by: caragones <carlos.aragonesmartinez-external@gemalto.com>
This commit is contained in:
Carlos Aragonés 2020-03-06 07:06:54 +01:00 committed by GitHub
parent 4286f055c6
commit 6b30baa7e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 1157 additions and 1046 deletions

View File

@ -1,159 +1,159 @@
cmake_minimum_required(VERSION 3.5)
set(PROJECT_NAME MiniFB)
project(${PROJECT_NAME})
message("Processing " ${PROJECT_NAME})
set(SrcLib
include/MiniFB.h
include/MiniFB_cpp.h
include/MiniFB_enums.h
src/MiniFB_common.c
src/MiniFB_cpp.cpp
src/MiniFB_internal.c
src/MiniFB_internal.h
src/WindowData.h
)
set(SrcWindows
src/windows/WinMiniFB.c
src/windows/WindowData_Win.h
)
set(SrcMacOSX
src/macosx/MacMiniFB.m
src/macosx/OSXWindow.h
src/macosx/OSXWindow.m
src/macosx/OSXWindowFrameView.h
src/macosx/OSXWindowFrameView.m
src/macosx/WindowData_OSX.h
)
set(SrcWayland
src/wayland/WaylandMiniFB.c
src/wayland/WindowData_Way.h
)
set(SrcX11
src/x11/X11MiniFB.c
src/x11/WindowData_X11.h
)
# Avoid RelWithDebInfo and MinSizeRel
#--------------------------------------
set(CMAKE_CONFIGURATION_TYPES "Debug;Release" CACHE STRING "" FORCE)
# Define Release by default
#--------------------------------------
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Release")
message(STATUS "Build type not specified: Use Release by default")
endif(NOT CMAKE_BUILD_TYPE)
# Set GCC/Clang flags
#--------------------------------------
if (NOT MSVC)
# Avoid default flag values
#--------------------------------------
set(CMAKE_C_FLAGS "" CACHE STRING "" FORCE)
set(CMAKE_C_FLAGS_DEBUG "" CACHE STRING "" FORCE)
set(CMAKE_C_FLAGS_RELEASE "" CACHE STRING "" FORCE)
set(CMAKE_CXX_FLAGS "" CACHE STRING "" FORCE)
set(CMAKE_CXX_FLAGS_DEBUG "" CACHE STRING "" FORCE)
set(CMAKE_CXX_FLAGS_RELEASE "" CACHE STRING "" FORCE)
set(CMAKE_OBJC_FLAGS "" CACHE STRING "" FORCE)
set(CMAKE_OBJC_FLAGS_DEBUG "" CACHE STRING "" FORCE)
set(CMAKE_OBJC_FLAGS_RELEASE "" CACHE STRING "" FORCE)
set(CMAKE_OBJCXX_FLAGS "" CACHE STRING "" FORCE)
set(CMAKE_OBJCXX_FLAGS_DEBUG "" CACHE STRING "" FORCE)
set(CMAKE_OBJCXX_FLAGS_RELEASE "" CACHE STRING "" FORCE)
# Set our flags
#--------------------------------------
add_compile_options("$<$<CONFIG:Debug>:-g>")
add_compile_options("$<IF:$<CONFIG:Debug>,-O0,-O2>")
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -pedantic -Wno-switch -Wno-unused-function -Wno-implicit-fallthrough")
set (CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} -std=c++11")
set (CMAKE_OBJC_FLAGS "${CMAKE_C_FLAGS}")
set (CMAKE_OBJCXX_FLAGS "${CMAKE_CXX_FLAGS}")
endif()
# Set default cmake flags
#--------------------------------------
if (APPLE)
OPTION(USE_METAL_API "Build the project using metal API code" ON)
elseif (UNIX)
OPTION(USE_WAYLAND_API "Build the project using wayland API code" OFF)
endif()
# Set compiler/platform specific flags and dependencies
#--------------------------------------
if (MSVC)
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
add_definitions(-D_WIN32_WINNT=0x0600)
list (APPEND SrcLib ${SrcWindows})
elseif (MINGW)
add_definitions(-D_WIN32_WINNT=0x0600)
list(APPEND SrcLib ${SrcWindows})
elseif (APPLE)
if(USE_METAL_API)
add_definitions(-DUSE_METAL_API)
endif()
link_libraries("-framework Cocoa")
link_libraries("-framework QuartzCore")
link_libraries("-framework Metal")
link_libraries("-framework MetalKit")
list(APPEND SrcLib ${SrcMacOSX})
elseif (UNIX)
if(USE_WAYLAND_API)
link_libraries("-lwayland-client")
link_libraries("-lwayland-cursor")
list(APPEND SrcLib ${SrcWayland})
else()
link_libraries("-lX11")
list(APPEND SrcLib ${SrcX11})
endif()
endif()
# Library
#--------------------------------------
add_library(minifb STATIC
${SrcLib}
)
# For all projects
#--------------------------------------
target_include_directories(minifb PUBLIC include)
target_include_directories(minifb PRIVATE src)
link_libraries(minifb)
# Examples
#--------------------------------------
add_executable(noise
tests/noise.c
)
add_executable(input_events
tests/input_events.c
)
add_executable(input_events_cpp
tests/input_events_cpp.cpp
)
add_executable(multiple_windows
tests/multiple_windows.c
)
message("Done " ${PROJECT_NAME})
cmake_minimum_required(VERSION 3.5)
set(PROJECT_NAME MiniFB)
project(${PROJECT_NAME})
message("Processing " ${PROJECT_NAME})
set(SrcLib
include/MiniFB.h
include/MiniFB_cpp.h
include/MiniFB_enums.h
src/MiniFB_common.c
src/MiniFB_cpp.cpp
src/MiniFB_internal.c
src/MiniFB_internal.h
src/WindowData.h
)
set(SrcWindows
src/windows/WinMiniFB.c
src/windows/WindowData_Win.h
)
set(SrcMacOSX
src/macosx/MacMiniFB.m
src/macosx/OSXWindow.h
src/macosx/OSXWindow.m
src/macosx/OSXWindowFrameView.h
src/macosx/OSXWindowFrameView.m
src/macosx/WindowData_OSX.h
)
set(SrcWayland
src/wayland/WaylandMiniFB.c
src/wayland/WindowData_Way.h
)
set(SrcX11
src/x11/X11MiniFB.c
src/x11/WindowData_X11.h
)
# Avoid RelWithDebInfo and MinSizeRel
#--------------------------------------
set(CMAKE_CONFIGURATION_TYPES "Debug;Release" CACHE STRING "" FORCE)
# Define Release by default
#--------------------------------------
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Release")
message(STATUS "Build type not specified: Use Release by default")
endif(NOT CMAKE_BUILD_TYPE)
# Set GCC/Clang flags
#--------------------------------------
if (NOT MSVC)
# Avoid default flag values
#--------------------------------------
set(CMAKE_C_FLAGS "" CACHE STRING "" FORCE)
set(CMAKE_C_FLAGS_DEBUG "" CACHE STRING "" FORCE)
set(CMAKE_C_FLAGS_RELEASE "" CACHE STRING "" FORCE)
set(CMAKE_CXX_FLAGS "" CACHE STRING "" FORCE)
set(CMAKE_CXX_FLAGS_DEBUG "" CACHE STRING "" FORCE)
set(CMAKE_CXX_FLAGS_RELEASE "" CACHE STRING "" FORCE)
set(CMAKE_OBJC_FLAGS "" CACHE STRING "" FORCE)
set(CMAKE_OBJC_FLAGS_DEBUG "" CACHE STRING "" FORCE)
set(CMAKE_OBJC_FLAGS_RELEASE "" CACHE STRING "" FORCE)
set(CMAKE_OBJCXX_FLAGS "" CACHE STRING "" FORCE)
set(CMAKE_OBJCXX_FLAGS_DEBUG "" CACHE STRING "" FORCE)
set(CMAKE_OBJCXX_FLAGS_RELEASE "" CACHE STRING "" FORCE)
# Set our flags
#--------------------------------------
add_compile_options("$<$<CONFIG:Debug>:-g>")
add_compile_options("$<IF:$<CONFIG:Debug>,-O0,-O2>")
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -pedantic -Wno-switch -Wno-unused-function -Wno-implicit-fallthrough")
set (CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} -std=c++11")
set (CMAKE_OBJC_FLAGS "${CMAKE_C_FLAGS}")
set (CMAKE_OBJCXX_FLAGS "${CMAKE_CXX_FLAGS}")
endif()
# Set default cmake flags
#--------------------------------------
if (APPLE)
OPTION(USE_METAL_API "Build the project using metal API code" ON)
elseif (UNIX)
OPTION(USE_WAYLAND_API "Build the project using wayland API code" OFF)
endif()
# Set compiler/platform specific flags and dependencies
#--------------------------------------
if (MSVC)
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
add_definitions(-D_WIN32_WINNT=0x0600)
list (APPEND SrcLib ${SrcWindows})
elseif (MINGW)
add_definitions(-D_WIN32_WINNT=0x0600)
list(APPEND SrcLib ${SrcWindows})
elseif (APPLE)
if(USE_METAL_API)
add_definitions(-DUSE_METAL_API)
endif()
link_libraries("-framework Cocoa")
link_libraries("-framework QuartzCore")
link_libraries("-framework Metal")
link_libraries("-framework MetalKit")
list(APPEND SrcLib ${SrcMacOSX})
elseif (UNIX)
if(USE_WAYLAND_API)
link_libraries("-lwayland-client")
link_libraries("-lwayland-cursor")
list(APPEND SrcLib ${SrcWayland})
else()
link_libraries("-lX11")
list(APPEND SrcLib ${SrcX11})
endif()
endif()
# Library
#--------------------------------------
add_library(minifb STATIC
${SrcLib}
)
# For all projects
#--------------------------------------
target_include_directories(minifb PUBLIC include)
target_include_directories(minifb PRIVATE src)
link_libraries(minifb)
# Examples
#--------------------------------------
add_executable(noise
tests/noise.c
)
add_executable(input_events
tests/input_events.c
)
add_executable(input_events_cpp
tests/input_events_cpp.cpp
)
add_executable(multiple_windows
tests/multiple_windows.c
)
message("Done " ${PROJECT_NAME})

305
README.md
View File

@ -1,142 +1,163 @@
MiniFB
======
MiniFB (Mini FrameBuffer) is a small cross platform library that makes it easy to render (32-bit) pixels in a window. An example is the best way to show how it works:
struct Window *window = mfb_open_ex("my display", 800, 600, WF_RESIZABLE);
if (!window)
return 0;
for (;;)
{
int state;
// TODO: add some fancy rendering to the buffer of size 800 * 600
state = mfb_update(buffer);
if (state < 0)
break;
}
Furthermore, you can add callbacks to the windows:
void active(struct Window *window, bool isActive) {
...
}
void resize(struct Window *window, int width, int height) {
...
// Optionally you can also change the viewport size
mfb_set_viewport(window, x, y, width, height);
}
void keyboard(struct Window *window, Key key, KeyMod mod, bool isPressed) {
...
// Remember to close the window in some way
if(key == KB_KEY_ESCAPE) {
mfb_close(window);
}
}
void char_input(struct Window *window, unsigned int charCode) {
...
}
void mouse_btn(struct Window *window, MouseButton button, KeyMod mod, bool isPressed) {
...
}
// Use wisely this event. It can be sent too often
void mouse_move(struct Window *window, int x, int y) {
...
}
// Mouse wheel
void mouse_scroll(struct Window *window, KeyMod mod, float deltaX, float deltaY) {
...
}
struct Window *window = mfb_open_ex("my display", 800, 600, WF_RESIZABLE);
if (!window)
return 0;
mfb_set_active_callback(window, active);
mfb_set_resize_callback(window, resize);
mfb_set_keyboard_callback(window, keyboard);
mfb_set_char_input_callback(window, char_input);
mfb_set_mouse_button_callback(window, mouse_btn);
mfb_set_mouse_move_callback(window, mouse_move);
mfb_set_mouse_scroll_callback(window, mouse_scroll);
Additionally you can set data per window and recover it
mfb_set_user_data(window, (void *) myData);
...
myData = (someCast *) mfb_get_user_data(window);
First the code creates window with the mfb_open call that is used to display the data, next it's the applications responsibility to allocate a buffer (which has to be at least the size of the window and in 32-bit) Next when calling mfb_update function the buffer will be copied over to the window and displayed. Currently the mfb_update will return -1 if ESC key is pressed but later on it will support to return a key code for a pressed button. See https://github.com/emoon/minifb/blob/master/tests/noise.c for a complete example
MiniFB has been tested on Windows, Mac OS X and Linux but may of course have trouble depending on your setup. Currently the code will not do any converting of data if not a proper 32-bit display can be created.
Build instructions
------------------
MiniFB uses tundra https://github.com/deplinenoise/tundra as build system and is required to build the code as is but not many changes should be needed if you want to use it directly in your own code.
You can also use CMake as build system.
Mac
---
Cocoa and clang is assumed to be installed on the system (downloading latest XCode + installing the command line tools should do the trick) then to build run: tundra2 macosx-clang-debug and you should be able to run the noise example (t2-output/macosx-clang-debug-default/noise)
MacOS X Mojave does not support Cocoa framework as expected. For that reason now you can switch to Metal API.
To enable it just compile defining the preprocessor macro USE_METAL_API.
If you use CMake just enable the flag:
mkdir build
cd build
cmake .. -DUSE_METAL_API=ON
or if you don't want to use Metal API:
mkdir build
cd build
cmake .. -DUSE_METAL_API=OFF
Windows
-------
Visual Studio (ver 2012 express has been tested) tools needed (using the vcvars32.bat (for 32-bit) will set up the enviroment) to build run: tundra2 win32-msvc-debug and you should be able to run noise in t2-output/win32-msvc-debug-default/noise.exe
If you use CMake the Visual Studio project will be generated (2015, 2017 and 2019 have been tested).
x11 (FreeBSD, Linux, *nix)
--------------------------
gcc and x11-dev libs needs to be installed. To build the code run tundra2 x11-gcc-debug and you should be able to run t2-output/x11-gcc-debug-default/noise
If you use CMake just disable the flag:
mkdir build
cd build
cmake .. -DUSE_WAYLAND_API=OFF
wayland (Linux)
--------------------------
Depends on gcc and wayland-client and wayland-cursor. Built using the wayland-gcc variants.
If you use CMake just enable the flag:
mkdir build
cd build
cmake .. -DUSE_WAYLAND_API=ON
MiniFB
======
MiniFB (Mini FrameBuffer) is a small cross platform library that makes it easy to render (32-bit) pixels in a window. An example is the best way to show how it works:
struct mfb_window *window = mfb_open_ex("my display", 800, 600, WF_RESIZABLE);
if (!window)
return 0;
for (;;)
{
int state;
// TODO: add some fancy rendering to the buffer of size 800 * 600
state = mfb_update(buffer);
if (state < 0)
break;
}
Furthermore, you can add callbacks to the windows:
void active(struct mfb_window *window, bool isActive) {
...
}
void resize(struct mfb_window *window, int width, int height) {
...
// Optionally you can also change the viewport size
mfb_set_viewport(window, x, y, width, height);
}
void keyboard(struct mfb_window *window, mfb_key key, mfb_key_mod mod, bool isPressed) {
...
// Remember to close the window in some way
if(key == KB_KEY_ESCAPE) {
mfb_close(window);
}
fprintf(stdout, "%s > keyboard: key: %s (pressed: %d) [key_mod: %x]\n", window_title, mfb_get_key_name(key), isPressed, mod);
}
void char_input(struct mfb_window *window, unsigned int charCode) {
...
}
void mouse_btn(struct mfb_window *window, mfb_mouse_button button, mfb_key_mod mod, bool isPressed) {
...
}
// Use wisely this event. It can be sent too often
void mouse_move(struct mfb_window *window, int x, int y) {
...
}
// Mouse wheel
void mouse_scroll(struct mfb_window *window, mfb_key_mod mod, float deltaX, float deltaY) {
...
}
struct mfb_window *window = mfb_open_ex("my display", 800, 600, WF_RESIZABLE);
if (!window)
return 0;
mfb_set_active_callback(window, active);
mfb_set_resize_callback(window, resize);
mfb_set_keyboard_callback(window, keyboard);
mfb_set_char_input_callback(window, char_input);
mfb_set_mouse_button_callback(window, mouse_btn);
mfb_set_mouse_move_callback(window, mouse_move);
mfb_set_mouse_scroll_callback(window, mouse_scroll);
Additionally you can set data per window and recover it
mfb_set_user_data(window, (void *) myData);
...
myData = (someCast *) mfb_get_user_data(window);
Finally, you can get information about the events in the window directly:
bool mfb_is_window_active(struct mfb_window *window);
unsigned mfb_get_window_width(struct mfb_window *window);
unsigned mfb_get_window_height(struct mfb_window *window);
int mfb_get_mouse_x(struct mfb_window *window); // Last mouse pos X
int mfb_get_mouse_y(struct mfb_window *window); // Last mouse pos Y
float mfb_get_mouse_scroll_x(struct mfb_window *window); // Mouse wheel X as a sum. When you call this function it resets.
float mfb_get_mouse_scroll_y(struct mfb_window *window); // Mouse wheel Y as a sum. When you call this function it resets.
const uint8_t * mfb_get_mouse_button_buffer(struct mfb_window *window); // One byte for every button. Press (1), Release 0. (up to 8 buttons)
const uint8_t * mfb_get_key_buffer(struct mfb_window *window); // One byte for every key. Press (1), Release 0.
First the code creates window with the mfb_open call that is used to display the data, next it's the applications responsibility to allocate a buffer (which has to be at least the size of the window and in 32-bit) Next when calling mfb_update function the buffer will be copied over to the window and displayed. Currently the mfb_update will return -1 if ESC key is pressed but later on it will support to return a key code for a pressed button. See https://github.com/emoon/minifb/blob/master/tests/noise.c for a complete example
MiniFB has been tested on Windows, Mac OS X and Linux but may of course have trouble depending on your setup. Currently the code will not do any converting of data if not a proper 32-bit display can be created.
Build instructions
------------------
MiniFB uses tundra https://github.com/deplinenoise/tundra as build system and is required to build the code as is but not many changes should be needed if you want to use it directly in your own code.
You can also use CMake as build system.
Mac
---
Cocoa and clang is assumed to be installed on the system (downloading latest XCode + installing the command line tools should do the trick) then to build run: tundra2 macosx-clang-debug and you should be able to run the noise example (t2-output/macosx-clang-debug-default/noise)
MacOS X Mojave does not support Cocoa framework as expected. For that reason now you can switch to Metal API.
To enable it just compile defining the preprocessor macro USE_METAL_API.
If you use CMake just enable the flag:
mkdir build
cd build
cmake .. -DUSE_METAL_API=ON
or if you don't want to use Metal API:
mkdir build
cd build
cmake .. -DUSE_METAL_API=OFF
Windows
-------
Visual Studio (ver 2012 express has been tested) tools needed (using the vcvars32.bat (for 32-bit) will set up the enviroment) to build run: tundra2 win32-msvc-debug and you should be able to run noise in t2-output/win32-msvc-debug-default/noise.exe
If you use CMake the Visual Studio project will be generated (2015, 2017 and 2019 have been tested).
x11 (FreeBSD, Linux, *nix)
--------------------------
gcc and x11-dev libs needs to be installed. To build the code run tundra2 x11-gcc-debug and you should be able to run t2-output/x11-gcc-debug-default/noise
If you use CMake just disable the flag:
mkdir build
cd build
cmake .. -DUSE_WAYLAND_API=OFF
wayland (Linux)
--------------------------
Depends on gcc and wayland-client and wayland-cursor. Built using the wayland-gcc variants.
If you use CMake just enable the flag:
mkdir build
cd build
cmake .. -DUSE_WAYLAND_API=ON

View File

@ -14,47 +14,47 @@ extern "C" {
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Create a window that is used to display the buffer sent into the mfb_update function, returns 0 if fails
struct Window * mfb_open(const char *title, unsigned width, unsigned height);
struct Window * mfb_open_ex(const char *title, unsigned width, unsigned height, unsigned flags);
struct mfb_window * mfb_open(const char *title, unsigned width, unsigned height);
struct mfb_window * mfb_open_ex(const char *title, unsigned width, unsigned height, unsigned flags);
// Update the display
// Input buffer is assumed to be a 32-bit buffer of the size given in the open call
// Will return a negative status if something went wrong or the user want to exit
// Also updates the window events
UpdateState mfb_update(struct Window *window, void *buffer);
mfb_update_state mfb_update(struct mfb_window *window, void *buffer);
// Only updates the window events
UpdateState mfb_update_events(struct Window *window);
mfb_update_state mfb_update_events(struct mfb_window *window);
// Close the window
void mfb_close(struct Window *window);
void mfb_close(struct mfb_window *window);
// Set user data
void mfb_set_user_data(struct Window *window, void *user_data);
void * mfb_get_user_data(struct Window *window);
void mfb_set_user_data(struct mfb_window *window, void *user_data);
void * mfb_get_user_data(struct mfb_window *window);
// Set viewport (useful when resize)
bool mfb_set_viewport(struct Window *window, unsigned offset_x, unsigned offset_y, unsigned width, unsigned height);
bool mfb_set_viewport(struct mfb_window *window, unsigned offset_x, unsigned offset_y, unsigned width, unsigned height);
void mfb_set_active_callback(struct Window *window, mfb_active_func callback);
void mfb_set_resize_callback(struct Window *window, mfb_resize_func callback);
void mfb_set_keyboard_callback(struct Window *window, mfb_keyboard_func callback);
void mfb_set_char_input_callback(struct Window *window, mfb_char_input_func callback);
void mfb_set_mouse_button_callback(struct Window *window, mfb_mouse_button_func callback);
void mfb_set_mouse_move_callback(struct Window *window, mfb_mouse_move_func callback);
void mfb_set_mouse_scroll_callback(struct Window *window, mfb_mouse_scroll_func callback);
void mfb_set_active_callback(struct mfb_window *window, mfb_active_func callback);
void mfb_set_resize_callback(struct mfb_window *window, mfb_resize_func callback);
void mfb_set_keyboard_callback(struct mfb_window *window, mfb_keyboard_func callback);
void mfb_set_char_input_callback(struct mfb_window *window, mfb_char_input_func callback);
void mfb_set_mouse_button_callback(struct mfb_window *window, mfb_mouse_button_func callback);
void mfb_set_mouse_move_callback(struct mfb_window *window, mfb_mouse_move_func callback);
void mfb_set_mouse_scroll_callback(struct mfb_window *window, mfb_mouse_scroll_func callback);
const char * mfb_get_key_name(Key key);
const char * mfb_get_key_name(mfb_key key);
bool mfb_is_window_active(struct Window *window);
unsigned mfb_get_window_width(struct Window *window);
unsigned mfb_get_window_height(struct Window *window);
int mfb_get_mouse_x(struct Window *window); // Last mouse pos X
int mfb_get_mouse_y(struct Window *window); // Last mouse pos Y
float mfb_get_mouse_scroll_x(struct Window *window); // Mouse wheel X as a sum. When you call this function it resets.
float mfb_get_mouse_scroll_y(struct Window *window); // Mouse wheel Y as a sum. When you call this function it resets.
const uint8_t * mfb_get_mouse_button_buffer(struct Window *window); // One byte for every button. Press (1), Release 0. (up to 8 buttons)
const uint8_t * mfb_get_key_buffer(struct Window *window); // One byte for every key. Press (1), Release 0.
bool mfb_is_window_active(struct mfb_window *window);
unsigned mfb_get_window_width(struct mfb_window *window);
unsigned mfb_get_window_height(struct mfb_window *window);
int mfb_get_mouse_x(struct mfb_window *window); // Last mouse pos X
int mfb_get_mouse_y(struct mfb_window *window); // Last mouse pos Y
float mfb_get_mouse_scroll_x(struct mfb_window *window); // Mouse wheel X as a sum. When you call this function it resets.
float mfb_get_mouse_scroll_y(struct mfb_window *window); // Mouse wheel Y as a sum. When you call this function it resets.
const uint8_t * mfb_get_mouse_button_buffer(struct mfb_window *window); // One byte for every button. Press (1), Release 0. (up to 8 buttons)
const uint8_t * mfb_get_key_buffer(struct mfb_window *window); // One byte for every key. Press (1), Release 0.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -6,131 +6,131 @@
#include "MiniFB.h"
template <class T>
void mfb_set_active_callback(struct Window *window, T *obj, void (T::*method)(struct Window *, bool));
void mfb_set_active_callback(struct mfb_window *window, T *obj, void (T::*method)(struct mfb_window *, bool));
template <class T>
void mfb_set_resize_callback(struct Window *window, T *obj, void (T::*method)(struct Window *, int, int));
void mfb_set_resize_callback(struct mfb_window *window, T *obj, void (T::*method)(struct mfb_window *, int, int));
template <class T>
void mfb_set_keyboard_callback(struct Window *window, T *obj, void (T::*method)(struct Window *, Key, KeyMod, bool));
void mfb_set_keyboard_callback(struct mfb_window *window, T *obj, void (T::*method)(struct mfb_window *, mfb_key, mfb_key_mod, bool));
template <class T>
void mfb_set_char_input_callback(struct Window *window, T *obj, void (T::*method)(struct Window *, unsigned int));
void mfb_set_char_input_callback(struct mfb_window *window, T *obj, void (T::*method)(struct mfb_window *, unsigned int));
template <class T>
void mfb_set_mouse_button_callback(struct Window *window, T *obj, void (T::*method)(struct Window *, MouseButton, KeyMod, bool));
void mfb_set_mouse_button_callback(struct mfb_window *window, T *obj, void (T::*method)(struct mfb_window *, mfb_mouse_button, mfb_key_mod, bool));
template <class T>
void mfb_set_mouse_move_callback(struct Window *window, T *obj, void (T::*method)(struct Window *, int, int));
void mfb_set_mouse_move_callback(struct mfb_window *window, T *obj, void (T::*method)(struct mfb_window *, int, int));
template <class T>
void mfb_set_mouse_scroll_callback(struct Window *window, T *obj, void (T::*method)(struct Window *, KeyMod, float, float));
void mfb_set_mouse_scroll_callback(struct mfb_window *window, T *obj, void (T::*method)(struct mfb_window *, mfb_key_mod, float, float));
//-------------------------------------
// To avoid clumsy hands
//-------------------------------------
class Stub {
Stub() : m_window(0x0) {}
class mfb_stub {
mfb_stub() : m_window(0x0) {}
template <class T>
friend void mfb_set_active_callback(struct Window *window, T *obj, void (T::*method)(struct Window *, bool));
friend void mfb_set_active_callback(struct mfb_window *window, T *obj, void (T::*method)(struct mfb_window *, bool));
template <class T>
friend void mfb_set_resize_callback(struct Window *window, T *obj, void (T::*method)(struct Window *, int, int));
friend void mfb_set_resize_callback(struct mfb_window *window, T *obj, void (T::*method)(struct mfb_window *, int, int));
template <class T>
friend void mfb_set_mouse_button_callback(struct Window *window, T *obj, void (T::*method)(struct Window *, MouseButton, KeyMod, bool));
friend void mfb_set_mouse_button_callback(struct mfb_window *window, T *obj, void (T::*method)(struct mfb_window *, mfb_mouse_button, mfb_key_mod, bool));
template <class T>
friend void mfb_set_keyboard_callback(struct Window *window, T *obj, void (T::*method)(struct Window *, Key, KeyMod, bool));
friend void mfb_set_keyboard_callback(struct mfb_window *window, T *obj, void (T::*method)(struct mfb_window *, mfb_key, mfb_key_mod, bool));
template <class T>
friend void mfb_set_char_input_callback(struct Window *window, T *obj, void (T::*method)(struct Window *, unsigned int));
friend void mfb_set_char_input_callback(struct mfb_window *window, T *obj, void (T::*method)(struct mfb_window *, unsigned int));
template <class T>
friend void mfb_set_mouse_button_callback(struct Window *window, T *obj, void (T::*method)(struct Window *, MouseButton, KeyMod, bool));
friend void mfb_set_mouse_button_callback(struct mfb_window *window, T *obj, void (T::*method)(struct mfb_window *, mfb_mouse_button, mfb_key_mod, bool));
template <class T>
friend void mfb_set_mouse_move_callback(struct Window *window, T *obj, void (T::*method)(struct Window *, int, int));
friend void mfb_set_mouse_move_callback(struct mfb_window *window, T *obj, void (T::*method)(struct mfb_window *, int, int));
template <class T>
friend void mfb_set_mouse_scroll_callback(struct Window *window, T *obj, void (T::*method)(struct Window *, KeyMod, float, float));
friend void mfb_set_mouse_scroll_callback(struct mfb_window *window, T *obj, void (T::*method)(struct mfb_window *, mfb_key_mod, float, float));
static Stub *GetInstance(struct Window *window);
static mfb_stub *GetInstance(struct mfb_window *window);
static void active_stub(struct Window *window, bool isActive);
static void resize_stub(struct Window *window, int width, int height);
static void keyboard_stub(struct Window *window, Key key, KeyMod mod, bool isPressed);
static void char_input_stub(struct Window *window, unsigned int);
static void mouse_btn_stub(struct Window *window, MouseButton button, KeyMod mod, bool isPressed);
static void mouse_move_stub(struct Window *window, int x, int y);
static void scroll_stub(struct Window *window, KeyMod mod, float deltaX, float deltaY);
static void active_stub(struct mfb_window *window, bool isActive);
static void resize_stub(struct mfb_window *window, int width, int height);
static void keyboard_stub(struct mfb_window *window, mfb_key key, mfb_key_mod mod, bool isPressed);
static void char_input_stub(struct mfb_window *window, unsigned int);
static void mouse_btn_stub(struct mfb_window *window, mfb_mouse_button button, mfb_key_mod mod, bool isPressed);
static void mouse_move_stub(struct mfb_window *window, int x, int y);
static void scroll_stub(struct mfb_window *window, mfb_key_mod mod, float deltaX, float deltaY);
struct Window *m_window;
std::function<void(struct Window *window, bool)> m_active;
std::function<void(struct Window *window, int, int)> m_resize;
std::function<void(struct Window *window, Key, KeyMod, bool)> m_keyboard;
std::function<void(struct Window *window, unsigned int)> m_char_input;
std::function<void(struct Window *window, MouseButton, KeyMod, bool)> m_mouse_btn;
std::function<void(struct Window *window, int, int)> m_mouse_move;
std::function<void(struct Window *window, KeyMod, float, float)> m_scroll;
struct mfb_window *m_window;
std::function<void(struct mfb_window *window, bool)> m_active;
std::function<void(struct mfb_window *window, int, int)> m_resize;
std::function<void(struct mfb_window *window, mfb_key, mfb_key_mod, bool)> m_keyboard;
std::function<void(struct mfb_window *window, unsigned int)> m_char_input;
std::function<void(struct mfb_window *window, mfb_mouse_button, mfb_key_mod, bool)> m_mouse_btn;
std::function<void(struct mfb_window *window, int, int)> m_mouse_move;
std::function<void(struct mfb_window *window, mfb_key_mod, float, float)> m_scroll;
};
//-------------------------------------
template <class T>
inline void mfb_set_active_callback(struct Window *window, T *obj, void (T::*method)(struct Window *window, bool)) {
inline void mfb_set_active_callback(struct mfb_window *window, T *obj, void (T::*method)(struct mfb_window *window, bool)) {
using namespace std::placeholders;
Stub *stub = Stub::GetInstance(window);
mfb_stub *stub = mfb_stub::GetInstance(window);
stub->m_active = std::bind(method, obj, _1, _2);
mfb_set_active_callback(window, Stub::active_stub);
mfb_set_active_callback(window, mfb_stub::active_stub);
}
template <class T>
inline void mfb_set_resize_callback(struct Window *window, T *obj, void (T::*method)(struct Window *window, int, int)) {
inline void mfb_set_resize_callback(struct mfb_window *window, T *obj, void (T::*method)(struct mfb_window *window, int, int)) {
using namespace std::placeholders;
Stub *stub = Stub::GetInstance(window);
mfb_stub *stub = mfb_stub::GetInstance(window);
stub->m_resize = std::bind(method, obj, _1, _2, _3);
mfb_set_resize_callback(window, Stub::resize_stub);
mfb_set_resize_callback(window, mfb_stub::resize_stub);
}
template <class T>
inline void mfb_set_keyboard_callback(struct Window *window, T *obj, void (T::*method)(struct Window *window, Key, KeyMod, bool)) {
inline void mfb_set_keyboard_callback(struct mfb_window *window, T *obj, void (T::*method)(struct mfb_window *window, mfb_key, mfb_key_mod, bool)) {
using namespace std::placeholders;
Stub *stub = Stub::GetInstance(window);
mfb_stub *stub = mfb_stub::GetInstance(window);
stub->m_keyboard = std::bind(method, obj, _1, _2, _3, _4);
mfb_set_keyboard_callback(window, Stub::keyboard_stub);
mfb_set_keyboard_callback(window, mfb_stub::keyboard_stub);
}
template <class T>
inline void mfb_set_char_input_callback(struct Window *window, T *obj, void (T::*method)(struct Window *window, unsigned int)) {
inline void mfb_set_char_input_callback(struct mfb_window *window, T *obj, void (T::*method)(struct mfb_window *window, unsigned int)) {
using namespace std::placeholders;
Stub *stub = Stub::GetInstance(window);
mfb_stub *stub = mfb_stub::GetInstance(window);
stub->m_char_input = std::bind(method, obj, _1, _2);
mfb_set_char_input_callback(window, Stub::char_input_stub);
mfb_set_char_input_callback(window, mfb_stub::char_input_stub);
}
template <class T>
inline void mfb_set_mouse_button_callback(struct Window *window, T *obj, void (T::*method)(struct Window *window, MouseButton, KeyMod, bool)) {
inline void mfb_set_mouse_button_callback(struct mfb_window *window, T *obj, void (T::*method)(struct mfb_window *window, mfb_mouse_button, mfb_key_mod, bool)) {
using namespace std::placeholders;
Stub *stub = Stub::GetInstance(window);
mfb_stub *stub = mfb_stub::GetInstance(window);
stub->m_mouse_btn = std::bind(method, obj, _1, _2, _3, _4);
mfb_set_mouse_button_callback(window, Stub::mouse_btn_stub);
mfb_set_mouse_button_callback(window, mfb_stub::mouse_btn_stub);
}
template <class T>
inline void mfb_set_mouse_move_callback(struct Window *window, T *obj, void (T::*method)(struct Window *window, int, int)) {
inline void mfb_set_mouse_move_callback(struct mfb_window *window, T *obj, void (T::*method)(struct mfb_window *window, int, int)) {
using namespace std::placeholders;
Stub *stub = Stub::GetInstance(window);
mfb_stub *stub = mfb_stub::GetInstance(window);
stub->m_mouse_move = std::bind(method, obj, _1, _2, _3);
mfb_set_mouse_move_callback(window, Stub::mouse_move_stub);
mfb_set_mouse_move_callback(window, mfb_stub::mouse_move_stub);
}
template <class T>
inline void mfb_set_mouse_scroll_callback(struct Window *window, T *obj, void (T::*method)(struct Window *window, KeyMod, float, float)) {
inline void mfb_set_mouse_scroll_callback(struct mfb_window *window, T *obj, void (T::*method)(struct mfb_window *window, mfb_key_mod, float, float)) {
using namespace std::placeholders;
Stub *stub = Stub::GetInstance(window);
mfb_stub *stub = mfb_stub::GetInstance(window);
stub->m_scroll = std::bind(method, obj, _1, _2, _3, _4);
mfb_set_mouse_scroll_callback(window, Stub::scroll_stub);
mfb_set_mouse_scroll_callback(window, mfb_stub::scroll_stub);
}
#endif

View File

@ -10,7 +10,7 @@ typedef enum {
STATE_INVALID_WINDOW = -2,
STATE_INVALID_BUFFER = -3,
STATE_INTERNAL_ERROR = -4,
} UpdateState;
} mfb_update_state;
typedef enum {
MOUSE_BTN_0, // No mouse button
@ -21,7 +21,7 @@ typedef enum {
MOUSE_BTN_5,
MOUSE_BTN_6,
MOUSE_BTN_7,
} MouseButton;
} mfb_mouse_button;
#define MOUSE_LEFT MOUSE_BTN_1
#define MOUSE_RIGHT MOUSE_BTN_2
#define MOUSE_MIDDLE MOUSE_BTN_3
@ -150,7 +150,7 @@ typedef enum {
KB_KEY_RIGHT_ALT = 346,
KB_KEY_RIGHT_SUPER = 347,
KB_KEY_MENU = 348
} Key;
} mfb_key;
#define KB_KEY_LAST KB_KEY_MENU
typedef enum {
@ -160,7 +160,7 @@ typedef enum {
KB_MOD_SUPER = 0x0008,
KB_MOD_CAPS_LOCK = 0x0010,
KB_MOD_NUM_LOCK = 0x0020
} KeyMod;
} mfb_key_mod;
typedef enum {
WF_RESIZABLE = 0x01,
@ -168,17 +168,17 @@ typedef enum {
WF_FULLSCREEN_DESKTOP = 0x04,
WF_BORDERLESS = 0x08,
WF_ALWAYS_ON_TOP = 0x10,
} WindowFlags;
} mfb_window_flags;
// Opaque pointer
struct Window;
struct mfb_window;
// Event callbacks
typedef void(*mfb_active_func)(struct Window *window, bool isActive);
typedef void(*mfb_resize_func)(struct Window *window, int width, int height);
typedef void(*mfb_keyboard_func)(struct Window *window, Key key, KeyMod mod, bool isPressed);
typedef void(*mfb_char_input_func)(struct Window *window, unsigned int code);
typedef void(*mfb_mouse_button_func)(struct Window *window, MouseButton button, KeyMod mod, bool isPressed);
typedef void(*mfb_mouse_move_func)(struct Window *window, int x, int y);
typedef void(*mfb_mouse_scroll_func)(struct Window *window, KeyMod mod, float deltaX, float deltaY);
typedef void(*mfb_active_func)(struct mfb_window *window, bool isActive);
typedef void(*mfb_resize_func)(struct mfb_window *window, int width, int height);
typedef void(*mfb_keyboard_func)(struct mfb_window *window, mfb_key key, mfb_key_mod mod, bool isPressed);
typedef void(*mfb_char_input_func)(struct mfb_window *window, unsigned int code);
typedef void(*mfb_mouse_button_func)(struct mfb_window *window, mfb_mouse_button button, mfb_key_mod mod, bool isPressed);
typedef void(*mfb_mouse_move_func)(struct mfb_window *window, int x, int y);
typedef void(*mfb_mouse_scroll_func)(struct mfb_window *window, mfb_key_mod mod, float deltaX, float deltaY);

View File

@ -7,7 +7,8 @@ short int g_keycodes[512] = { 0 };
//-------------------------------------
//-------------------------------------
void mfb_set_active_callback(struct Window *window, mfb_active_func callback) {
void
mfb_set_active_callback(struct mfb_window *window, mfb_active_func callback) {
if(window != 0x0) {
SWindowData *window_data = (SWindowData *) window;
window_data->active_func = callback;
@ -15,7 +16,8 @@ void mfb_set_active_callback(struct Window *window, mfb_active_func callback) {
}
//-------------------------------------
void mfb_set_resize_callback(struct Window *window, mfb_resize_func callback) {
void
mfb_set_resize_callback(struct mfb_window *window, mfb_resize_func callback) {
if(window != 0x0) {
SWindowData *window_data = (SWindowData *) window;
window_data->resize_func = callback;
@ -23,7 +25,8 @@ void mfb_set_resize_callback(struct Window *window, mfb_resize_func callback) {
}
//-------------------------------------
void mfb_set_keyboard_callback(struct Window *window, mfb_keyboard_func callback) {
void
mfb_set_keyboard_callback(struct mfb_window *window, mfb_keyboard_func callback) {
if(window != 0x0) {
SWindowData *window_data = (SWindowData *) window;
window_data->keyboard_func = callback;
@ -31,7 +34,8 @@ void mfb_set_keyboard_callback(struct Window *window, mfb_keyboard_func callback
}
//-------------------------------------
void mfb_set_char_input_callback(struct Window *window, mfb_char_input_func callback) {
void
mfb_set_char_input_callback(struct mfb_window *window, mfb_char_input_func callback) {
if(window != 0x0) {
SWindowData *window_data = (SWindowData *) window;
window_data->char_input_func = callback;
@ -39,7 +43,8 @@ void mfb_set_char_input_callback(struct Window *window, mfb_char_input_func call
}
//-------------------------------------
void mfb_set_mouse_button_callback(struct Window *window, mfb_mouse_button_func callback) {
void
mfb_set_mouse_button_callback(struct mfb_window *window, mfb_mouse_button_func callback) {
if(window != 0x0) {
SWindowData *window_data = (SWindowData *) window;
window_data->mouse_btn_func = callback;
@ -47,7 +52,8 @@ void mfb_set_mouse_button_callback(struct Window *window, mfb_mouse_button_func
}
//-------------------------------------
void mfb_set_mouse_move_callback(struct Window *window, mfb_mouse_move_func callback) {
void
mfb_set_mouse_move_callback(struct mfb_window *window, mfb_mouse_move_func callback) {
if(window != 0x0) {
SWindowData *window_data = (SWindowData *) window;
window_data->mouse_move_func = callback;
@ -55,7 +61,8 @@ void mfb_set_mouse_move_callback(struct Window *window, mfb_mouse_move_func call
}
//-------------------------------------
void mfb_set_mouse_scroll_callback(struct Window *window, mfb_mouse_scroll_func callback) {
void
mfb_set_mouse_scroll_callback(struct mfb_window *window, mfb_mouse_scroll_func callback) {
if(window != 0x0) {
SWindowData *window_data = (SWindowData *) window;
window_data->mouse_wheel_func = callback;
@ -63,7 +70,8 @@ void mfb_set_mouse_scroll_callback(struct Window *window, mfb_mouse_scroll_func
}
//-------------------------------------
void mfb_set_user_data(struct Window *window, void *user_data) {
void
mfb_set_user_data(struct mfb_window *window, void *user_data) {
if(window != 0x0) {
SWindowData *window_data = (SWindowData *) window;
window_data->user_data = user_data;
@ -71,7 +79,8 @@ void mfb_set_user_data(struct Window *window, void *user_data) {
}
//-------------------------------------
void *mfb_get_user_data(struct Window *window) {
void *
mfb_get_user_data(struct mfb_window *window) {
if(window != 0x0) {
SWindowData *window_data = (SWindowData *) window;
return window_data->user_data;
@ -81,7 +90,8 @@ void *mfb_get_user_data(struct Window *window) {
}
//-------------------------------------
void mfb_close(struct Window *window) {
void
mfb_close(struct mfb_window *window) {
if(window != 0x0) {
SWindowData *window_data = (SWindowData *) window;
window_data->close = true;
@ -89,7 +99,8 @@ void mfb_close(struct Window *window) {
}
//-------------------------------------
void keyboard_default(struct Window *window, Key key, KeyMod mod, bool isPressed) {
void
keyboard_default(struct mfb_window *window, mfb_key key, mfb_key_mod mod, bool isPressed) {
kUnused(mod);
kUnused(isPressed);
if (key == KB_KEY_ESCAPE) {
@ -99,7 +110,8 @@ void keyboard_default(struct Window *window, Key key, KeyMod mod, bool isPressed
}
//-------------------------------------
bool mfb_is_window_active(struct Window *window) {
bool
mfb_is_window_active(struct mfb_window *window) {
if(window != 0x0) {
SWindowData *window_data = (SWindowData *) window;
return window_data->is_active;
@ -108,7 +120,8 @@ bool mfb_is_window_active(struct Window *window) {
}
//-------------------------------------
unsigned mfb_get_window_width(struct Window *window) {
unsigned
mfb_get_window_width(struct mfb_window *window) {
if(window != 0x0) {
SWindowData *window_data = (SWindowData *) window;
return window_data->window_width;
@ -117,7 +130,8 @@ unsigned mfb_get_window_width(struct Window *window) {
}
//-------------------------------------
unsigned mfb_get_window_height(struct Window *window) {
unsigned
mfb_get_window_height(struct mfb_window *window) {
if(window != 0x0) {
SWindowData *window_data = (SWindowData *) window;
return window_data->window_height;
@ -126,7 +140,8 @@ unsigned mfb_get_window_height(struct Window *window) {
}
//-------------------------------------
int mfb_get_mouse_x(struct Window *window) {
int
mfb_get_mouse_x(struct mfb_window *window) {
if(window != 0x0) {
SWindowData *window_data = (SWindowData *) window;
return window_data->mouse_pos_x;
@ -135,7 +150,8 @@ int mfb_get_mouse_x(struct Window *window) {
}
//-------------------------------------
int mfb_get_mouse_y(struct Window *window) {
int
mfb_get_mouse_y(struct mfb_window *window) {
if(window != 0x0) {
SWindowData *window_data = (SWindowData *) window;
return window_data->mouse_pos_y;
@ -144,7 +160,8 @@ int mfb_get_mouse_y(struct Window *window) {
}
//-------------------------------------
float mfb_get_mouse_scroll_x(struct Window *window) {
float
mfb_get_mouse_scroll_x(struct mfb_window *window) {
if(window != 0x0) {
SWindowData *window_data = (SWindowData *) window;
return window_data->mouse_wheel_x;
@ -153,7 +170,8 @@ float mfb_get_mouse_scroll_x(struct Window *window) {
}
//-------------------------------------
float mfb_get_mouse_scroll_y(struct Window *window) {
float
mfb_get_mouse_scroll_y(struct mfb_window *window) {
if(window != 0x0) {
SWindowData *window_data = (SWindowData *) window;
return window_data->mouse_wheel_y;
@ -162,7 +180,8 @@ float mfb_get_mouse_scroll_y(struct Window *window) {
}
//-------------------------------------
const uint8_t * mfb_get_mouse_button_buffer(struct Window *window) {
const uint8_t *
mfb_get_mouse_button_buffer(struct mfb_window *window) {
if(window != 0x0) {
SWindowData *window_data = (SWindowData *) window;
return window_data->mouse_button_status;
@ -171,7 +190,8 @@ const uint8_t * mfb_get_mouse_button_buffer(struct Window *window) {
}
//-------------------------------------
const uint8_t * mfb_get_key_buffer(struct Window *window) {
const uint8_t *
mfb_get_key_buffer(struct mfb_window *window) {
if(window != 0x0) {
SWindowData *window_data = (SWindowData *) window;
return window_data->key_status;
@ -179,10 +199,9 @@ const uint8_t * mfb_get_key_buffer(struct Window *window) {
return 0;
}
//-------------------------------------
const char * mfb_get_key_name(Key key) {
const char *
mfb_get_key_name(mfb_key key) {
switch (key)
{

View File

@ -2,53 +2,68 @@
#include <MiniFB_enums.h>
#include <vector>
Stub *
Stub::GetInstance(struct Window *window) {
static std::vector<Stub *> s_instances;
//-------------------------------------
mfb_stub *
mfb_stub::GetInstance(struct mfb_window *window) {
static std::vector<mfb_stub *> s_instances;
for(Stub *instance : s_instances) {
for(mfb_stub *instance : s_instances) {
if(instance->m_window == window) {
return instance;
}
}
s_instances.push_back(new Stub);
s_instances.push_back(new mfb_stub);
s_instances.back()->m_window = window;
return s_instances.back();
}
void Stub::active_stub(struct Window *window, bool isActive) {
Stub *stub = Stub::GetInstance(window);
//-------------------------------------
void
mfb_stub::active_stub(struct mfb_window *window, bool isActive) {
mfb_stub *stub = mfb_stub::GetInstance(window);
stub->m_active(window, isActive);
}
void Stub::resize_stub(struct Window *window, int width, int height) {
Stub *stub = Stub::GetInstance(window);
//-------------------------------------
void
mfb_stub::resize_stub(struct mfb_window *window, int width, int height) {
mfb_stub *stub = mfb_stub::GetInstance(window);
stub->m_resize(window, width, height);
}
void Stub::keyboard_stub(struct Window *window, Key key, KeyMod mod, bool isPressed) {
Stub *stub = Stub::GetInstance(window);
//-------------------------------------
void
mfb_stub::keyboard_stub(struct mfb_window *window, mfb_key key, mfb_key_mod mod, bool isPressed) {
mfb_stub *stub = mfb_stub::GetInstance(window);
stub->m_keyboard(window, key, mod, isPressed);
}
void Stub::char_input_stub(struct Window *window, unsigned int code) {
Stub *stub = Stub::GetInstance(window);
//-------------------------------------
void
mfb_stub::char_input_stub(struct mfb_window *window, unsigned int code) {
mfb_stub *stub = mfb_stub::GetInstance(window);
stub->m_char_input(window, code);
}
void Stub::mouse_btn_stub(struct Window *window, MouseButton button, KeyMod mod, bool isPressed) {
Stub *stub = Stub::GetInstance(window);
//-------------------------------------
void
mfb_stub::mouse_btn_stub(struct mfb_window *window, mfb_mouse_button button, mfb_key_mod mod, bool isPressed) {
mfb_stub *stub = mfb_stub::GetInstance(window);
stub->m_mouse_btn(window, button, mod, isPressed);
}
void Stub::mouse_move_stub(struct Window *window, int x, int y) {
Stub *stub = Stub::GetInstance(window);
//-------------------------------------
void
mfb_stub::mouse_move_stub(struct mfb_window *window, int x, int y) {
mfb_stub *stub = mfb_stub::GetInstance(window);
stub->m_mouse_move(window, x, y);
}
void Stub::scroll_stub(struct Window *window, KeyMod mod, float deltaX, float deltaY) {
Stub *stub = Stub::GetInstance(window);
//-------------------------------------
void
mfb_stub::scroll_stub(struct mfb_window *window, mfb_key_mod mod, float deltaX, float deltaY) {
mfb_stub *stub = mfb_stub::GetInstance(window);
stub->m_scroll(window, mod, deltaX, deltaY);
}

View File

@ -4,7 +4,8 @@
//#define kUseBilinearInterpolation
#if defined(kUseBilinearInterpolation)
static uint32_t interpolate(uint32_t *srcImage, uint32_t x, uint32_t y, uint32_t srcOffsetX, uint32_t srcOffsetY, uint32_t srcWidth, uint32_t srcHeight, uint32_t srcPitch) {
static uint32_t
interpolate(uint32_t *srcImage, uint32_t x, uint32_t y, uint32_t srcOffsetX, uint32_t srcOffsetY, uint32_t srcWidth, uint32_t srcHeight, uint32_t srcPitch) {
uint32_t incX = x + 1 < srcWidth ? 1 : 0;
uint32_t incY = y + 1 < srcHeight ? srcPitch : 0;
uint8_t *p00 = (uint8_t *) &srcImage[(srcOffsetX >> 16)];
@ -38,8 +39,9 @@ static uint32_t interpolate(uint32_t *srcImage, uint32_t x, uint32_t y, uint32_t
#endif
// Only for 32 bits images
void stretch_image(uint32_t *srcImage, uint32_t srcX, uint32_t srcY, uint32_t srcWidth, uint32_t srcHeight, uint32_t srcPitch,
uint32_t *dstImage, uint32_t dstX, uint32_t dstY, uint32_t dstWidth, uint32_t dstHeight, uint32_t dstPitch) {
void
stretch_image(uint32_t *srcImage, uint32_t srcX, uint32_t srcY, uint32_t srcWidth, uint32_t srcHeight, uint32_t srcPitch,
uint32_t *dstImage, uint32_t dstX, uint32_t dstY, uint32_t dstWidth, uint32_t dstHeight, uint32_t dstPitch) {
uint32_t x, y;
uint32_t srcOffsetX, srcOffsetY;

View File

@ -3,7 +3,7 @@
#include "MiniFB.h"
#include "MiniFB_enums.h"
#define kCall(func, ...) if(window_data && window_data->func) window_data->func((struct Window *) window_data, __VA_ARGS__);
#define kCall(func, ...) if(window_data && window_data->func) window_data->func((struct mfb_window *) window_data, __VA_ARGS__);
#define kUnused(var) (void) var;
#if defined(__cplusplus)
@ -12,7 +12,7 @@ extern "C" {
extern short int g_keycodes[512];
void init_keycodes();
void keyboard_default(struct Window *window, Key key, KeyMod mod, bool isPressed);
void keyboard_default(struct mfb_window *window, mfb_key key, mfb_key_mod mod, bool isPressed);
#if defined(__cplusplus)
}

View File

@ -85,7 +85,8 @@ NSString* g_shadersSrc = @
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#if defined(USE_METAL_API)
static bool create_shaders(SWindowData_OSX *window_data_osx) {
static bool
create_shaders(SWindowData_OSX *window_data_osx) {
// Error
NSError* nsError = 0x0;
NSError** nsErrorPtr = &nsError;
@ -136,14 +137,16 @@ static bool create_shaders(SWindowData_OSX *window_data_osx) {
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
struct Window *mfb_open(const char *title, unsigned width, unsigned height)
struct mfb_window *
mfb_open(const char *title, unsigned width, unsigned height)
{
return mfb_open_ex(title, width, height, 0);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
struct Window *mfb_open_ex(const char *title, unsigned width, unsigned height, unsigned flags)
struct mfb_window *
mfb_open_ex(const char *title, unsigned width, unsigned height, unsigned flags)
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
@ -254,7 +257,7 @@ struct Window *mfb_open_ex(const char *title, unsigned width, unsigned height, u
[NSApp finishLaunching];
#endif
mfb_set_keyboard_callback((struct Window *) window_data, keyboard_default);
mfb_set_keyboard_callback((struct mfb_window *) window_data, keyboard_default);
#if defined(USE_METAL_API)
NSLog(@"Window created using Metal API");
@ -264,12 +267,13 @@ struct Window *mfb_open_ex(const char *title, unsigned width, unsigned height, u
[pool drain];
return (struct Window *) window_data;
return (struct mfb_window *) window_data;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static void destroy_window_data(SWindowData *window_data)
static void
destroy_window_data(SWindowData *window_data)
{
if(window_data == 0x0)
return;
@ -293,7 +297,8 @@ static void destroy_window_data(SWindowData *window_data)
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static void update_events(SWindowData *window_data)
static void
update_events(SWindowData *window_data)
{
NSEvent* event;
@ -312,7 +317,8 @@ static void update_events(SWindowData *window_data)
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
UpdateState mfb_update(struct Window *window, void *buffer)
mfb_update_state
mfb_update(struct mfb_window *window, void *buffer)
{
if(window == 0x0) {
return STATE_INVALID_WINDOW;
@ -345,7 +351,8 @@ UpdateState mfb_update(struct Window *window, void *buffer)
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
UpdateState mfb_update_events(struct Window *window)
mfb_update_state
mfb_update_events(struct mfb_window *window)
{
if(window == 0x0) {
return STATE_INVALID_WINDOW;
@ -368,7 +375,8 @@ UpdateState mfb_update_events(struct Window *window)
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool mfb_set_viewport(struct Window *window, unsigned offset_x, unsigned offset_y, unsigned width, unsigned height)
bool
mfb_set_viewport(struct mfb_window *window, unsigned offset_x, unsigned offset_y, unsigned width, unsigned height)
{
SWindowData *window_data = (SWindowData *) window;
@ -410,7 +418,8 @@ bool mfb_set_viewport(struct Window *window, unsigned offset_x, unsigned offset_
extern short int g_keycodes[512];
void init_keycodes()
void
init_keycodes()
{
// Clear keys
for (unsigned int i = 0; i < sizeof(g_keycodes) / sizeof(g_keycodes[0]); ++i)

View File

@ -136,7 +136,7 @@ keyboard_key(void *data, struct wl_keyboard *keyboard, uint32_t serial, uint32_t
SWindowData *window_data = (SWindowData *) data;
if(key < 512) {
Key key_code = (Key) g_keycodes[key];
mfb_key key_code = (mfb_key) g_keycodes[key];
bool is_pressed = (bool) (state == WL_KEYBOARD_KEY_STATE_PRESSED);
switch (key_code)
{
@ -174,7 +174,7 @@ keyboard_key(void *data, struct wl_keyboard *keyboard, uint32_t serial, uint32_t
}
window_data->key_status[key_code] = is_pressed;
kCall(keyboard_func, key_code, (KeyMod)window_data->mod_keys, is_pressed);
kCall(keyboard_func, key_code, (mfb_key_mod) window_data->mod_keys, is_pressed);
}
}
@ -210,7 +210,8 @@ keyboard_repeat_info(void *data, struct wl_keyboard *keyboard, int32_t rate, int
kUnused(delay);
}
static const struct wl_keyboard_listener keyboard_listener = {
static const struct
wl_keyboard_listener keyboard_listener = {
.keymap = keyboard_keymap,
.enter = keyboard_enter,
.leave = keyboard_leave,
@ -319,7 +320,7 @@ pointer_button(void *data, struct wl_pointer *pointer, uint32_t serial, uint32_t
//printf("Pointer button '%d'(%d)\n", button, state);
SWindowData *window_data = (SWindowData *) data;
kCall(mouse_btn_func, (MouseButton) (button - BTN_MOUSE + 1), (KeyMod) window_data->mod_keys, state == 1);
kCall(mouse_btn_func, (mfb_mouse_button) (button - BTN_MOUSE + 1), (mfb_key_mod) window_data->mod_keys, state == 1);
}
// Scroll and other axis notifications.
@ -352,10 +353,10 @@ pointer_axis(void *data, struct wl_pointer *pointer, uint32_t time, uint32_t axi
//printf("Pointer handle axis: axis: %d (0x%x)\n", axis, value);
SWindowData *window_data = (SWindowData *) data;
if(axis == 0) {
kCall(mouse_wheel_func, (KeyMod) window_data->mod_keys, 0.0f, -(value / 256.0f));
kCall(mouse_wheel_func, (mfb_key_mod) window_data->mod_keys, 0.0f, -(value / 256.0f));
}
else if(axis == 1) {
kCall(mouse_wheel_func, (KeyMod) window_data->mod_keys, -(value / 256.0f), 0.0f);
kCall(mouse_wheel_func, (mfb_key_mod) window_data->mod_keys, -(value / 256.0f), 0.0f);
}
}
@ -388,7 +389,8 @@ axis_discrete(void *data, struct wl_pointer *pointer, uint32_t axis, int32_t dis
kUnused(discrete);
}
static const struct wl_pointer_listener pointer_listener = {
static const struct
wl_pointer_listener pointer_listener = {
.enter = pointer_enter,
.leave = pointer_leave,
.motion = pointer_motion,
@ -439,7 +441,8 @@ seat_name(void *data, struct wl_seat *seat, const char *name) {
printf("Seat '%s'n", name);
}
static const struct wl_seat_listener seat_listener = {
static const struct
wl_seat_listener seat_listener = {
.capabilities = seat_capabilities,
.name = 0x0,
};
@ -475,7 +478,8 @@ shm_format(void *data, struct wl_shm *shm, uint32_t format)
}
}
static const struct wl_shm_listener shm_listener = {
static const struct
wl_shm_listener shm_listener = {
.format = shm_format
};
@ -515,7 +519,8 @@ registry_global(void *data, struct wl_registry *registry, uint32_t id, char cons
}
}
static const struct wl_registry_listener registry_listener = {
static const struct
wl_registry_listener registry_listener = {
.global = registry_global,
.global_remove = 0x0,
};
@ -552,14 +557,14 @@ static const struct wl_shell_surface_listener shell_surface_listener = {
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
struct Window *
struct mfb_window *
mfb_open_ex(const char *title, unsigned width, unsigned height, unsigned flags) {
// TODO: Not yet
kUnused(flags);
return mfb_open(title, width, height);
}
struct Window *
struct mfb_window *
mfb_open(const char *title, unsigned width, unsigned height)
{
int fd = -1;
@ -662,11 +667,11 @@ mfb_open(const char *title, unsigned width, unsigned height)
wl_surface_damage(window_data_way->surface, window_data->dst_offset_x, window_data->dst_offset_y, window_data->dst_width, window_data->dst_height);
wl_surface_commit(window_data_way->surface);
mfb_set_keyboard_callback((struct Window *) window_data, keyboard_default);
mfb_set_keyboard_callback((struct mfb_window *) window_data, keyboard_default);
printf("Window created using Wayland API\n");
return (struct Window *) window_data;
return (struct mfb_window *) window_data;
out:
close(fd);
@ -688,12 +693,13 @@ frame_done(void *data, struct wl_callback *callback, uint32_t cookie)
*(uint32_t *)data = 1;
}
static const struct wl_callback_listener frame_listener = {
static const struct
wl_callback_listener frame_listener = {
.done = frame_done,
};
UpdateState
mfb_update(struct Window *window, void *buffer)
mfb_update_state
mfb_update(struct mfb_window *window, void *buffer)
{
uint32_t done = 0;
@ -739,8 +745,8 @@ mfb_update(struct Window *window, void *buffer)
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
UpdateState
mfb_update_events(struct Window *window)
mfb_update_state
mfb_update_events(struct mfb_window *window)
{
if(window == 0x0) {
return STATE_INVALID_WINDOW;
@ -896,7 +902,7 @@ init_keycodes(void)
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool
mfb_set_viewport(struct Window *window, unsigned offset_x, unsigned offset_y, unsigned width, unsigned height) {
mfb_set_viewport(struct mfb_window *window, unsigned offset_x, unsigned offset_y, unsigned width, unsigned height) {
SWindowData *window_data = (SWindowData *) window;

View File

@ -12,10 +12,11 @@ long s_window_style = WS_POPUP | WS_SYSMENU | WS_CAPTION;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
uint32_t translate_mod();
Key translate_key(unsigned int wParam, unsigned long lParam);
void destroy_window_data(SWindowData *window_data);
mfb_key translate_key(unsigned int wParam, unsigned long lParam);
void destroy_window_data(SWindowData *window_data);
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
LRESULT CALLBACK
WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
LRESULT res = 0;
SWindowData *window_data = (SWindowData *) GetWindowLongPtr(hWnd, GWLP_USERDATA);
@ -69,7 +70,7 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
case WM_SYSKEYUP:
{
if (window_data) {
Key key_code = translate_key((unsigned int)wParam, (unsigned long)lParam);
mfb_key key_code = translate_key((unsigned int)wParam, (unsigned long)lParam);
int is_pressed = !((lParam >> 31) & 1);
window_data->mod_keys = translate_mod();
@ -113,8 +114,8 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
case WM_XBUTTONDBLCLK:
{
if (window_data) {
MouseButton button = MOUSE_BTN_0;
window_data->mod_keys = translate_mod();
mfb_mouse_button button = MOUSE_BTN_0;
window_data->mod_keys = translate_mod();
int is_pressed = 0;
switch(message) {
case WM_LBUTTONDOWN:
@ -219,7 +220,8 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
struct Window *mfb_open_ex(const char *title, unsigned width, unsigned height, unsigned flags) {
struct mfb_window *
mfb_open_ex(const char *title, unsigned width, unsigned height, unsigned flags) {
RECT rect = { 0 };
int x = 0, y = 0;
@ -357,18 +359,20 @@ struct Window *mfb_open_ex(const char *title, unsigned width, unsigned height, u
window_data_win->hdc = GetDC(window_data_win->window);
mfb_set_keyboard_callback((struct Window *) window_data, keyboard_default);
mfb_set_keyboard_callback((struct mfb_window *) window_data, keyboard_default);
return (struct Window *) window_data;
return (struct mfb_window *) window_data;
}
struct Window *mfb_open(const char *title, unsigned width, unsigned height) {
struct mfb_window *
mfb_open(const char *title, unsigned width, unsigned height) {
return mfb_open_ex(title, width, height, 0);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
UpdateState mfb_update(struct Window *window, void *buffer) {
mfb_update_state
mfb_update(struct mfb_window *window, void *buffer) {
MSG msg;
if (window == 0x0) {
@ -401,7 +405,8 @@ UpdateState mfb_update(struct Window *window, void *buffer) {
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
UpdateState mfb_update_events(struct Window *window) {
mfb_update_state
mfb_update_events(struct mfb_window *window) {
MSG msg;
if (window == 0x0) {
@ -428,7 +433,8 @@ UpdateState mfb_update_events(struct Window *window) {
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void destroy_window_data(SWindowData *window_data) {
void
destroy_window_data(SWindowData *window_data) {
if (window_data == 0x0)
return;
@ -452,7 +458,8 @@ void destroy_window_data(SWindowData *window_data) {
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
uint32_t translate_mod() {
uint32_t
translate_mod() {
uint32_t mods = 0;
if (GetKeyState(VK_SHIFT) & 0x8000)
@ -475,7 +482,8 @@ uint32_t translate_mod() {
extern short int g_keycodes[512];
void init_keycodes() {
void
init_keycodes() {
// Clear keys
for (size_t i = 0; i < sizeof(g_keycodes) / sizeof(g_keycodes[0]); ++i)
@ -606,7 +614,8 @@ void init_keycodes() {
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Key translate_key(unsigned int wParam, unsigned long lParam) {
mfb_key
translate_key(unsigned int wParam, unsigned long lParam) {
if (wParam == VK_CONTROL) {
MSG next;
DWORD time;
@ -626,12 +635,13 @@ Key translate_key(unsigned int wParam, unsigned long lParam) {
if (wParam == VK_PROCESSKEY)
return KB_KEY_UNKNOWN;
return (Key) g_keycodes[HIWORD(lParam) & 0x1FF];
return (mfb_key) g_keycodes[HIWORD(lParam) & 0x1FF];
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool mfb_set_viewport(struct Window *window, unsigned offset_x, unsigned offset_y, unsigned width, unsigned height) {
bool
mfb_set_viewport(struct mfb_window *window, unsigned offset_x, unsigned offset_y, unsigned width, unsigned height) {
SWindowData *window_data = (SWindowData *) window;
if (offset_x + width > window_data->window_width) {

View File

@ -1,20 +1,20 @@
#pragma once
#include <MiniFB_enums.h>
#include <stdint.h>
#include <X11/Xlib.h>
typedef struct {
Window window;
Display *display;
int screen;
GC gc;
XImage *image;
void *image_buffer;
XImage *image_scaler;
uint32_t image_scaler_width;
uint32_t image_scaler_height;
} SWindowData_X11;
#pragma once
#include <MiniFB_enums.h>
#include <stdint.h>
#include <X11/Xlib.h>
typedef struct {
Window window;
Display *display;
int screen;
GC gc;
XImage *image;
void *image_buffer;
XImage *image_scaler;
uint32_t image_scaler_width;
uint32_t image_scaler_height;
} SWindowData_X11;

View File

@ -20,7 +20,7 @@ stretch_image(uint32_t *srcImage, uint32_t srcX, uint32_t srcY, uint32_t srcWidt
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
struct Window *
struct mfb_window *
mfb_open_ex(const char *title, unsigned width, unsigned height, unsigned flags) {
int depth, i, formatCount, convDepth = -1;
XPixmapFormatValues* formats;
@ -185,14 +185,14 @@ mfb_open_ex(const char *title, unsigned width, unsigned height, unsigned flags)
window_data_x11->image = XCreateImage(window_data_x11->display, CopyFromParent, depth, ZPixmap, 0, 0x0, width, height, 32, width * 4);
mfb_set_keyboard_callback((struct Window *) window_data, keyboard_default);
mfb_set_keyboard_callback((struct mfb_window *) window_data, keyboard_default);
printf("Window created using X11 API\n");
return (struct Window *) window_data;
return (struct mfb_window *) window_data;
}
struct Window *
struct mfb_window *
mfb_open(const char *title, unsigned width, unsigned height) {
return mfb_open_ex(title, width, height, 0);
}
@ -203,7 +203,8 @@ int translate_key(int scancode);
int translate_mod(int state);
int translate_mod_ex(int key, int state, int is_pressed);
static void processEvents(SWindowData *window_data) {
static void
processEvents(SWindowData *window_data) {
XEvent event;
SWindowData_X11 *window_data_x11 = (SWindowData_X11 *) window_data->specific;
@ -214,44 +215,44 @@ static void processEvents(SWindowData *window_data) {
case KeyPress:
case KeyRelease:
{
Key key_code = (Key) translate_key(event.xkey.keycode);
int is_pressed = (event.type == KeyPress);
mfb_key key_code = (mfb_key) translate_key(event.xkey.keycode);
int is_pressed = (event.type == KeyPress);
window_data->mod_keys = translate_mod_ex(key_code, event.xkey.state, is_pressed);
window_data->key_status[key_code] = is_pressed;
kCall(keyboard_func, key_code, (KeyMod) window_data->mod_keys, is_pressed);
kCall(keyboard_func, key_code, (mfb_key_mod) window_data->mod_keys, is_pressed);
}
break;
case ButtonPress:
case ButtonRelease:
{
MouseButton button = (MouseButton) event.xbutton.button;
mfb_mouse_button button = (mfb_mouse_button) event.xbutton.button;
int is_pressed = (event.type == ButtonPress);
window_data->mod_keys = translate_mod(event.xkey.state);
window_data->mod_keys = translate_mod(event.xkey.state);
switch (button) {
case Button1:
case Button2:
case Button3:
kCall(mouse_btn_func, button, (KeyMod) window_data->mod_keys, is_pressed);
kCall(mouse_btn_func, button, (mfb_key_mod) window_data->mod_keys, is_pressed);
break;
case Button4:
kCall(mouse_wheel_func, (KeyMod) window_data->mod_keys, 0.0f, 1.0f);
kCall(mouse_wheel_func, (mfb_key_mod) window_data->mod_keys, 0.0f, 1.0f);
break;
case Button5:
kCall(mouse_wheel_func, (KeyMod) window_data->mod_keys, 0.0f, -1.0f);
kCall(mouse_wheel_func, (mfb_key_mod) window_data->mod_keys, 0.0f, -1.0f);
break;
case 6:
kCall(mouse_wheel_func, (KeyMod) window_data->mod_keys, 1.0f, 0.0f);
kCall(mouse_wheel_func, (mfb_key_mod) window_data->mod_keys, 1.0f, 0.0f);
break;
case 7:
kCall(mouse_wheel_func, (KeyMod) window_data->mod_keys, -1.0f, 0.0f);
kCall(mouse_wheel_func, (mfb_key_mod) window_data->mod_keys, -1.0f, 0.0f);
break;
default:
kCall(mouse_btn_func, (MouseButton) (button - 4), (KeyMod) window_data->mod_keys, is_pressed);
kCall(mouse_btn_func, (mfb_mouse_button) (button - 4), (mfb_key_mod) window_data->mod_keys, is_pressed);
break;
}
}
@ -303,7 +304,8 @@ static void processEvents(SWindowData *window_data) {
void destroy(SWindowData *window_data);
UpdateState mfb_update(struct Window *window, void *buffer) {
mfb_update_state
mfb_update(struct mfb_window *window, void *buffer) {
if (window == 0x0) {
return STATE_INVALID_WINDOW;
}
@ -358,7 +360,8 @@ UpdateState mfb_update(struct Window *window, void *buffer) {
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
UpdateState mfb_update_events(struct Window *window) {
mfb_update_state
mfb_update_events(struct mfb_window *window) {
if (window == 0x0) {
return STATE_INVALID_WINDOW;
}
@ -378,7 +381,8 @@ UpdateState mfb_update_events(struct Window *window) {
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void destroy(SWindowData *window_data) {
void
destroy(SWindowData *window_data) {
if (window_data != 0x0) {
if (window_data->specific != 0x0) {
SWindowData_X11 *window_data_x11 = (SWindowData_X11 *) window_data->specific;
@ -400,7 +404,8 @@ void destroy(SWindowData *window_data) {
extern short int g_keycodes[512];
static int translateKeyCodeB(int keySym) {
static int
translateKeyCodeB(int keySym) {
switch (keySym)
{
@ -563,7 +568,8 @@ static int translateKeyCodeA(int keySym) {
return KB_KEY_UNKNOWN;
}
void init_keycodes(SWindowData_X11 *window_data_x11) {
void
init_keycodes(SWindowData_X11 *window_data_x11) {
size_t i;
int keySym;
@ -585,7 +591,8 @@ void init_keycodes(SWindowData_X11 *window_data_x11) {
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int translate_key(int scancode) {
int
translate_key(int scancode) {
if (scancode < 0 || scancode > 255)
return KB_KEY_UNKNOWN;
@ -594,7 +601,8 @@ int translate_key(int scancode) {
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int translate_mod(int state) {
int
translate_mod(int state) {
int mod_keys = 0;
if (state & ShiftMask)
@ -615,7 +623,8 @@ int translate_mod(int state) {
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int translate_mod_ex(int key, int state, int is_pressed) {
int
translate_mod_ex(int key, int state, int is_pressed) {
int mod_keys = 0;
mod_keys = translate_mod(state);
@ -660,7 +669,8 @@ int translate_mod_ex(int key, int state, int is_pressed) {
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool mfb_set_viewport(struct Window *window, unsigned offset_x, unsigned offset_y, unsigned width, unsigned height) {
bool
mfb_set_viewport(struct mfb_window *window, unsigned offset_x, unsigned offset_y, unsigned width, unsigned height) {
SWindowData *window_data = (SWindowData *) window;
if (offset_x + width > window_data->window_width) {

View File

@ -1,141 +1,149 @@
#include <MiniFB.h>
#include <stdio.h>
#include <stdint.h>
#define kUnused(var) (void) var
#define WIDTH 800
#define HEIGHT 600
static unsigned int g_buffer[WIDTH * HEIGHT];
static bool g_active = true;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void active(struct Window *window, bool isActive) {
const char *window_title = "";
if(window) {
window_title = (const char *) mfb_get_user_data(window);
}
fprintf(stdout, "%s > active: %d\n", window_title, isActive);
g_active = isActive;
}
void resize(struct Window *window, int width, int height) {
uint32_t x = 0;
uint32_t y = 0;
const char *window_title = "";
if(window) {
window_title = (const char *) mfb_get_user_data(window);
}
fprintf(stdout, "%s > resize: %d, %d\n", window_title, width, height);
if(width > WIDTH) {
x = (width - WIDTH) >> 1;
width = WIDTH;
}
if(height > HEIGHT) {
y = (height - HEIGHT) >> 1;
height = HEIGHT;
}
mfb_set_viewport(window, x, y, width, height);
}
void keyboard(struct Window *window, Key key, KeyMod mod, bool isPressed) {
const char *window_title = "";
if(window) {
window_title = (const char *) mfb_get_user_data(window);
}
fprintf(stdout, "%s > keyboard: key: %s (pressed: %d) [KeyMod: %x]\n", window_title, mfb_get_key_name(key), isPressed, mod);
if(key == KB_KEY_ESCAPE) {
mfb_close(window);
}
}
void char_input(struct Window *window, unsigned int charCode) {
const char *window_title = "";
if(window) {
window_title = (const char *) mfb_get_user_data(window);
}
fprintf(stdout, "%s > charCode: %d\n", window_title, charCode);
}
void mouse_btn(struct Window *window, MouseButton button, KeyMod mod, bool isPressed) {
const char *window_title = "";
if(window) {
window_title = (const char *) mfb_get_user_data(window);
}
fprintf(stdout, "%s > mouse_btn: button: %d (pressed: %d) [KeyMod: %x]\n", window_title, button, isPressed, mod);
}
void mouse_move(struct Window *window, int x, int y) {
kUnused(window);
kUnused(x);
kUnused(y);
// const char *window_title = "";
// if(window) {
// window_t(const char *) itle = mfb_get_user_data(window);
// }
//fprintf(stdout, "%s > mouse_move: %d, %d\n", window_title, x, y);
}
void mouse_scroll(struct Window *window, KeyMod mod, float deltaX, float deltaY) {
const char *window_title = "";
if(window) {
window_title = (const char *) mfb_get_user_data(window);
}
fprintf(stdout, "%s > mouse_scroll: x: %f, y: %f [KeyMod: %x]\n", window_title, deltaX, deltaY, mod);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int main()
{
int noise, carry, seed = 0xbeef;
struct Window *window = mfb_open_ex("Input Events Test", WIDTH, HEIGHT, WF_RESIZABLE);
if (!window)
return 0;
mfb_set_active_callback(window, active);
mfb_set_resize_callback(window, resize);
mfb_set_keyboard_callback(window, keyboard);
mfb_set_char_input_callback(window, char_input);
mfb_set_mouse_button_callback(window, mouse_btn);
mfb_set_mouse_move_callback(window, mouse_move);
mfb_set_mouse_scroll_callback(window, mouse_scroll);
mfb_set_user_data(window, (void *) "Input Events Test");
for (;;)
{
int i;
UpdateState state;
if(g_active)
{
for (i = 0; i < WIDTH * HEIGHT; ++i)
{
noise = seed;
noise >>= 3;
noise ^= seed;
carry = noise & 1;
noise >>= 1;
seed >>= 1;
seed |= (carry << 30);
noise &= 0xFF;
g_buffer[i] = MFB_RGB(noise, noise, noise);
}
state = mfb_update(window, g_buffer);
}
else {
state = mfb_update_events(window);
}
if (state != STATE_OK) {
window = 0x0;
break;
}
}
return 0;
}
#include <MiniFB.h>
#include <stdio.h>
#include <stdint.h>
#define kUnused(var) (void) var
#define WIDTH 800
#define HEIGHT 600
static unsigned int g_buffer[WIDTH * HEIGHT];
static bool g_active = true;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void
active(struct mfb_window *window, bool isActive) {
const char *window_title = "";
if(window) {
window_title = (const char *) mfb_get_user_data(window);
}
fprintf(stdout, "%s > active: %d\n", window_title, isActive);
g_active = isActive;
}
void
resize(struct mfb_window *window, int width, int height) {
uint32_t x = 0;
uint32_t y = 0;
const char *window_title = "";
if(window) {
window_title = (const char *) mfb_get_user_data(window);
}
fprintf(stdout, "%s > resize: %d, %d\n", window_title, width, height);
if(width > WIDTH) {
x = (width - WIDTH) >> 1;
width = WIDTH;
}
if(height > HEIGHT) {
y = (height - HEIGHT) >> 1;
height = HEIGHT;
}
mfb_set_viewport(window, x, y, width, height);
}
void
keyboard(struct mfb_window *window, mfb_key key, mfb_key_mod mod, bool isPressed) {
const char *window_title = "";
if(window) {
window_title = (const char *) mfb_get_user_data(window);
}
fprintf(stdout, "%s > keyboard: key: %s (pressed: %d) [key_mod: %x]\n", window_title, mfb_get_key_name(key), isPressed, mod);
if(key == KB_KEY_ESCAPE) {
mfb_close(window);
}
}
void
char_input(struct mfb_window *window, unsigned int charCode) {
const char *window_title = "";
if(window) {
window_title = (const char *) mfb_get_user_data(window);
}
fprintf(stdout, "%s > charCode: %d\n", window_title, charCode);
}
void
mouse_btn(struct mfb_window *window, mfb_mouse_button button, mfb_key_mod mod, bool isPressed) {
const char *window_title = "";
if(window) {
window_title = (const char *) mfb_get_user_data(window);
}
fprintf(stdout, "%s > mouse_btn: button: %d (pressed: %d) [key_mod: %x]\n", window_title, button, isPressed, mod);
}
void
mouse_move(struct mfb_window *window, int x, int y) {
kUnused(window);
kUnused(x);
kUnused(y);
// const char *window_title = "";
// if(window) {
// window_t(const char *) itle = mfb_get_user_data(window);
// }
//fprintf(stdout, "%s > mouse_move: %d, %d\n", window_title, x, y);
}
void
mouse_scroll(struct mfb_window *window, mfb_key_mod mod, float deltaX, float deltaY) {
const char *window_title = "";
if(window) {
window_title = (const char *) mfb_get_user_data(window);
}
fprintf(stdout, "%s > mouse_scroll: x: %f, y: %f [key_mod: %x]\n", window_title, deltaX, deltaY, mod);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int
main()
{
int noise, carry, seed = 0xbeef;
struct mfb_window *window = mfb_open_ex("Input Events Test", WIDTH, HEIGHT, WF_RESIZABLE);
if (!window)
return 0;
mfb_set_active_callback(window, active);
mfb_set_resize_callback(window, resize);
mfb_set_keyboard_callback(window, keyboard);
mfb_set_char_input_callback(window, char_input);
mfb_set_mouse_button_callback(window, mouse_btn);
mfb_set_mouse_move_callback(window, mouse_move);
mfb_set_mouse_scroll_callback(window, mouse_scroll);
mfb_set_user_data(window, (void *) "Input Events Test");
for (;;)
{
int i;
mfb_update_state state;
if(g_active)
{
for (i = 0; i < WIDTH * HEIGHT; ++i)
{
noise = seed;
noise >>= 3;
noise ^= seed;
carry = noise & 1;
noise >>= 1;
seed >>= 1;
seed |= (carry << 30);
noise &= 0xFF;
g_buffer[i] = MFB_RGB(noise, noise, noise);
}
state = mfb_update(window, g_buffer);
}
else {
state = mfb_update_events(window);
}
if (state != STATE_OK) {
window = 0x0;
break;
}
}
return 0;
}

View File

@ -1,138 +1,139 @@
#include <MiniFB.h>
#include <stdio.h>
#include <stdint.h>
#define kUnused(var) (void) var;
#define WIDTH 800
#define HEIGHT 600
static unsigned int g_buffer[WIDTH * HEIGHT];
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class Events {
public:
void active(struct Window *window, bool isActive) {
const char *window_title = "";
if(window) {
window_title = (const char *) mfb_get_user_data(window);
}
fprintf(stdout, "%s > active: %d\n", window_title, isActive);
}
void resize(struct Window *window, int width, int height) {
uint32_t x = 0;
uint32_t y = 0;
const char *window_title = "";
if(window) {
window_title = (const char *) mfb_get_user_data(window);
}
fprintf(stdout, "%s > resize: %d, %d\n", window_title, width, height);
if(width > WIDTH) {
x = (width - WIDTH) >> 1;
width = WIDTH;
}
if(height > HEIGHT) {
y = (height - HEIGHT) >> 1;
height = HEIGHT;
}
mfb_set_viewport(window, x, y, width, height);
}
void keyboard(struct Window *window, Key key, KeyMod mod, bool isPressed) {
const char *window_title = "";
if(window) {
window_title = (const char *) mfb_get_user_data(window);
}
fprintf(stdout, "%s > keyboard: key: %s (pressed: %d) [KeyMod: %x]\n", window_title, mfb_get_key_name(key), isPressed, mod);
if(key == KB_KEY_ESCAPE) {
mfb_close(window);
}
}
void char_input(struct Window *window, unsigned int charCode) {
const char *window_title = "";
if(window) {
window_title = (const char *) mfb_get_user_data(window);
}
fprintf(stdout, "%s > charCode: %d\n", window_title, charCode);
}
void mouse_btn(struct Window *window, MouseButton button, KeyMod mod, bool isPressed) {
const char *window_title = "";
if(window) {
window_title = (const char *) mfb_get_user_data(window);
}
fprintf(stdout, "%s > mouse_btn: button: %d (pressed: %d) [KeyMod: %x]\n", window_title, button, isPressed, mod);
}
void mouse_move(struct Window *window, int x, int y) {
kUnused(window);
kUnused(x);
kUnused(y);
// const char *window_title = "";
// if(window) {
// window_t(const char *) itle = mfb_get_user_data(window);
// }
//fprintf(stdout, "%s > mouse_move: %d, %d\n", window_title, x, y);
}
void mouse_scroll(struct Window *window, KeyMod mod, float deltaX, float deltaY) {
const char *window_title = "";
if(window) {
window_title = (const char *) mfb_get_user_data(window);
}
fprintf(stdout, "%s > mouse_scroll: x: %f, y: %f [KeyMod: %x]\n", window_title, deltaX, deltaY, mod);
}
};
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int main()
{
int noise, carry, seed = 0xbeef;
struct Window *window = mfb_open_ex("Input Events CPP Test", WIDTH, HEIGHT, WF_RESIZABLE);
if (!window)
return 0;
Events e;
mfb_set_active_callback(window, &e, &Events::active);
mfb_set_resize_callback(window, &e, &Events::resize);
mfb_set_keyboard_callback(window, &e, &Events::keyboard);
mfb_set_char_input_callback(window, &e, &Events::char_input);
mfb_set_mouse_button_callback(window, &e, &Events::mouse_btn);
mfb_set_mouse_move_callback(window, &e, &Events::mouse_move);
mfb_set_mouse_scroll_callback(window, &e, &Events::mouse_scroll);
mfb_set_user_data(window, (void *) "Input Events CPP Test");
for (;;)
{
int i;
UpdateState state;
for (i = 0; i < WIDTH * HEIGHT; ++i)
{
noise = seed;
noise >>= 3;
noise ^= seed;
carry = noise & 1;
noise >>= 1;
seed >>= 1;
seed |= (carry << 30);
noise &= 0xFF;
g_buffer[i] = MFB_RGB(noise, noise, noise);
}
state = mfb_update(window, g_buffer);
if (state != STATE_OK) {
window = 0x0;
break;
}
}
return 0;
}
#include <MiniFB.h>
#include <stdio.h>
#include <stdint.h>
#define kUnused(var) (void) var;
#define WIDTH 800
#define HEIGHT 600
static unsigned int g_buffer[WIDTH * HEIGHT];
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class Events {
public:
void active(struct mfb_window *window, bool isActive) {
const char *window_title = "";
if(window) {
window_title = (const char *) mfb_get_user_data(window);
}
fprintf(stdout, "%s > active: %d\n", window_title, isActive);
}
void resize(struct mfb_window *window, int width, int height) {
uint32_t x = 0;
uint32_t y = 0;
const char *window_title = "";
if(window) {
window_title = (const char *) mfb_get_user_data(window);
}
fprintf(stdout, "%s > resize: %d, %d\n", window_title, width, height);
if(width > WIDTH) {
x = (width - WIDTH) >> 1;
width = WIDTH;
}
if(height > HEIGHT) {
y = (height - HEIGHT) >> 1;
height = HEIGHT;
}
mfb_set_viewport(window, x, y, width, height);
}
void keyboard(struct mfb_window *window, mfb_key key, mfb_key_mod mod, bool isPressed) {
const char *window_title = "";
if(window) {
window_title = (const char *) mfb_get_user_data(window);
}
fprintf(stdout, "%s > keyboard: key: %s (pressed: %d) [key_mod: %x]\n", window_title, mfb_get_key_name(key), isPressed, mod);
if(key == KB_KEY_ESCAPE) {
mfb_close(window);
}
}
void char_input(struct mfb_window *window, unsigned int charCode) {
const char *window_title = "";
if(window) {
window_title = (const char *) mfb_get_user_data(window);
}
fprintf(stdout, "%s > charCode: %d\n", window_title, charCode);
}
void mouse_btn(struct mfb_window *window, mfb_mouse_button button, mfb_key_mod mod, bool isPressed) {
const char *window_title = "";
if(window) {
window_title = (const char *) mfb_get_user_data(window);
}
fprintf(stdout, "%s > mouse_btn: button: %d (pressed: %d) [key_mod: %x]\n", window_title, button, isPressed, mod);
}
void mouse_move(struct mfb_window *window, int x, int y) {
kUnused(window);
kUnused(x);
kUnused(y);
// const char *window_title = "";
// if(window) {
// window_t(const char *) itle = mfb_get_user_data(window);
// }
//fprintf(stdout, "%s > mouse_move: %d, %d\n", window_title, x, y);
}
void mouse_scroll(struct mfb_window *window, mfb_key_mod mod, float deltaX, float deltaY) {
const char *window_title = "";
if(window) {
window_title = (const char *) mfb_get_user_data(window);
}
fprintf(stdout, "%s > mouse_scroll: x: %f, y: %f [key_mod: %x]\n", window_title, deltaX, deltaY, mod);
}
};
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int
main()
{
int noise, carry, seed = 0xbeef;
struct mfb_window *window = mfb_open_ex("Input Events CPP Test", WIDTH, HEIGHT, WF_RESIZABLE);
if (!window)
return 0;
Events e;
mfb_set_active_callback(window, &e, &Events::active);
mfb_set_resize_callback(window, &e, &Events::resize);
mfb_set_keyboard_callback(window, &e, &Events::keyboard);
mfb_set_char_input_callback(window, &e, &Events::char_input);
mfb_set_mouse_button_callback(window, &e, &Events::mouse_btn);
mfb_set_mouse_move_callback(window, &e, &Events::mouse_move);
mfb_set_mouse_scroll_callback(window, &e, &Events::mouse_scroll);
mfb_set_user_data(window, (void *) "Input Events CPP Test");
for (;;)
{
int i;
mfb_update_state state;
for (i = 0; i < WIDTH * HEIGHT; ++i)
{
noise = seed;
noise >>= 3;
noise ^= seed;
carry = noise & 1;
noise >>= 1;
seed >>= 1;
seed |= (carry << 30);
noise &= 0xFF;
g_buffer[i] = MFB_RGB(noise, noise, noise);
}
state = mfb_update(window, g_buffer);
if (state != STATE_OK) {
window = 0x0;
break;
}
}
return 0;
}

View File

@ -1,188 +1,197 @@
#include <MiniFB.h>
#include <stdio.h>
#include <stdint.h>
#define _USE_MATH_DEFINES
#include <math.h>
#define kPI 3.14159265358979323846f
#define kUnused(var) (void) var;
#define WIDTH_A 800
#define HEIGHT_A 600
static unsigned int g_buffer_a[WIDTH_A * HEIGHT_A];
#define WIDTH_B 320
#define HEIGHT_B 240
static unsigned int g_buffer_b[WIDTH_B * HEIGHT_B];
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void active(struct Window *window, bool isActive) {
const char *window_title = "";
if(window) {
window_title = (const char *) mfb_get_user_data(window);
}
fprintf(stdout, "%s > active: %d\n", window_title, isActive);
}
void resize(struct Window *window, int width, int height) {
const char *window_title = "";
if(window) {
window_title = (const char *) mfb_get_user_data(window);
}
fprintf(stdout, "%s > resize: %d, %d\n", window_title, width, height);
}
void keyboard(struct Window *window, Key key, KeyMod mod, bool isPressed) {
const char *window_title = "";
if(window) {
window_title = (const char *) mfb_get_user_data(window);
}
fprintf(stdout, "%s > keyboard: key: %s (pressed: %d) [KeyMod: %x]\n", window_title, mfb_get_key_name(key), isPressed, mod);
if(key == KB_KEY_ESCAPE) {
mfb_close(window);
}
}
void char_input(struct Window *window, unsigned int charCode) {
const char *window_title = "";
if(window) {
window_title = (const char *) mfb_get_user_data(window);
}
fprintf(stdout, "%s > charCode: %d\n", window_title, charCode);
}
void mouse_btn(struct Window *window, MouseButton button, KeyMod mod, bool isPressed) {
const char *window_title = "";
if(window) {
window_title = (const char *) mfb_get_user_data(window);
}
fprintf(stdout, "%s > mouse_btn: button: %d (pressed: %d) [KeyMod: %x]\n", window_title, button, isPressed, mod);
}
void mouse_move(struct Window *window, int x, int y) {
kUnused(window);
kUnused(x);
kUnused(y);
// const char *window_title = "";
// if(window) {
// window_t(const char *) itle = mfb_get_user_data(window);
// }
//fprintf(stdout, "%s > mouse_move: %d, %d\n", window_title, x, y);
}
void mouse_scroll(struct Window *window, KeyMod mod, float deltaX, float deltaY) {
const char *window_title = "";
if(window) {
window_title = (const char *) mfb_get_user_data(window);
}
fprintf(stdout, "%s > mouse_scroll: x: %f, y: %f [KeyMod: %x]\n", window_title, deltaX, deltaY, mod);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int main()
{
int noise, carry, seed = 0xbeef;
struct Window *window_a = mfb_open_ex("Multiple Windows Test", WIDTH_A, HEIGHT_A, WF_RESIZABLE);
if (!window_a)
return 0;
mfb_set_active_callback(window_a, active);
mfb_set_resize_callback(window_a, resize);
mfb_set_keyboard_callback(window_a, keyboard);
mfb_set_char_input_callback(window_a, char_input);
mfb_set_mouse_button_callback(window_a, mouse_btn);
mfb_set_mouse_move_callback(window_a, mouse_move);
mfb_set_mouse_scroll_callback(window_a, mouse_scroll);
mfb_set_user_data(window_a, (void *) "Window A");
//--
struct Window *window_b = mfb_open_ex("Secondary Window", WIDTH_B, HEIGHT_B, WF_RESIZABLE);
if (!window_b)
return 0;
mfb_set_active_callback(window_b, active);
mfb_set_resize_callback(window_b, resize);
mfb_set_keyboard_callback(window_b, keyboard);
mfb_set_char_input_callback(window_b, char_input);
mfb_set_mouse_button_callback(window_b, mouse_btn);
mfb_set_mouse_move_callback(window_b, mouse_move);
mfb_set_mouse_scroll_callback(window_b, mouse_scroll);
mfb_set_user_data(window_b, (void *) "Window B");
// Generate pallete for plasma effect
uint32_t pallete[512];
float inc = 90.0f / 64.0f;
for(uint32_t c=0; c<64; ++c) {
int32_t col = (int32_t) ((255.0f * sinf(c * inc * kPI / 180.0f)) + 0.5f);
pallete[64*0 + c] = MFB_RGB(col, 0, 0);
pallete[64*1 + c] = MFB_RGB(255, col, 0);
pallete[64*2 + c] = MFB_RGB(255-col, 255, 0);
pallete[64*3 + c] = MFB_RGB(0, 255, col);
pallete[64*4 + c] = MFB_RGB(0, 255-col, 255);
pallete[64*5 + c] = MFB_RGB(col, 0, 255);
pallete[64*6 + c] = MFB_RGB(255, 0, 255-col);
pallete[64*7 + c] = MFB_RGB(255-col, 0, 0);
}
//--
float time = 0;
for (;;)
{
int i, x, y;
float dx, dy, time_x, time_y;
int index;
UpdateState state_a, state_b;
for (i = 0; i < WIDTH_A * HEIGHT_A; ++i)
{
noise = seed;
noise >>= 3;
noise ^= seed;
carry = noise & 1;
noise >>= 1;
seed >>= 1;
seed |= (carry << 30);
noise &= 0xFF;
g_buffer_a[i] = MFB_RGB(noise, noise, noise);
}
//--
time_x = sinf(time * kPI / 180.0f);
time_y = cosf(time * kPI / 180.0f);
i = 0;
for(y=0; y<HEIGHT_B; ++y) {
dy = cosf((y * time_y) * kPI / 180.0f); // [-1, 1]
for(x=0; x<WIDTH_B; ++x) {
dx = sinf((x * time_x) * kPI / 180.0f); // [-1, 1]
index = (int) ((2.0f + dx + dy) * 0.25f * 511.0f); // [0, 511]
g_buffer_b[i++] = pallete[index];
}
}
time += 0.1f;
//--
state_a = mfb_update(window_a, g_buffer_a);
if (state_a != STATE_OK) {
window_a = 0x0;
}
//--
state_b = mfb_update(window_b, g_buffer_b);
if (state_b != STATE_OK) {
window_b = 0x0;
}
if(window_a == 0x0 && window_b == 0x0) {
break;
}
}
return 0;
}
#include <MiniFB.h>
#include <stdio.h>
#include <stdint.h>
#define _USE_MATH_DEFINES
#include <math.h>
#define kPI 3.14159265358979323846f
#define kUnused(var) (void) var;
#define WIDTH_A 800
#define HEIGHT_A 600
static unsigned int g_buffer_a[WIDTH_A * HEIGHT_A];
#define WIDTH_B 320
#define HEIGHT_B 240
static unsigned int g_buffer_b[WIDTH_B * HEIGHT_B];
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void
active(struct mfb_window *window, bool isActive) {
const char *window_title = "";
if(window) {
window_title = (const char *) mfb_get_user_data(window);
}
fprintf(stdout, "%s > active: %d\n", window_title, isActive);
}
void
resize(struct mfb_window *window, int width, int height) {
const char *window_title = "";
if(window) {
window_title = (const char *) mfb_get_user_data(window);
}
fprintf(stdout, "%s > resize: %d, %d\n", window_title, width, height);
}
void
keyboard(struct mfb_window *window, mfb_key key, mfb_key_mod mod, bool isPressed) {
const char *window_title = "";
if(window) {
window_title = (const char *) mfb_get_user_data(window);
}
fprintf(stdout, "%s > keyboard: key: %s (pressed: %d) [key_mod: %x]\n", window_title, mfb_get_key_name(key), isPressed, mod);
if(key == KB_KEY_ESCAPE) {
mfb_close(window);
}
}
void
char_input(struct mfb_window *window, unsigned int charCode) {
const char *window_title = "";
if(window) {
window_title = (const char *) mfb_get_user_data(window);
}
fprintf(stdout, "%s > charCode: %d\n", window_title, charCode);
}
void
mouse_btn(struct mfb_window *window, mfb_mouse_button button, mfb_key_mod mod, bool isPressed) {
const char *window_title = "";
if(window) {
window_title = (const char *) mfb_get_user_data(window);
}
fprintf(stdout, "%s > mouse_btn: button: %d (pressed: %d) [key_mod: %x]\n", window_title, button, isPressed, mod);
}
void
mouse_move(struct mfb_window *window, int x, int y) {
kUnused(window);
kUnused(x);
kUnused(y);
// const char *window_title = "";
// if(window) {
// window_t(const char *) itle = mfb_get_user_data(window);
// }
//fprintf(stdout, "%s > mouse_move: %d, %d\n", window_title, x, y);
}
void
mouse_scroll(struct mfb_window *window, mfb_key_mod mod, float deltaX, float deltaY) {
const char *window_title = "";
if(window) {
window_title = (const char *) mfb_get_user_data(window);
}
fprintf(stdout, "%s > mouse_scroll: x: %f, y: %f [key_mod: %x]\n", window_title, deltaX, deltaY, mod);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int
main()
{
int noise, carry, seed = 0xbeef;
struct mfb_window *window_a = mfb_open_ex("Multiple Windows Test", WIDTH_A, HEIGHT_A, WF_RESIZABLE);
if (!window_a)
return 0;
mfb_set_active_callback(window_a, active);
mfb_set_resize_callback(window_a, resize);
mfb_set_keyboard_callback(window_a, keyboard);
mfb_set_char_input_callback(window_a, char_input);
mfb_set_mouse_button_callback(window_a, mouse_btn);
mfb_set_mouse_move_callback(window_a, mouse_move);
mfb_set_mouse_scroll_callback(window_a, mouse_scroll);
mfb_set_user_data(window_a, (void *) "Window A");
//--
struct mfb_window *window_b = mfb_open_ex("Secondary Window", WIDTH_B, HEIGHT_B, WF_RESIZABLE);
if (!window_b)
return 0;
mfb_set_active_callback(window_b, active);
mfb_set_resize_callback(window_b, resize);
mfb_set_keyboard_callback(window_b, keyboard);
mfb_set_char_input_callback(window_b, char_input);
mfb_set_mouse_button_callback(window_b, mouse_btn);
mfb_set_mouse_move_callback(window_b, mouse_move);
mfb_set_mouse_scroll_callback(window_b, mouse_scroll);
mfb_set_user_data(window_b, (void *) "Window B");
// Generate pallete for plasma effect
uint32_t pallete[512];
float inc = 90.0f / 64.0f;
for(uint32_t c=0; c<64; ++c) {
int32_t col = (int32_t) ((255.0f * sinf(c * inc * kPI / 180.0f)) + 0.5f);
pallete[64*0 + c] = MFB_RGB(col, 0, 0);
pallete[64*1 + c] = MFB_RGB(255, col, 0);
pallete[64*2 + c] = MFB_RGB(255-col, 255, 0);
pallete[64*3 + c] = MFB_RGB(0, 255, col);
pallete[64*4 + c] = MFB_RGB(0, 255-col, 255);
pallete[64*5 + c] = MFB_RGB(col, 0, 255);
pallete[64*6 + c] = MFB_RGB(255, 0, 255-col);
pallete[64*7 + c] = MFB_RGB(255-col, 0, 0);
}
//--
float time = 0;
for (;;)
{
int i, x, y;
float dx, dy, time_x, time_y;
int index;
mfb_update_state state_a, state_b;
for (i = 0; i < WIDTH_A * HEIGHT_A; ++i)
{
noise = seed;
noise >>= 3;
noise ^= seed;
carry = noise & 1;
noise >>= 1;
seed >>= 1;
seed |= (carry << 30);
noise &= 0xFF;
g_buffer_a[i] = MFB_RGB(noise, noise, noise);
}
//--
time_x = sinf(time * kPI / 180.0f);
time_y = cosf(time * kPI / 180.0f);
i = 0;
for(y=0; y<HEIGHT_B; ++y) {
dy = cosf((y * time_y) * kPI / 180.0f); // [-1, 1]
for(x=0; x<WIDTH_B; ++x) {
dx = sinf((x * time_x) * kPI / 180.0f); // [-1, 1]
index = (int) ((2.0f + dx + dy) * 0.25f * 511.0f); // [0, 511]
g_buffer_b[i++] = pallete[index];
}
}
time += 0.1f;
//--
state_a = mfb_update(window_a, g_buffer_a);
if (state_a != STATE_OK) {
window_a = 0x0;
}
//--
state_b = mfb_update(window_b, g_buffer_b);
if (state_b != STATE_OK) {
window_b = 0x0;
}
if(window_a == 0x0 && window_b == 0x0) {
break;
}
}
return 0;
}

View File

@ -1,45 +1,46 @@
#include <MiniFB.h>
#include <stdio.h>
#include <stdint.h>
#define WIDTH 800
#define HEIGHT 600
static unsigned int g_buffer[WIDTH * HEIGHT];
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int main()
{
int noise, carry, seed = 0xbeef;
struct Window *window = mfb_open_ex("Noise Test", WIDTH, HEIGHT, WF_RESIZABLE);
if (!window)
return 0;
for (;;)
{
int i;
UpdateState state;
for (i = 0; i < WIDTH * HEIGHT; ++i)
{
noise = seed;
noise >>= 3;
noise ^= seed;
carry = noise & 1;
noise >>= 1;
seed >>= 1;
seed |= (carry << 30);
noise &= 0xFF;
g_buffer[i] = MFB_RGB(noise, noise, noise);
}
state = mfb_update(window, g_buffer);
if (state != STATE_OK) {
window = 0x0;
break;
}
}
return 0;
}
#include <MiniFB.h>
#include <stdio.h>
#include <stdint.h>
#define WIDTH 800
#define HEIGHT 600
static unsigned int g_buffer[WIDTH * HEIGHT];
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int
main()
{
int noise, carry, seed = 0xbeef;
struct mfb_window *window = mfb_open_ex("Noise Test", WIDTH, HEIGHT, WF_RESIZABLE);
if (!window)
return 0;
for (;;)
{
int i;
mfb_update_state state;
for (i = 0; i < WIDTH * HEIGHT; ++i)
{
noise = seed;
noise >>= 3;
noise ^= seed;
carry = noise & 1;
noise >>= 1;
seed >>= 1;
seed |= (carry << 30);
noise &= 0xFF;
g_buffer[i] = MFB_RGB(noise, noise, noise);
}
state = mfb_update(window, g_buffer);
if (state != STATE_OK) {
window = 0x0;
break;
}
}
return 0;
}