Add: [Win32] Video driver that uses OpenGL to transfer the video buffer to the screen.
parent
af4d32357c
commit
ef478ade64
@ -1,3 +1,4 @@
|
|||||||
add_subdirectory(md5)
|
add_subdirectory(md5)
|
||||||
add_subdirectory(squirrel)
|
add_subdirectory(squirrel)
|
||||||
|
add_subdirectory(opengl)
|
||||||
add_subdirectory(os2)
|
add_subdirectory(os2)
|
||||||
|
@ -0,0 +1,5 @@
|
|||||||
|
add_files(
|
||||||
|
khrplatform.h
|
||||||
|
glext.h
|
||||||
|
CONDITION OPENGL_FOUND
|
||||||
|
)
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,290 @@
|
|||||||
|
#ifndef __khrplatform_h_
|
||||||
|
#define __khrplatform_h_
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Copyright (c) 2008-2018 The Khronos Group Inc.
|
||||||
|
**
|
||||||
|
** Permission is hereby granted, free of charge, to any person obtaining a
|
||||||
|
** copy of this software and/or associated documentation files (the
|
||||||
|
** "Materials"), to deal in the Materials without restriction, including
|
||||||
|
** without limitation the rights to use, copy, modify, merge, publish,
|
||||||
|
** distribute, sublicense, and/or sell copies of the Materials, and to
|
||||||
|
** permit persons to whom the Materials are furnished to do so, subject to
|
||||||
|
** the following conditions:
|
||||||
|
**
|
||||||
|
** The above copyright notice and this permission notice shall be included
|
||||||
|
** in all copies or substantial portions of the Materials.
|
||||||
|
**
|
||||||
|
** THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||||
|
** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||||
|
** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||||
|
** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||||
|
** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||||
|
** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||||
|
** MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* Khronos platform-specific types and definitions.
|
||||||
|
*
|
||||||
|
* The master copy of khrplatform.h is maintained in the Khronos EGL
|
||||||
|
* Registry repository at https://github.com/KhronosGroup/EGL-Registry
|
||||||
|
* The last semantic modification to khrplatform.h was at commit ID:
|
||||||
|
* 67a3e0864c2d75ea5287b9f3d2eb74a745936692
|
||||||
|
*
|
||||||
|
* Adopters may modify this file to suit their platform. Adopters are
|
||||||
|
* encouraged to submit platform specific modifications to the Khronos
|
||||||
|
* group so that they can be included in future versions of this file.
|
||||||
|
* Please submit changes by filing pull requests or issues on
|
||||||
|
* the EGL Registry repository linked above.
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* See the Implementer's Guidelines for information about where this file
|
||||||
|
* should be located on your system and for more details of its use:
|
||||||
|
* http://www.khronos.org/registry/implementers_guide.pdf
|
||||||
|
*
|
||||||
|
* This file should be included as
|
||||||
|
* #include <KHR/khrplatform.h>
|
||||||
|
* by Khronos client API header files that use its types and defines.
|
||||||
|
*
|
||||||
|
* The types in khrplatform.h should only be used to define API-specific types.
|
||||||
|
*
|
||||||
|
* Types defined in khrplatform.h:
|
||||||
|
* khronos_int8_t signed 8 bit
|
||||||
|
* khronos_uint8_t unsigned 8 bit
|
||||||
|
* khronos_int16_t signed 16 bit
|
||||||
|
* khronos_uint16_t unsigned 16 bit
|
||||||
|
* khronos_int32_t signed 32 bit
|
||||||
|
* khronos_uint32_t unsigned 32 bit
|
||||||
|
* khronos_int64_t signed 64 bit
|
||||||
|
* khronos_uint64_t unsigned 64 bit
|
||||||
|
* khronos_intptr_t signed same number of bits as a pointer
|
||||||
|
* khronos_uintptr_t unsigned same number of bits as a pointer
|
||||||
|
* khronos_ssize_t signed size
|
||||||
|
* khronos_usize_t unsigned size
|
||||||
|
* khronos_float_t signed 32 bit floating point
|
||||||
|
* khronos_time_ns_t unsigned 64 bit time in nanoseconds
|
||||||
|
* khronos_utime_nanoseconds_t unsigned time interval or absolute time in
|
||||||
|
* nanoseconds
|
||||||
|
* khronos_stime_nanoseconds_t signed time interval in nanoseconds
|
||||||
|
* khronos_boolean_enum_t enumerated boolean type. This should
|
||||||
|
* only be used as a base type when a client API's boolean type is
|
||||||
|
* an enum. Client APIs which use an integer or other type for
|
||||||
|
* booleans cannot use this as the base type for their boolean.
|
||||||
|
*
|
||||||
|
* Tokens defined in khrplatform.h:
|
||||||
|
*
|
||||||
|
* KHRONOS_FALSE, KHRONOS_TRUE Enumerated boolean false/true values.
|
||||||
|
*
|
||||||
|
* KHRONOS_SUPPORT_INT64 is 1 if 64 bit integers are supported; otherwise 0.
|
||||||
|
* KHRONOS_SUPPORT_FLOAT is 1 if floats are supported; otherwise 0.
|
||||||
|
*
|
||||||
|
* Calling convention macros defined in this file:
|
||||||
|
* KHRONOS_APICALL
|
||||||
|
* KHRONOS_APIENTRY
|
||||||
|
* KHRONOS_APIATTRIBUTES
|
||||||
|
*
|
||||||
|
* These may be used in function prototypes as:
|
||||||
|
*
|
||||||
|
* KHRONOS_APICALL void KHRONOS_APIENTRY funcname(
|
||||||
|
* int arg1,
|
||||||
|
* int arg2) KHRONOS_APIATTRIBUTES;
|
||||||
|
*/
|
||||||
|
|
||||||
|
#if defined(__SCITECH_SNAP__) && !defined(KHRONOS_STATIC)
|
||||||
|
# define KHRONOS_STATIC 1
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/*-------------------------------------------------------------------------
|
||||||
|
* Definition of KHRONOS_APICALL
|
||||||
|
*-------------------------------------------------------------------------
|
||||||
|
* This precedes the return type of the function in the function prototype.
|
||||||
|
*/
|
||||||
|
#if defined(KHRONOS_STATIC)
|
||||||
|
/* If the preprocessor constant KHRONOS_STATIC is defined, make the
|
||||||
|
* header compatible with static linking. */
|
||||||
|
# define KHRONOS_APICALL
|
||||||
|
#elif defined(_WIN32)
|
||||||
|
# define KHRONOS_APICALL __declspec(dllimport)
|
||||||
|
#elif defined (__SYMBIAN32__)
|
||||||
|
# define KHRONOS_APICALL IMPORT_C
|
||||||
|
#elif defined(__ANDROID__)
|
||||||
|
# define KHRONOS_APICALL __attribute__((visibility("default")))
|
||||||
|
#else
|
||||||
|
# define KHRONOS_APICALL
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/*-------------------------------------------------------------------------
|
||||||
|
* Definition of KHRONOS_APIENTRY
|
||||||
|
*-------------------------------------------------------------------------
|
||||||
|
* This follows the return type of the function and precedes the function
|
||||||
|
* name in the function prototype.
|
||||||
|
*/
|
||||||
|
#if defined(_WIN32) && !defined(_WIN32_WCE) && !defined(__SCITECH_SNAP__)
|
||||||
|
/* Win32 but not WinCE */
|
||||||
|
# define KHRONOS_APIENTRY __stdcall
|
||||||
|
#else
|
||||||
|
# define KHRONOS_APIENTRY
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/*-------------------------------------------------------------------------
|
||||||
|
* Definition of KHRONOS_APIATTRIBUTES
|
||||||
|
*-------------------------------------------------------------------------
|
||||||
|
* This follows the closing parenthesis of the function prototype arguments.
|
||||||
|
*/
|
||||||
|
#if defined (__ARMCC_2__)
|
||||||
|
#define KHRONOS_APIATTRIBUTES __softfp
|
||||||
|
#else
|
||||||
|
#define KHRONOS_APIATTRIBUTES
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/*-------------------------------------------------------------------------
|
||||||
|
* basic type definitions
|
||||||
|
*-----------------------------------------------------------------------*/
|
||||||
|
#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || defined(__GNUC__) || defined(__SCO__) || defined(__USLC__)
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Using <stdint.h>
|
||||||
|
*/
|
||||||
|
#include <stdint.h>
|
||||||
|
typedef int32_t khronos_int32_t;
|
||||||
|
typedef uint32_t khronos_uint32_t;
|
||||||
|
typedef int64_t khronos_int64_t;
|
||||||
|
typedef uint64_t khronos_uint64_t;
|
||||||
|
#define KHRONOS_SUPPORT_INT64 1
|
||||||
|
#define KHRONOS_SUPPORT_FLOAT 1
|
||||||
|
|
||||||
|
#elif defined(__VMS ) || defined(__sgi)
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Using <inttypes.h>
|
||||||
|
*/
|
||||||
|
#include <inttypes.h>
|
||||||
|
typedef int32_t khronos_int32_t;
|
||||||
|
typedef uint32_t khronos_uint32_t;
|
||||||
|
typedef int64_t khronos_int64_t;
|
||||||
|
typedef uint64_t khronos_uint64_t;
|
||||||
|
#define KHRONOS_SUPPORT_INT64 1
|
||||||
|
#define KHRONOS_SUPPORT_FLOAT 1
|
||||||
|
|
||||||
|
#elif defined(_WIN32) && !defined(__SCITECH_SNAP__)
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Win32
|
||||||
|
*/
|
||||||
|
typedef __int32 khronos_int32_t;
|
||||||
|
typedef unsigned __int32 khronos_uint32_t;
|
||||||
|
typedef __int64 khronos_int64_t;
|
||||||
|
typedef unsigned __int64 khronos_uint64_t;
|
||||||
|
#define KHRONOS_SUPPORT_INT64 1
|
||||||
|
#define KHRONOS_SUPPORT_FLOAT 1
|
||||||
|
|
||||||
|
#elif defined(__sun__) || defined(__digital__)
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Sun or Digital
|
||||||
|
*/
|
||||||
|
typedef int khronos_int32_t;
|
||||||
|
typedef unsigned int khronos_uint32_t;
|
||||||
|
#if defined(__arch64__) || defined(_LP64)
|
||||||
|
typedef long int khronos_int64_t;
|
||||||
|
typedef unsigned long int khronos_uint64_t;
|
||||||
|
#else
|
||||||
|
typedef long long int khronos_int64_t;
|
||||||
|
typedef unsigned long long int khronos_uint64_t;
|
||||||
|
#endif /* __arch64__ */
|
||||||
|
#define KHRONOS_SUPPORT_INT64 1
|
||||||
|
#define KHRONOS_SUPPORT_FLOAT 1
|
||||||
|
|
||||||
|
#elif 0
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Hypothetical platform with no float or int64 support
|
||||||
|
*/
|
||||||
|
typedef int khronos_int32_t;
|
||||||
|
typedef unsigned int khronos_uint32_t;
|
||||||
|
#define KHRONOS_SUPPORT_INT64 0
|
||||||
|
#define KHRONOS_SUPPORT_FLOAT 0
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Generic fallback
|
||||||
|
*/
|
||||||
|
#include <stdint.h>
|
||||||
|
typedef int32_t khronos_int32_t;
|
||||||
|
typedef uint32_t khronos_uint32_t;
|
||||||
|
typedef int64_t khronos_int64_t;
|
||||||
|
typedef uint64_t khronos_uint64_t;
|
||||||
|
#define KHRONOS_SUPPORT_INT64 1
|
||||||
|
#define KHRONOS_SUPPORT_FLOAT 1
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Types that are (so far) the same on all platforms
|
||||||
|
*/
|
||||||
|
typedef signed char khronos_int8_t;
|
||||||
|
typedef unsigned char khronos_uint8_t;
|
||||||
|
typedef signed short int khronos_int16_t;
|
||||||
|
typedef unsigned short int khronos_uint16_t;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Types that differ between LLP64 and LP64 architectures - in LLP64,
|
||||||
|
* pointers are 64 bits, but 'long' is still 32 bits. Win64 appears
|
||||||
|
* to be the only LLP64 architecture in current use.
|
||||||
|
*/
|
||||||
|
#ifdef _WIN64
|
||||||
|
typedef signed long long int khronos_intptr_t;
|
||||||
|
typedef unsigned long long int khronos_uintptr_t;
|
||||||
|
typedef signed long long int khronos_ssize_t;
|
||||||
|
typedef unsigned long long int khronos_usize_t;
|
||||||
|
#else
|
||||||
|
typedef signed long int khronos_intptr_t;
|
||||||
|
typedef unsigned long int khronos_uintptr_t;
|
||||||
|
typedef signed long int khronos_ssize_t;
|
||||||
|
typedef unsigned long int khronos_usize_t;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if KHRONOS_SUPPORT_FLOAT
|
||||||
|
/*
|
||||||
|
* Float type
|
||||||
|
*/
|
||||||
|
typedef float khronos_float_t;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if KHRONOS_SUPPORT_INT64
|
||||||
|
/* Time types
|
||||||
|
*
|
||||||
|
* These types can be used to represent a time interval in nanoseconds or
|
||||||
|
* an absolute Unadjusted System Time. Unadjusted System Time is the number
|
||||||
|
* of nanoseconds since some arbitrary system event (e.g. since the last
|
||||||
|
* time the system booted). The Unadjusted System Time is an unsigned
|
||||||
|
* 64 bit value that wraps back to 0 every 584 years. Time intervals
|
||||||
|
* may be either signed or unsigned.
|
||||||
|
*/
|
||||||
|
typedef khronos_uint64_t khronos_utime_nanoseconds_t;
|
||||||
|
typedef khronos_int64_t khronos_stime_nanoseconds_t;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Dummy value used to pad enum types to 32 bits.
|
||||||
|
*/
|
||||||
|
#ifndef KHRONOS_MAX_ENUM
|
||||||
|
#define KHRONOS_MAX_ENUM 0x7FFFFFFF
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Enumerated boolean type
|
||||||
|
*
|
||||||
|
* Values other than zero should be considered to be true. Therefore
|
||||||
|
* comparisons should not be made against KHRONOS_TRUE.
|
||||||
|
*/
|
||||||
|
typedef enum {
|
||||||
|
KHRONOS_FALSE = 0,
|
||||||
|
KHRONOS_TRUE = 1,
|
||||||
|
KHRONOS_BOOLEAN_ENUM_FORCE_SIZE = KHRONOS_MAX_ENUM
|
||||||
|
} khronos_boolean_enum_t;
|
||||||
|
|
||||||
|
#endif /* __khrplatform_h_ */
|
@ -0,0 +1,222 @@
|
|||||||
|
/* $Id$ */
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of OpenTTD.
|
||||||
|
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
|
||||||
|
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||||
|
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/** @file opengl_v.cpp OpenGL video driver support. */
|
||||||
|
|
||||||
|
#include "../stdafx.h"
|
||||||
|
|
||||||
|
#if defined(_WIN32)
|
||||||
|
# include <windows.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(__APPLE__)
|
||||||
|
# include <OpenGL/gl3.h>
|
||||||
|
#else
|
||||||
|
# include <GL/gl.h>
|
||||||
|
#endif
|
||||||
|
#include "../3rdparty/opengl/glext.h"
|
||||||
|
|
||||||
|
#include "opengl.h"
|
||||||
|
#include "../core/mem_func.hpp"
|
||||||
|
#include "../gfx_func.h"
|
||||||
|
#include "../debug.h"
|
||||||
|
|
||||||
|
#include "../safeguards.h"
|
||||||
|
|
||||||
|
|
||||||
|
/* static */ OpenGLBackend *OpenGLBackend::instance = nullptr;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Find a substring in a string made of space delimited elements. The substring
|
||||||
|
* has to match the complete element, partial matches don't count.
|
||||||
|
* @param string List of space delimited elements.
|
||||||
|
* @param substring Substring to find.
|
||||||
|
* @return Pointer to the start of the match or nullptr if the substring is not present.
|
||||||
|
*/
|
||||||
|
static const char *FindStringInExtensionList(const char *string, const char *substring)
|
||||||
|
{
|
||||||
|
while (1) {
|
||||||
|
/* Is the extension string present at all? */
|
||||||
|
const char *pos = strstr(string, substring);
|
||||||
|
if (pos == nullptr) break;
|
||||||
|
|
||||||
|
/* Is this a real match, i.e. are the chars before and after the matched string
|
||||||
|
* indeed spaces (or the start or end of the string, respectively)? */
|
||||||
|
const char *end = pos + strlen(substring);
|
||||||
|
if ((pos == string || pos[-1] == ' ') && (*end == ' ' || *end == '\0')) return pos;
|
||||||
|
|
||||||
|
/* False hit, try again for the remaining string. */
|
||||||
|
string = end;
|
||||||
|
}
|
||||||
|
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if an OpenGL extension is supported by the current context.
|
||||||
|
* @param extension The extension string to test.
|
||||||
|
* @return True if the extension is supported, false if not.
|
||||||
|
*/
|
||||||
|
static bool IsOpenGLExtensionSupported(const char *extension)
|
||||||
|
{
|
||||||
|
return FindStringInExtensionList((const char *)glGetString(GL_EXTENSIONS), extension) != nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
static byte _gl_major_ver = 0; ///< Major OpenGL version.
|
||||||
|
static byte _gl_minor_ver = 0; ///< Minor OpenGL version.
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if the current OpenGL version is equal or higher than a given one.
|
||||||
|
* @param major Minimal major version.
|
||||||
|
* @param minor Minimal minor version.
|
||||||
|
* @pre OpenGL was initialized.
|
||||||
|
* @return True if the OpenGL version is equal or higher than the requested one.
|
||||||
|
*/
|
||||||
|
static bool IsOpenGLVersionAtLeast(byte major, byte minor)
|
||||||
|
{
|
||||||
|
return (_gl_major_ver > major) || (_gl_major_ver == major && _gl_minor_ver >= minor);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create and initialize the singleton back-end class.
|
||||||
|
*/
|
||||||
|
/* static */ const char *OpenGLBackend::Create()
|
||||||
|
{
|
||||||
|
if (OpenGLBackend::instance != nullptr) OpenGLBackend::Destroy();
|
||||||
|
|
||||||
|
OpenGLBackend::instance = new OpenGLBackend();
|
||||||
|
return OpenGLBackend::instance->Init();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Free resources and destroy singleton back-end class.
|
||||||
|
*/
|
||||||
|
/* static */ void OpenGLBackend::Destroy()
|
||||||
|
{
|
||||||
|
delete OpenGLBackend::instance;
|
||||||
|
OpenGLBackend::instance = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Construct OpenGL back-end class.
|
||||||
|
*/
|
||||||
|
OpenGLBackend::OpenGLBackend()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Free allocated resources.
|
||||||
|
*/
|
||||||
|
OpenGLBackend::~OpenGLBackend()
|
||||||
|
{
|
||||||
|
glDeleteTextures(1, &this->vid_texture);
|
||||||
|
free(this->vid_buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check for the needed OpenGL functionality and allocate all resources.
|
||||||
|
* @return Error string or nullptr if successful.
|
||||||
|
*/
|
||||||
|
const char *OpenGLBackend::Init()
|
||||||
|
{
|
||||||
|
/* Always query the supported OpenGL version as the current context might have changed. */
|
||||||
|
const char *ver = (const char *)glGetString(GL_VERSION);
|
||||||
|
const char *vend = (const char *)glGetString(GL_VENDOR);
|
||||||
|
const char *renderer = (const char *)glGetString(GL_RENDERER);
|
||||||
|
|
||||||
|
if (ver == nullptr || vend == nullptr || renderer == nullptr) return "OpenGL not supported";
|
||||||
|
|
||||||
|
DEBUG(driver, 1, "OpenGL driver: %s - %s (%s)", vend, renderer, ver);
|
||||||
|
|
||||||
|
const char *minor = strchr(ver, '.');
|
||||||
|
_gl_major_ver = atoi(ver);
|
||||||
|
_gl_minor_ver = minor != nullptr ? atoi(minor + 1) : 0;
|
||||||
|
|
||||||
|
/* OpenGL 1.3 is the absolute minimum. */
|
||||||
|
if (!IsOpenGLVersionAtLeast(1, 3)) return "OpenGL version >= 1.3 required";
|
||||||
|
/* Check for non-power-of-two texture support. */
|
||||||
|
if (!IsOpenGLVersionAtLeast(2, 0) && !IsOpenGLExtensionSupported("GL_ARB_texture_non_power_of_two")) return "Non-power-of-two textures not supported";
|
||||||
|
|
||||||
|
/* Setup video buffer texture. */
|
||||||
|
glGenTextures(1, &this->vid_texture);
|
||||||
|
glBindTexture(GL_TEXTURE_2D, this->vid_texture);
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
||||||
|
glBindTexture(GL_TEXTURE_2D, 0);
|
||||||
|
if (glGetError() != GL_NO_ERROR) return "Can't generate video buffer texture";
|
||||||
|
|
||||||
|
/* Prime vertex array with a full-screen quad. */
|
||||||
|
static const float vert_array[] = { 1.f, -1.f, 1.f, 1.f, -1.f, -1.f, -1.f, 1.f };
|
||||||
|
static const float tex_array[] = { 1.f, 1.f, 1.f, 0.f, 0.f, 1.f, 0.f, 0.f };
|
||||||
|
|
||||||
|
glVertexPointer(2, GL_FLOAT, 0, vert_array);
|
||||||
|
glTexCoordPointer(2, GL_FLOAT, 0, tex_array);
|
||||||
|
glEnableClientState(GL_VERTEX_ARRAY);
|
||||||
|
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
|
||||||
|
|
||||||
|
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
||||||
|
glDisable(GL_DEPTH_TEST);
|
||||||
|
glEnable(GL_TEXTURE_2D);
|
||||||
|
(void)glGetError(); // Clear errors.
|
||||||
|
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Change the size of the drawing window and allocate matching resources.
|
||||||
|
* @param w New width of the window.
|
||||||
|
* @param h New height of the window.
|
||||||
|
* @param force Recreate resources even if size didn't change.
|
||||||
|
* @param False if nothing had to be done, true otherwise.
|
||||||
|
*/
|
||||||
|
bool OpenGLBackend::Resize(int w, int h, bool force)
|
||||||
|
{
|
||||||
|
if (!force && _screen.width == w && _screen.height == h && this->vid_buffer != nullptr) return false;
|
||||||
|
|
||||||
|
glViewport(0, 0, w, h);
|
||||||
|
|
||||||
|
/* Re-allocate video buffer texture. */
|
||||||
|
free(this->vid_buffer);
|
||||||
|
this->vid_buffer = CallocT<uint32>(w * h); // 32bpp
|
||||||
|
|
||||||
|
glBindTexture(GL_TEXTURE_2D, this->vid_texture);
|
||||||
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, w, h, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, nullptr);
|
||||||
|
glBindTexture(GL_TEXTURE_2D, 0);
|
||||||
|
|
||||||
|
/* Set new viewport. */
|
||||||
|
_screen.height = h;
|
||||||
|
_screen.width = w;
|
||||||
|
_screen.pitch = w;
|
||||||
|
_screen.dst_ptr = this->GetVideoBuffer();
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Render video buffer to the screen.
|
||||||
|
*/
|
||||||
|
void OpenGLBackend::Paint()
|
||||||
|
{
|
||||||
|
assert(this->vid_buffer != nullptr);
|
||||||
|
|
||||||
|
glClear(GL_COLOR_BUFFER_BIT);
|
||||||
|
|
||||||
|
/* Update changed rect of the video buffer texture. */
|
||||||
|
glBindTexture(GL_TEXTURE_2D, this->vid_texture);
|
||||||
|
glPixelStorei(GL_UNPACK_ROW_LENGTH, _screen.pitch);
|
||||||
|
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, _screen.width, _screen.height, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, this->vid_buffer);
|
||||||
|
|
||||||
|
/* Blit video buffer to screen. */
|
||||||
|
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,49 @@
|
|||||||
|
/* $Id$ */
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of OpenTTD.
|
||||||
|
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
|
||||||
|
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||||
|
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/** @file opengl.h OpenGL video driver support. */
|
||||||
|
|
||||||
|
#ifndef VIDEO_OPENGL_H
|
||||||
|
#define VIDEO_OPENGL_H
|
||||||
|
|
||||||
|
#include "../core/alloc_type.hpp"
|
||||||
|
|
||||||
|
/** Platform-independent back-end singleton class for OpenGL video drivers. */
|
||||||
|
class OpenGLBackend : public ZeroedMemoryAllocator {
|
||||||
|
private:
|
||||||
|
static OpenGLBackend *instance; ///< Singleton instance pointer.
|
||||||
|
|
||||||
|
void *vid_buffer; ///< Pointer to the memory used for the video driver to draw to.
|
||||||
|
GLuint vid_texture; ///< Texture handle for the video buffer texture.
|
||||||
|
|
||||||
|
OpenGLBackend();
|
||||||
|
~OpenGLBackend();
|
||||||
|
|
||||||
|
const char *Init();
|
||||||
|
|
||||||
|
public:
|
||||||
|
/** Get singleton instance of this class. */
|
||||||
|
static inline OpenGLBackend *Get()
|
||||||
|
{
|
||||||
|
return OpenGLBackend::instance;
|
||||||
|
}
|
||||||
|
static const char *Create();
|
||||||
|
static void Destroy();
|
||||||
|
|
||||||
|
bool Resize(int w, int h, bool force = false);
|
||||||
|
void Paint();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a pointer to the memory for the video driver to draw to.
|
||||||
|
* @return Pointer to draw on.
|
||||||
|
*/
|
||||||
|
void *GetVideoBuffer() { return this->vid_buffer; }
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif /* VIDEO_OPENGL_H */
|
Loading…
Reference in New Issue