Working on the Display API

This commit is contained in:
Joshua Granick
2015-07-31 12:10:57 -07:00
parent 0b9926a87a
commit fd376e6f99
9 changed files with 365 additions and 263 deletions

View File

@@ -9,6 +9,9 @@ import lime.graphics.ConsoleRenderContext;
import lime.graphics.GLRenderContext;
import lime.graphics.RenderContext;
import lime.graphics.Renderer;
import lime.math.Rectangle;
import lime.system.Display;
import lime.system.DisplayMode;
import lime.system.System;
import lime.ui.Gamepad;
import lime.ui.Window;
@@ -17,6 +20,7 @@ import lime.ui.Window;
@:access(lime._backend.native.NativeRenderer)
@:access(lime.app.Application)
@:access(lime.graphics.Renderer)
@:access(lime.system.Display)
@:access(lime.ui.Gamepad)
@:access(lime.ui.Window)
@@ -55,6 +59,33 @@ class NativeApplication {
handle = lime_application_create (null);
#if !lime_console
// TODO: Evolve this more
var displays:Array<Dynamic> = lime_display_get_details ();
for (displayInfo in displays) {
var display = new Display ();
display.name = displayInfo.name;
display.supportedModes = [];
display.bounds = new Rectangle (displayInfo.bounds.x, displayInfo.bounds.y, displayInfo.bounds.width, displayInfo.bounds.height);
for (mode in cast (displayInfo.supportedModes, Array<Dynamic>)) {
var displayMode = new DisplayMode (mode.width, mode.height, mode.refreshRate, mode.pixelFormat);
display.supportedModes.push (displayMode);
}
display.currentMode = display.supportedModes[displayInfo.currentMode];
Display.devices.push (display);
}
#end
if (config != null) {
setFrameRate (config.fps);
@@ -444,6 +475,7 @@ class NativeApplication {
private static var lime_application_set_frame_rate = System.load ("lime", "lime_application_set_frame_rate", 2);
private static var lime_application_update = System.load ("lime", "lime_application_update", 1);
private static var lime_application_quit = System.load ("lime", "lime_application_quit", 1);
private static var lime_display_get_details = System.load ("lime", "lime_display_get_details", 0);
private static var lime_gamepad_event_manager_register = System.load ("lime", "lime_gamepad_event_manager_register", 2);
private static var lime_key_event_manager_register = System.load ("lime", "lime_key_event_manager_register", 2);
private static var lime_mouse_event_manager_register = System.load ("lime", "lime_mouse_event_manager_register", 2);

View File

@@ -1,184 +1,40 @@
package lime.system;
import lime.math.Rectangle;
import lime.math.Vector2;
import lime.system.Display;
/**
* ...
* @author larsiusprime
*/
import lime.math.Rectangle;
class Display {
/********STATIC*********/
public static var devices = new Map<Int, Display> ();
/**How many display devices are currently connected and actively displaying visuals**/
public static var numDisplays(get, null):Int;
public static var devices = new Array<Display> ();
/**
* Sync with the OS to get the current display device information
*/
public static function init():Void {
for (i in 0...numDisplays) {
var d = new Display(i);
d.sync();
devices.set(i, d);
}
}
* The desktop area represented by this display, with the upper-leftmost display at 0,0
**/
public var bounds (default, null):Rectangle;
/**
* Get the display device with the given id
* @param id
* @return
*/
public static function get(id:Int):Display {
if (devices.exists(id)) {
return devices.get(id);
}
return null;
}
* The current display mode
**/
public var currentMode (default, null):DisplayMode;
/**
* Get the total number of connected displays
* @return
*/
private static function get_numDisplays():Int {
return lime_display_get_num_devices();
}
/*********INSTANCE**********/
/**Which number is assigned to the display device by the OS**/
public var id (default, null):Int;
/**The name of the device, such as "Samsung SyncMaster P2350", "iPhone 6", "Occulus Rift DK2", etc.**/
* The name of the device, such as "Samsung SyncMaster P2350", "iPhone 6", "Occulus Rift DK2", etc.
**/
public var name (default, null):String;
/**Number of horizontal and vertical pixels currently being displayed**/
public var resolution(default, null):Resolution;
/**Horizontal resolution / Vertical resolution**/
public var aspectRatio(get, null):Float;
/**The current display mode**/
public var mode(default, null):DisplayMode;
/**All of the display modes supported by this device**/
public var modes(default, null):Array<DisplayMode>;
/**The desktop area represented by this display, with the upper-leftmost display at 0,0**/
public var bounds(default, null):Rectangle;
private function new(id:Int) {
this.id = id;
sync();
}
/**
* Updates this object's data with the latest information from the OS about the device
*/
public function sync():Void {
* All of the display modes supported by this device
**/
public var supportedModes (default, null):Array<DisplayMode>;
name = lime_display_get_name(id);
var obj = null;
private function new () {
obj = lime_display_get_current_display_mode(id);
mode = new DisplayMode(obj.width, obj.height, obj.refresh_rate, obj.format);
resolution = new Resolution(mode.width, mode.height);
obj = lime_display_get_display_bounds(id);
bounds = new Rectangle(obj.x, obj.y, obj.width, obj.height);
modes = [];
var numModes = lime_display_get_num_display_modes(id);
for (i in 0...numModes) {
obj = lime_display_get_display_mode(id, i);
modes.push(new DisplayMode(obj.width, obj.height, obj.refresh_rate, obj.format));
}
}
/**GET / SET**/
private function get_aspectRatio():Float {
if (resolution.height != 0) {
return resolution.width / resolution.height;
}
return 0;
}
// Native Methods
#if (cpp || neko || nodejs)
private static var lime_display_get_num_devices = System.load("lime", "lime_display_get_num_devices", 0);
private static var lime_display_get_name = System.load ("lime", "lime_display_get_name", 1);
private static var lime_display_get_num_display_modes = System.load ("lime", "lime_display_get_num_display_modes", 1);
private static var lime_display_get_display_bounds = System.load ("lime", "lime_display_get_display_bounds", 1);
private static var lime_display_get_display_mode = System.load ("lime", "lime_display_get_display_mode", 2);
private static var lime_display_get_current_display_mode = System.load ("lime", "lime_display_get_current_display_mode", 1);
#end
}
class DisplayMode {
/**horizontal resolution**/
public var width(default, null):Int;
/**vertical resolution**/
public var height(default, null):Int;
/**refresh rate in Hz**/
public var refreshRate(default, null):Int;
/**pixel color format**/
public var format(default, null):Int;
public function new(width:Int, height:Int, refreshRate:Int, format:Int) {
this.width = width;
this.height = height;
this.refreshRate = refreshRate;
this.format = format;
}
}
abstract Resolution (Vector2) from Vector2 {
public inline function new (width:Float = 0, height:Float = 0) this = new Vector2(width, height);
public var width(get, never):Float;
public var height(get, never):Float;
inline function get_width ():Float return this.x;
inline function get_height ():Float return this.y;
}

View File

@@ -0,0 +1,41 @@
package lime.system;
import lime.graphics.PixelFormat;
class DisplayMode {
/**
* vertical resolution
**/
public var height (default, null):Int;
/**
* pixel format
**/
public var pixelFormat (default, null):PixelFormat;
/**
* refresh rate in Hz
**/
public var refreshRate (default, null):Int;
/**
* horizontal resolution
**/
public var width (default, null):Int;
public function new (width:Int, height:Int, refreshRate:Int, pixelFormat:PixelFormat) {
this.width = width;
this.height = height;
this.refreshRate = refreshRate;
this.pixelFormat = pixelFormat;
}
}

View File

@@ -18,6 +18,7 @@ namespace lime {
Rectangle (value rect);
void Contract (double x, double y, double width, double height);
value Value ();
double height;
double width;

View File

@@ -1,24 +1,24 @@
#ifndef LIME_SYSTEM_DISPLAY_H
#define LIME_SYSTEM_DISPLAY_H
#include <SDL.h>
#include <hx/CFFI.h>
namespace lime {
class Display {
public:
static value GetCurrentDisplayMode (int displayIndex);
static value GetDisplayMode (int displayIndex, int modeIndex);
static value GetDisplayBounds (int displayIndex);
static int GetNumDevices ();
static const char* GetDisplayName (int displayIndex);
static int GetNumDisplayModes (int displayIndex);
static value GetDetails ();
};
}

View File

@@ -161,6 +161,13 @@ namespace lime {
}
value lime_display_get_details () {
return Display::GetDetails ();
}
void lime_font_destroy (value handle) {
Font *font = (Font*)(intptr_t)val_float (handle);
@@ -391,41 +398,6 @@ namespace lime {
}
value lime_display_get_display_bounds (value displayIndex) {
return Display::GetDisplayBounds(val_int(displayIndex));
}
value lime_display_get_current_display_mode (value displayIndex) {
return Display::GetCurrentDisplayMode(val_int(displayIndex));
}
value lime_display_get_display_mode (value displayIndex, value modeIndex) {
return Display::GetDisplayMode(val_int(displayIndex), val_int(modeIndex));
}
value lime_display_get_name (value displayIndex) {
return alloc_string (Display::GetDisplayName (val_int (displayIndex)));
}
value lime_display_get_num_devices () {
return alloc_int (Display::GetNumDevices());
}
value lime_display_get_num_display_modes (value displayIndex) {
return alloc_int (Display::GetNumDisplayModes(val_int (displayIndex)));
}
value lime_gamepad_add_mappings (value mappings) {
@@ -1192,6 +1164,7 @@ namespace lime {
DEFINE_PRIM (lime_bytes_from_data_pointer, 2);
DEFINE_PRIM (lime_bytes_get_data_pointer, 1);
DEFINE_PRIM (lime_bytes_read_file, 1);
DEFINE_PRIM (lime_display_get_details, 0);
DEFINE_PRIM (lime_font_get_ascender, 1);
DEFINE_PRIM (lime_font_get_descender, 1);
DEFINE_PRIM (lime_font_get_family_name, 1);
@@ -1208,12 +1181,6 @@ namespace lime {
DEFINE_PRIM (lime_font_render_glyph, 3);
DEFINE_PRIM (lime_font_render_glyphs, 3);
DEFINE_PRIM (lime_font_set_size, 2);
DEFINE_PRIM (lime_display_get_display_bounds, 1);
DEFINE_PRIM (lime_display_get_current_display_mode, 1);
DEFINE_PRIM (lime_display_get_display_mode, 2);
DEFINE_PRIM (lime_display_get_name, 1);
DEFINE_PRIM (lime_display_get_num_devices, 0);
DEFINE_PRIM (lime_display_get_num_display_modes, 1);
DEFINE_PRIM (lime_gamepad_add_mappings, 1);
DEFINE_PRIM (lime_gamepad_event_manager_register, 2);
DEFINE_PRIM (lime_gamepad_get_device_guid, 1);

View File

@@ -1,67 +1,106 @@
#include <graphics/PixelFormat.h>
#include <math/Rectangle.h>
#include <system/Display.h>
#include <SDL.h>
namespace lime {
value Display::GetDisplayBounds (int displayIndex) {
static int id_bounds;
static int id_currentMode;
static int id_height;
static int id_name;
static int id_pixelFormat;
static int id_refreshRate;
static int id_supportedModes;
static int id_width;
static bool init = false;
SDL_Rect rect = { 0, 0, 0, 0};
SDL_GetDisplayBounds(displayIndex, &rect);
value mValue = alloc_empty_object ();
alloc_field (mValue, val_id("x"), alloc_int(rect.x));
alloc_field (mValue, val_id("y"), alloc_int(rect.y));
alloc_field (mValue, val_id("width"), alloc_int(rect.w));
alloc_field (mValue, val_id("height"), alloc_int(rect.h));
return mValue;
value Display::GetDetails () {
if (!init) {
id_bounds = val_id ("bounds");
id_currentMode = val_id ("currentMode");
id_height = val_id ("height");
id_name = val_id ("name");
id_pixelFormat = val_id ("pixelFormat");
id_refreshRate = val_id ("refreshRate");
id_supportedModes = val_id ("supportedModes");
id_width = val_id ("width");
init = true;
}
int numDevices = SDL_GetNumVideoDisplays ();
value devices = alloc_array (numDevices);
value display, supportedModes, mode;
int numDisplayModes;
SDL_Rect bounds = { 0, 0, 0, 0 };
SDL_DisplayMode currentDisplayMode = { SDL_PIXELFORMAT_UNKNOWN, 0, 0, 0, 0 };
SDL_DisplayMode displayMode = { SDL_PIXELFORMAT_UNKNOWN, 0, 0, 0, 0 };
for (int i = 0; i < numDevices; i++) {
display = alloc_empty_object ();
alloc_field (display, id_name, alloc_string (SDL_GetDisplayName (i)));
SDL_GetDisplayBounds (i, &bounds);
alloc_field (display, id_bounds, Rectangle (bounds.x, bounds.y, bounds.w, bounds.h).Value ());
SDL_GetCurrentDisplayMode (i, &currentDisplayMode);
numDisplayModes = SDL_GetNumDisplayModes (i);
supportedModes = alloc_array (numDisplayModes);
for (int j = 0; j < numDisplayModes; j++) {
SDL_GetDisplayMode (i, j, &displayMode);
if (displayMode.format == currentDisplayMode.format && displayMode.w == currentDisplayMode.w && displayMode.h == currentDisplayMode.h && displayMode.refresh_rate == currentDisplayMode.refresh_rate) {
alloc_field (display, id_currentMode, alloc_int (j));
}
mode = alloc_empty_object ();
alloc_field (mode, id_height, alloc_int (displayMode.h));
switch (displayMode.format) {
case SDL_PIXELFORMAT_ARGB8888:
alloc_field (mode, id_pixelFormat, alloc_int (ARGB32));
break;
case SDL_PIXELFORMAT_BGRA8888:
case SDL_PIXELFORMAT_BGRX8888:
alloc_field (mode, id_pixelFormat, alloc_int (BGRA32));
break;
default:
alloc_field (mode, id_pixelFormat, alloc_int (RGBA32));
}
alloc_field (mode, id_refreshRate, alloc_int (displayMode.refresh_rate));
alloc_field (mode, id_width, alloc_int (displayMode.w));
val_array_set_i (supportedModes, j, mode);
}
alloc_field (display, id_supportedModes, supportedModes);
val_array_set_i (devices, i, display);
}
return devices;
}
value Display::GetCurrentDisplayMode (int displayIndex) {
SDL_DisplayMode mode = { SDL_PIXELFORMAT_UNKNOWN, 0, 0, 0, 0 };
SDL_GetCurrentDisplayMode(displayIndex, &mode);
value mValue = alloc_empty_object ();
alloc_field (mValue, val_id("width"), alloc_int(mode.w));
alloc_field (mValue, val_id("height"), alloc_int(mode.h));
alloc_field (mValue, val_id("refresh_rate"), alloc_int(mode.refresh_rate));
alloc_field (mValue, val_id("format"), alloc_int(mode.format));
return mValue;
}
value Display::GetDisplayMode (int displayIndex, int modeIndex) {
SDL_DisplayMode mode = { SDL_PIXELFORMAT_UNKNOWN, 0, 0, 0, 0 };
SDL_GetDisplayMode(displayIndex, modeIndex, &mode);
value mValue = alloc_empty_object ();
alloc_field (mValue, val_id("width"), alloc_int(mode.w));
alloc_field (mValue, val_id("height"), alloc_int(mode.h));
alloc_field (mValue, val_id("refresh_rate"), alloc_int(mode.refresh_rate));
alloc_field (mValue, val_id("format"), alloc_int(mode.format));
return mValue;
}
int Display::GetNumDevices() {
return SDL_GetNumVideoDisplays();
}
const char* Display::GetDisplayName (int displayIndex) {
return SDL_GetDisplayName(displayIndex);
}
int Display::GetNumDisplayModes (int displayIndex) {
return SDL_GetNumDisplayModes(displayIndex);
}
}

View File

@@ -70,4 +70,26 @@ namespace lime {
}
value Rectangle::Value () {
if (!init) {
id_height = val_id ("height");
id_width = val_id ("width");
id_x = val_id ("x");
id_y = val_id ("y");
init = true;
}
value rect = alloc_empty_object ();
alloc_field (rect, id_height, alloc_float (height));
alloc_field (rect, id_width, alloc_float (width));
alloc_field (rect, id_x, alloc_float (x));
alloc_field (rect, id_y, alloc_float (y));
return rect;
}
}

View File

@@ -0,0 +1,144 @@
#include <graphics/ImageBuffer.h>
namespace lime {
static int id_bitsPerPixel;
static int id_buffer;
static int id_data;
static int id_format;
static int id_height;
static int id_premultiplied;
static int id_transparent;
static int id_width;
static bool init = false;
ImageBuffer::ImageBuffer () {
width = 0;
height = 0;
bitsPerPixel = 32;
format = RGBA32;
data = 0;
premultiplied = false;
transparent = false;
}
ImageBuffer::ImageBuffer (value imageBuffer) {
if (!init) {
id_bitsPerPixel = val_id ("bitsPerPixel");
id_transparent = val_id ("transparent");
id_buffer = val_id ("buffer");
id_width = val_id ("width");
id_height = val_id ("height");
id_data = val_id ("data");
id_format = val_id ("format");
id_premultiplied = val_id ("premultiplied");
init = true;
}
width = val_int (val_field (imageBuffer, id_width));
height = val_int (val_field (imageBuffer, id_height));
bitsPerPixel = val_int (val_field (imageBuffer, id_bitsPerPixel));
format = (PixelFormat)val_int (val_field (imageBuffer, id_format));
transparent = val_bool (val_field (imageBuffer, id_transparent));
value data_value = val_field (imageBuffer, id_data);
value buffer_value = val_field (data_value, id_buffer);
premultiplied = val_bool (val_field (imageBuffer, id_premultiplied));
data = new Bytes (buffer_value);
}
ImageBuffer::~ImageBuffer () {
delete data;
}
void ImageBuffer::Blit (const unsigned char *data, int x, int y, int width, int height) {
if (x < 0 || x + width > this->width || y < 0 || y + height > this->height) {
return;
}
int stride = Stride ();
unsigned char *bytes = this->data->Data ();
for (int i = 0; i < height; i++) {
memcpy (&bytes[(i + y) * this->width + x], &data[i * width], stride);
}
}
void ImageBuffer::Resize (int width, int height, int bitsPerPixel) {
this->bitsPerPixel = bitsPerPixel;
this->width = width;
this->height = height;
int stride = Stride ();
if (!this->data) {
this->data = new Bytes (height * stride);
} else {
this->data->Resize (height * stride);
}
}
int ImageBuffer::Stride () {
return width * (((bitsPerPixel + 3) & ~0x3) >> 3);
}
value ImageBuffer::Value () {
if (!init) {
id_bitsPerPixel = val_id ("bitsPerPixel");
id_transparent = val_id ("transparent");
id_buffer = val_id ("buffer");
id_width = val_id ("width");
id_height = val_id ("height");
id_data = val_id ("data");
id_format = val_id ("format");
id_premultiplied = val_id ("premultiplied");
init = true;
}
mValue = alloc_empty_object ();
alloc_field (mValue, id_width, alloc_int (width));
alloc_field (mValue, id_height, alloc_int (height));
alloc_field (mValue, id_bitsPerPixel, alloc_int (bitsPerPixel));
alloc_field (mValue, id_data, data ? data->Value () : alloc_null ());
alloc_field (mValue, id_transparent, alloc_bool (transparent));
alloc_field (mValue, id_format, alloc_int (format));
alloc_field (mValue, id_premultiplied, alloc_bool (premultiplied));
return mValue;
}
}