本文整理汇总了C++中SDL_GL_CreateContext函数的典型用法代码示例。如果您正苦于以下问题:C++ SDL_GL_CreateContext函数的具体用法?C++ SDL_GL_CreateContext怎么用?C++ SDL_GL_CreateContext使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了SDL_GL_CreateContext函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main(int argc, char * arg[])
{
//Controls the game loop
bool run=true;
// init everyting - SDL, if it is nonzero we have a problem
if(SDL_Init(SDL_INIT_EVERYTHING) != 0)
{
std::cout << "ERROR SDL_Init " <<SDL_GetError()<< std::endl;
return -1;
}
//Request opengl 4.1 context, Core Context
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK,SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
//Create a window
SDL_Window * window = SDL_CreateWindow(
"SDL", // window title
SDL_WINDOWPOS_CENTERED, // x position, centered
SDL_WINDOWPOS_CENTERED, // y position, centered
640, // width, in pixels
480, // height, in pixels
SDL_WINDOW_OPENGL // flags
);
// Create an OpenGL context associated with the window.
SDL_GLContext glcontext = SDL_GL_CreateContext(window);
//Call our InitOpenGL Function
initOpenGL();
//Set our viewport
setViewport(640,480);
initScene();
//Value to hold the event generated by SDL
SDL_Event event;
//Game Loop
while(run)
{
//While we still have events in the queue
while (SDL_PollEvent(&event)) {
//Get event type
if (event.type == SDL_QUIT || event.type == SDL_WINDOWEVENT_CLOSE) {
//set our boolean which controls the loop to false
run = false;
}
if (event.type==SDL_KEYDOWN){
switch( event.key.keysym.sym )
{
case SDLK_LEFT:
break;
case SDLK_RIGHT:
break;
case SDLK_UP:
break;
case SDLK_DOWN:
break;
default:
break;
}
}
}
//init Scene
update();
//render
render();
//Call swap so that our GL back buffer is displayed
SDL_GL_SwapWindow(window);
}
// clean up, reverse order!!!
cleanUp();
SDL_GL_DeleteContext(glcontext);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
示例2: main
//Program entry point
int main(int argc, char *argv[])
{
UNUSED(argc);
UNUSED(argv);
//paths required by platform code
platform::app_code.app_dll_path = "App.dll";
platform::app_state.record_file = "input.smi";
//Load the application dll
platform_load_app_code(&platform::app_code);
//initialize sdl
if (SDL_Init(SDL_INIT_EVERYTHING) < 0)
{
std::cerr << ERROR_LINE << "SDL Error: Unable to initialize SDL" << std::endl;
}
else
{
std::cout << "SDL initialized"<<std::endl;
}
//SDL version
{
SDL_version ver;
SDL_VERSION(&ver);
std::cout << "SDL version " << (int)ver.major << "." << (int)ver.minor << "." << (int)ver.patch << std::endl;
SDL_GetVersion(&ver);
std::cout << "Linked version " << (int)ver.major << "." << (int)ver.minor << "." << (int)ver.patch << std::endl;
}
//init time
platform_update_time(platform::app_time);
//call app config to get settings
platform::app_code.config(&platform::services.config);
//Set Opengl attributes
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, PLATFORM_GL_VERSION_MAJOR);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, PLATFORM_GL_VERSION_MINOR);
//Set buffer attributes
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, PLATFORM_GL_DOUBLEBUFFER);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, PLATFORM_GL_DEPTHBUFFER);
//Create a window
platform_create_window();
if (!platform::services.config.sdl_window)
{
std::cerr << ERROR_LINE << "Error: Unable to create window" << std::endl;
return -1;
}
else
{
platform::services.config.quit = false;
std::cout << "SDL window " << platform::services.config.width << "x" << platform::services.config.height << " opened" << std::endl;
}
//set OpenGL context
auto sdl_context = SDL_GL_CreateContext(platform::services.config.sdl_window);
// Initialise GLEW
glewExperimental = GL_TRUE;
GLenum glewError = glewInit();
if (glewError != GLEW_OK)
{
std::cerr << ERROR_LINE << "GLEW Error: Unable to initialise glew";
return -1;
}
else
{
std::cout<<"GLEW "<<glewGetString(GLEW_VERSION)<<" Initialised";
}
//Allocate memory for the app
auto base_address = Gigabytes(1);
platform::services.memory = {};
platform::services.memory.permanent_memory_size = Megabytes(8);
platform::services.memory.permanent_memory = platform::os::get_memory(base_address,platform::services.memory.permanent_memory_size);
if (!platform::services.memory.permanent_memory)
{
std::cerr << "\nUnable to allocate enough memory";
return -1;
}
//link memory to app state
platform::app_state.memory = &platform::services.memory;
//call the application startup
platform::app_code.startup(&platform::services);
//run application
#ifdef __EMSCRIPTEN__
emscripten_set_main_loop(platform_update, 0, true);
#else
//.........这里部分代码省略.........
示例3: PlatformCreateOpenGLContext
FPlatformOpenGLContext* PlatformCreateOpenGLContext(FPlatformOpenGLDevice* Device, void* InWindowHandle)
{
Device->SharedContext->Context = SDL_GL_CreateContext((SDL_Window*)InWindowHandle);
return Device->SharedContext;
}
示例4: main
int main(int argc, char *argv[])
{
SDL_Window *window;
SDL_GLContext context;
// Slightly different SDL initialization
if ( SDL_Init(SDL_INIT_VIDEO) != 0 ) {
printf("Unable to initialize SDL: %s\n", SDL_GetError());
return 1;
}
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); // *new*
window = SDL_CreateWindow("sdl_fog_density", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_OPENGL);
if ( !window ) {
printf("Unable to create window: %s\n", SDL_GetError());
return 1;
}
context = SDL_GL_CreateContext(window);
// Set the OpenGL state after creating the context with SDL_SetVideoMode
glClearColor( 0, 0, 0, 0 );
glEnable( GL_TEXTURE_2D ); // Needed when we're using the fixed-function pipeline.
glViewport( 0, 0, 640, 480 );
glMatrixMode( GL_PROJECTION );
glPushMatrix(); // just for testing
glLoadIdentity();
glOrtho( 0, 640, 480, 0, -1000, 1000 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
// Load the OpenGL texture
GLuint texture; // Texture object handle
SDL_Surface *surface; // Gives us the information to make the texture
if ( (surface = IMG_Load("screenshot.png")) ) {
// Check that the image's width is a power of 2
if ( (surface->w & (surface->w - 1)) != 0 ) {
printf("warning: image.bmp's width is not a power of 2\n");
}
// Also check if the height is a power of 2
if ( (surface->h & (surface->h - 1)) != 0 ) {
printf("warning: image.bmp's height is not a power of 2\n");
}
// Have OpenGL generate a texture object handle for us
glGenTextures( 1, &texture );
// Bind the texture object
glBindTexture( GL_TEXTURE_2D, texture );
// Set the texture's stretching properties
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
//SDL_LockSurface(surface);
// Add some greyness
memset(surface->pixels, 0x66, surface->w*surface->h);
// Edit the texture object's image data using the information SDL_Surface gives us
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, surface->w, surface->h, 0,
GL_RGBA, GL_UNSIGNED_BYTE, surface->pixels );
//SDL_UnlockSurface(surface);
}
else {
printf("SDL could not load image.bmp: %s\n", SDL_GetError());
SDL_Quit();
return 1;
}
// Free the SDL_Surface only if it was successfully created
if ( surface ) {
SDL_FreeSurface( surface );
}
// Clear the screen before drawing
glClear( GL_COLOR_BUFFER_BIT );
// Bind the texture to which subsequent calls refer to
glBindTexture( GL_TEXTURE_2D, texture );
glEnable(GL_FOG);
GLfloat fogColor[] = { 1.0, 0.5, 0.5, 0.05 };
glFogfv(GL_FOG_COLOR, fogColor);
glFogf(GL_FOG_DENSITY, 0.2);
glFogi(GL_FOG_MODE, GL_EXP2);
assert(glIsEnabled(GL_FOG));
//.........这里部分代码省略.........
示例5: VideoMode
bool OpenGLSdlGraphicsManager::setupMode(uint width, uint height) {
// In case we request a fullscreen mode we will use the mode the user
// has chosen last time or the biggest mode available.
if (_wantsFullScreen) {
if (_desiredFullscreenWidth && _desiredFullscreenHeight) {
// In case only a distinct set of modes is available we check
// whether the requested mode is actually available.
if (!_fullscreenVideoModes.empty()) {
VideoModeArray::const_iterator i = Common::find(_fullscreenVideoModes.begin(),
_fullscreenVideoModes.end(),
VideoMode(_desiredFullscreenWidth, _desiredFullscreenHeight));
// It's not available fall back to default.
if (i == _fullscreenVideoModes.end()) {
_desiredFullscreenWidth = 0;
_desiredFullscreenHeight = 0;
}
}
}
// In case no desired mode has been set we default to the biggest mode
// available or the requested mode in case we don't know any
// any fullscreen modes.
if (!_desiredFullscreenWidth || !_desiredFullscreenHeight) {
if (!_fullscreenVideoModes.empty()) {
VideoModeArray::const_iterator i = _fullscreenVideoModes.end();
--i;
_desiredFullscreenWidth = i->width;
_desiredFullscreenHeight = i->height;
} else {
_desiredFullscreenWidth = width;
_desiredFullscreenHeight = height;
}
}
// Remember our choice.
ConfMan.setInt("last_fullscreen_mode_width", _desiredFullscreenWidth, Common::ConfigManager::kApplicationDomain);
ConfMan.setInt("last_fullscreen_mode_height", _desiredFullscreenHeight, Common::ConfigManager::kApplicationDomain);
// Use our choice.
width = _desiredFullscreenWidth;
height = _desiredFullscreenHeight;
}
// This is pretty confusing since RGBA8888 talks about the memory
// layout here. This is a different logical layout depending on
// whether we run on little endian or big endian. However, we can
// only safely assume that RGBA8888 in memory layout is supported.
// Thus, we chose this one.
const Graphics::PixelFormat rgba8888 =
#ifdef SCUMM_LITTLE_ENDIAN
Graphics::PixelFormat(4, 8, 8, 8, 8, 0, 8, 16, 24);
#else
Graphics::PixelFormat(4, 8, 8, 8, 8, 24, 16, 8, 0);
#endif
#if SDL_VERSION_ATLEAST(2, 0, 0)
if (_glContext) {
notifyContextDestroy();
SDL_GL_DeleteContext(_glContext);
_glContext = nullptr;
}
_window->destroyWindow();
uint32 flags = SDL_WINDOW_OPENGL;
if (_wantsFullScreen) {
flags |= SDL_WINDOW_FULLSCREEN;
} else {
flags |= SDL_WINDOW_RESIZABLE;
}
// Request a OpenGL (ES) context we can use.
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, _glContextProfileMask);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, _glContextMajor);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, _glContextMinor);
if (!_window->createWindow(width, height, flags)) {
// We treat fullscreen requests as a "hint" for now. This means in
// case it is not available we simply ignore it.
if (_wantsFullScreen) {
_window->createWindow(width, height, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
}
if (!_window->getSDLWindow()) {
return false;
}
}
_glContext = SDL_GL_CreateContext(_window->getSDLWindow());
if (!_glContext) {
return false;
}
notifyContextCreate(rgba8888, rgba8888);
int actualWidth, actualHeight;
getWindowDimensions(&actualWidth, &actualHeight);
setActualScreenSize(actualWidth, actualHeight);
_eventSource->resetKeyboardEmulation(actualWidth - 1, actualHeight - 1);
//.........这里部分代码省略.........
示例6: main
int main(int argc, char **argv)
{
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
fprintf(stderr, "Error: Unable to init SDL: %s\n", SDL_GetError());
exit(1);
}
SDL_Window* screen;
SDL_GLContext glcontext;
screen = SDL_CreateWindow("Window Name", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
600, 600,SDL_WINDOW_OPENGL);
glcontext = SDL_GL_CreateContext(screen);
SDL_ShowCursor(false);
GLenum err = glewInit();
if (err != GLEW_OK) fprintf(stderr, "Error: %s", glewGetErrorString(err));
if (!screen) {
fprintf(stderr, "Unable to start video: %s\n", SDL_GetError());
}
setupGL();
light=glm::vec3(0.0,-100.0,20.0);
//setup geometry
geoms.push_back(new CCubeVAO(1000,10,1000,glm::vec3(0,-50,0)));
geoms.push_back(new CCubeVAO(50,50,10,glm::vec3(-100,0,0)));
geoms.push_back(new CCubeVAO(10,10,10,glm::vec3(100,70,-100)));
geoms.push_back(new CCubeVAO(100,10,500,glm::vec3(200,50,-100)));
geoms.push_back(new ImageVAO(tex));
yaw=0;
pit=0;
bool quit=false;
long long count=0;
while(!quit){
input(screen,quit);
render(screen,count++);
}
//cleanup
glUseProgram(0);
for(int x=0;x<geoms.size();x++)
delete geoms.at(x);
glDeleteProgram(program);
glDeleteTextures(1,&tex);
SDL_GL_DeleteContext(glcontext);
}
示例7: main
int main(int argc, char* argv[]) {
if(SDL_Init(SDL_INIT_EVERYTHING) < 0)
{
std::cout << "Could not init SDL 2" << std::endl;
return 0;
}
SDL_Window *mainWindow = SDL_CreateWindow("Game",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
WINDOW_WIDTH,
WINDOW_HEIGHT,
SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
if(!mainWindow)
{
std::cout << "Could not create a window" << std::endl;
return 0;
}
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GLContext mainContext = SDL_GL_CreateContext(mainWindow);
SDL_GL_SetSwapInterval(1);
int value = 0;
SDL_GL_GetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, &value);
std::cout << "SDL_GL_CONTEXT_MAJOR_VERSION: " << value << std::endl;
SDL_GL_GetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, &value);
std::cout << "SDL_GL_CONTEXT_MINOR_VERSION: " << value << std::endl;
struct stat statBuffer = {};
bool loop = true;
GameState gameState = {};
gameState.memory.mainMemorySz = MEGABYTES(128);
gameState.memory.mainMemory = mmap(0,
gameState.memory.mainMemorySz,
PROT_READ | PROT_WRITE,
MAP_ANONYMOUS | MAP_PRIVATE,
-1, 0);
gameState.memory.transientMemorySz = GIGABYTES(2);
gameState.memory.transientMemory = mmap(0,
gameState.memory.transientMemorySz,
PROT_READ | PROT_WRITE,
MAP_ANONYMOUS | MAP_PRIVATE,
-1, 0);
int currentTime = SDL_GetTicks();
while(loop)
{
int lastTime = currentTime;
currentTime = SDL_GetTicks();
int elapsedTime = currentTime - lastTime;
// NOTE(Brett): Check for the libgame.so and load the newest verion of the functions
stat("libgame.so", &statBuffer);
if(LastLibBuildTime == 0 || (statBuffer.st_mtime > LastLibBuildTime))
{
if(LibHandle)
dlclose(LibHandle);
LibHandle = dlopen("libgame.so", RTLD_GLOBAL);
GameUpdateAndRender = (update_method)dlsym(LibHandle, "UpdateAndRender");
GameSetup = (setup_method)dlsym(LibHandle, "Setup");
LastLibBuildTime = (int)statBuffer.st_mtime;
gameState.initialized = false;
GameSetup(&gameState);
}
SDL_Event event;
while(SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT)
loop = false;
if(event.type == SDL_KEYDOWN)
{
switch(event.key.keysym.sym)
{
case SDLK_ESCAPE:
loop = false;
break;
default:
break;
}
}
}
GameUpdateAndRender(&gameState);
//.........这里部分代码省略.........
示例8: GLimp_SetMode
//.........这里部分代码省略.........
if( ( screen = SDL_CreateWindow( CLIENT_WINDOW_TITLE, x, y,
glConfig.vidWidth, glConfig.vidHeight, flags ) ) == 0 )
{
ri.Printf( PRINT_DEVELOPER, "SDL_CreateWindow failed: %s\n", SDL_GetError( ) );
continue;
}
if( fullscreen )
{
SDL_DisplayMode mode;
switch( testColorBits )
{
case 16: mode.format = SDL_PIXELFORMAT_RGB565; break;
case 24: mode.format = SDL_PIXELFORMAT_RGB24; break;
default: ri.Printf( PRINT_DEVELOPER, "testColorBits is %d, can't fullscreen\n", testColorBits ); continue;
}
mode.w = glConfig.vidWidth;
mode.h = glConfig.vidHeight;
mode.refresh_rate = glConfig.displayFrequency = ri.Cvar_VariableIntegerValue( "r_displayRefresh" );
mode.driverdata = NULL;
if( SDL_SetWindowDisplayMode( screen, &mode ) < 0 )
{
ri.Printf( PRINT_DEVELOPER, "SDL_SetWindowDisplayMode failed: %s\n", SDL_GetError( ) );
continue;
}
}
SDL_SetWindowTitle( screen, CLIENT_WINDOW_TITLE );
SDL_SetWindowIcon( screen, icon );
if( ( opengl_context = (QGLContext)SDL_GL_CreateContext( screen ) ) == NULL )
{
Com_Printf( "SDL_GL_CreateContext failed: %s\n", SDL_GetError( ) );
continue;
}
if( SDL_GL_MakeCurrent( screen, opengl_context ) < 0 )
{
Com_Printf( "SDL_GL_MakeCurrent failed: %s\n", SDL_GetError( ) );
continue;
}
SDL_GL_SetSwapInterval( r_swapInterval->integer );
glConfig.colorBits = testColorBits;
glConfig.depthBits = testDepthBits;
glConfig.stencilBits = testStencilBits;
Com_Printf( "Using %d color bits, %d depth, %d stencil display.\n",
glConfig.colorBits, glConfig.depthBits, glConfig.stencilBits );
break;
}
/*SDL_FreeSurface( icon );*/
GLimp_DetectAvailableModes();
glstring = (char *) qglGetString (GL_RENDERER);
Com_Printf( "GL_RENDERER: %s\n", glstring );
return RSERR_OK;
#if 0
const char* glstring;
示例9: main
int main(int argc, char **argv){
/* vertices for a triangle */
/* Why does triangle[] = { vec4_new(...) } result in a segfault when returning
* from _mm_load_ps?
*/
/* Just want a sort of 3D object, but not a closed object otherwise it's hard
* to tell what's going on w/ flat shading */
vec4_t object[18];
//+Z face
object[0] = vec4_new(-1, -1, 1, 1);
object[1] = vec4_new(1, -1, 1, 1);
object[2] = vec4_new(1, 1, 1, 1);
object[3] = vec4_new(1, 1, 1, 1);
object[4] = vec4_new(-1, 1, 1, 1);
object[5] = vec4_new(-1, -1, 1, 1);
//+X face
object[6] = vec4_new(1, -1, 1, 1);
object[7] = vec4_new(1, -1, -1, 1);
object[8] = vec4_new(1, 1, -1, 1);
object[9] = vec4_new(1, 1, -1, 1);
object[10] = vec4_new(1, 1, 1, 1);
object[11] = vec4_new(1, -1, 1, 1);
//-X face
object[12] = vec4_new(-1, -1, 1, 1);
object[13] = vec4_new(-1, -1, -1, 1);
object[14] = vec4_new(-1, 1, -1, 1);
object[15] = vec4_new(-1, 1, -1, 1);
object[16] = vec4_new(-1, 1, 1, 1);
object[17] = vec4_new(-1, -1, 1, 1);
if (SDL_Init(SDL_INIT_EVERYTHING) != 0){
fprintf(stderr, "SDL Init error: %s\n", SDL_GetError());
return 1;
}
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
#ifdef DEBUG
SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_DEBUG_FLAG);
#endif
SDL_Window *win = SDL_CreateWindow("SSE GL Test", SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED, WIN_WIDTH, WIN_HEIGHT, SDL_WINDOW_OPENGL);
SDL_GLContext context = SDL_GL_CreateContext(win);
if (check_GL_error("Opened win + context")){
SDL_GL_DeleteContext(context);
SDL_DestroyWindow(win);
return 1;
}
glewExperimental = GL_TRUE;
GLenum err = glewInit();
if (err != GLEW_OK){
fprintf(stderr, "GLEW init error %d\n", err);
SDL_GL_DeleteContext(context);
SDL_DestroyWindow(win);
return 1;
}
check_GL_error("Post GLEW init");
#ifdef DEBUG
glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB);
glDebugMessageCallbackARB(gl_debug_callback, NULL);
glDebugMessageControlARB(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE,
0, NULL, GL_TRUE);
#endif
glClearColor(0, 0, 0, 1);
glClearDepth(1);
glEnable(GL_DEPTH_TEST);
//Model's vao and vbo
GLuint model[2];
glGenVertexArrays(1, model);
glBindVertexArray(model[0]);
glGenBuffers(1, model + 1);
glBindBuffer(GL_ARRAY_BUFFER, model[1]);
glBufferData(GL_ARRAY_BUFFER, 4 * 6 * 3 * sizeof(GLfloat), object, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0);
if (check_GL_error("Setup buffers")){
return 1;
}
GLint vshader = make_shader(vert_shader_src, GL_VERTEX_SHADER);
GLint fshader = make_shader(frag_shader_src, GL_FRAGMENT_SHADER);
if (vshader == -1 || fshader == -1){
return 1;
}
GLint program = make_program(vshader, fshader);
if (program == -1){
return 1;
}
glDeleteShader(vshader);
glDeleteShader(fshader);
mat4_t model_mat = mat4_mult(mat4_rotate(45, vec4_new(1, 1, 0, 0)), mat4_scale(2, 2, 2));
model_mat = mat4_mult(mat4_translate(vec4_new(0, 2, -5, 1)), model_mat);
mat4_t view_mat = mat4_look_at(vec4_new(0, 0, 5, 0), vec4_new(0, 0, 0, 0), vec4_new(0, 1, 0, 0));
mat4_t proj_mat = mat4_perspective(75, ((float)WIN_WIDTH) / WIN_HEIGHT, 1, 100);
glUseProgram(program);
//.........这里部分代码省略.........
示例10: SDL_Init
Window::Window() {
SDL_Init(SDL_INIT_VIDEO);
displayWindow = SDL_CreateWindow("My Game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_OPENGL);
SDL_GLContext context = SDL_GL_CreateContext(displayWindow);
SDL_GL_MakeCurrent(displayWindow, context);
}
示例11: bbStringToUTF8String
BBSDLContext *bbSDLGraphicsCreateGraphics( int width,int height,int depth,int hz,int flags ) {
int mode;
char * appTitle = bbStringToUTF8String( bbAppTitle );
int windowFlags = 0;
if ((flags & FLAGS_DX) == 0) {
windowFlags = SDL_WINDOW_OPENGL;
}
if (flags & FLAGS_BORDERLESS) windowFlags |= SDL_WINDOW_BORDERLESS;
if( depth ){
windowFlags |= SDL_WINDOW_FULLSCREEN;
mode=MODE_DISPLAY;
} else {
if (flags & FLAGS_FULLSCREEN) {
windowFlags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
}
mode=MODE_WINDOW;
}
if ((flags & FLAGS_DX) == 0) {
if (flags & FLAGS_BACKBUFFER) SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
if (flags & FLAGS_ALPHABUFFER) SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 1);
if (flags & FLAGS_DEPTHBUFFER) SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
if (flags & FLAGS_STENCILBUFFER) SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 1);
}
#ifdef __ANDROID__
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 6);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
#endif
#ifdef __RASPBERRYPI__
int rpi_mode;
int rpi_group;
if (depth && (flags & FLAGS_RPI_TV_FULLSCREEN)) {
bmx_tvservice_get_closest_mode(width, height, hz, &rpi_mode, &rpi_group);
bmx_tvservice_setmode(rpi_mode, rpi_group, width, height);
}
SDL_VideoInit(NULL);
#endif
SDL_Window *window = SDL_CreateWindow(appTitle, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
width, height, windowFlags);
if (window == NULL) {
printf("error... %s\n", SDL_GetError());fflush(stdout);
return NULL;
}
SDL_GLContext context = 0;
if ((flags & FLAGS_DX) == 0) {
SDL_GL_SetSwapInterval(-1);
context = SDL_GL_CreateContext(window);
}
BBSDLContext *bbcontext=(BBSDLContext*)malloc( sizeof(BBSDLContext) );
memset( bbcontext,0,sizeof(BBSDLContext) );
bbcontext->mode=mode;
bbcontext->width=width;
bbcontext->height=height;
#ifdef __RASPBERRYPI__
bbcontext->depth=16;
#else
bbcontext->depth=24;
#endif
bbcontext->hertz=hz;
bbcontext->flags=flags;
bbcontext->sync=-1;
bbcontext->window=window;
bbcontext->context=context;
SDL_GetWindowWMInfo(window, &bbcontext->info);
return bbcontext;
}
示例12: main
int main(int argc, const char * argv[]) {
// Window size
int SCREEN_WIDTH = 800;
int SCREEN_HEIGHT = 600;
SDL_Window *mainwindow; /* Our window handle */
SDL_GLContext maincontext; /* Our opengl context handle */
SDL_Init(SDL_INIT_VIDEO);
mainwindow = SDL_CreateWindow(PROGRAM_NAME, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);
/* Create our opengl context and attach it to our window */
maincontext = SDL_GL_CreateContext(mainwindow);
/* This makes our buffer swap syncronized with the monitor's vertical refresh */
SDL_GL_SetSwapInterval(1);
player.setColor(0, 1, 0);
myGrid.size = 9;
myGrid.LinesX = 10;
myGrid.LinesY = 10;
myGrid.placeTypes();
myGrid.printTypes();
SDL_Event event;
bool running = true;
// Initialize camera
changeSize(SCREEN_WIDTH, SCREEN_HEIGHT);
while (running) {
Uint32 start = SDL_GetTicks();
//std::cout << "Start: " << start << std::endl;
while (SDL_PollEvent(&event)) {
switch (event.type) {
// Capture any window event
// https://wiki.libsdl.org/SDL_WindowEvent
case SDL_WINDOWEVENT:
switch (event.window.event) {
case SDL_WINDOWEVENT_RESIZED:
int w = event.window.data1;
int h = event.window.data2;
changeSize(w, h);
break;
}
break;
// Handle keyboar events
case SDL_KEYDOWN:
switch (event.key.keysym.sym) {
case SDLK_UP:
std::cout << "Up key pressed" << std::endl;
player.move(1, 0, 0);
std::cout << "Player pos X " << player.posX << std::endl;
break;
case SDLK_DOWN:
std::cout << "Down key pressed" << std::endl;
player.move(-1, 0, 0);
std::cout << "Player pos X " << player.posX << std::endl;
break;
case SDLK_LEFT:
std::cout << "Left key pressed" << std::endl;
player.move(0, 1, 0);
std::cout << "Player pos Y " << player.posY << std::endl;
break;
case SDLK_RIGHT:
std::cout << "Right key pressed" << std::endl;
player.move(0, -1, 0);
std::cout << "Player pos Y " << player.posY << std::endl;
break;
}
break;
case SDL_QUIT:
running = false;
break;
}
}
renderScene(mainwindow);
// Insure that screen update happen no more then 60 fps
//std::cout << "End: " << SDL_GetTicks() << std::endl;
if (1000/60>(SDL_GetTicks() - start)) {
SDL_Delay(1000/60 - (SDL_GetTicks() - start));
}
}
/* Delete our opengl context, destroy our window, and shutdown SDL */
SDL_GL_DeleteContext(maincontext);
SDL_DestroyWindow(mainwindow);
SDL_Quit();
return 0;
}
示例13: main
/* Run Program */
int main (int argc, char** argv) {
//Read input argument
int steps=1000;
double h = 0.1;
if (argc == 1){
printf("USAGE: NumberOfSteps StepSize/Tolerance\nExited.\n");
return 0;
}
if (argc > 1) steps = atoi(argv[1]);
if (argc > 2) h = atof(argv[2]);
printf("Input arguments: %i %f \n", steps, h);
//Check if SDL Initialization is successful
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
return -1;
}
//Create Window
SDL_Window *win = SDL_CreateWindow("Exercise 5, Lorenz Equations",50,50,800,600,SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL);
if (!win) {
fprintf(stderr, "Could not create window: %s\n", SDL_GetError());
}
//Initialize OpenGL
SDL_GLContext context = SDL_GL_CreateContext(win);
if (!context) {
fprintf(stderr, "Could not create OpenGL context: %s\n", SDL_GetError());
}
glViewport(0,0,(GLsizei)800,(GLsizei)600);
glClearColor(1.0f,1.0f,1.0f,1.0f);
glClear(GL_COLOR_BUFFER_BIT);
SDL_GL_SwapWindow(win);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glEnable(GL_DEPTH_TEST);
glFrustum(-100,100,-100,100,0.01,100);
glEnable(GL_POLYGON_OFFSET_FILL);
glPolygonOffset(1,1);
glEnable(GL_LIGHTING);
glShadeModel(GL_SMOOTH);
GLfloat lightcol[] = {0.9,0.9,0.9,1.0};
GLfloat lightamb[] = {0.5,0.5,0.5,1.0};
GLfloat lightpos[] = {20.0,20.0,30.0,1.0};
glLightfv(GL_LIGHT0,GL_AMBIENT,lightamb);
glLightfv(GL_LIGHT0,GL_DIFFUSE,lightcol);
glEnable(GL_LIGHT0);
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE);
glEnable(GL_RESCALE_NORMAL);
//Init all vectors
double t0 = 0;
double* t = malloc(sizeof(double)*steps);
double* xValues = malloc(sizeof(double)*steps);
double* yValues = malloc(sizeof(double)*steps);
double* zValues = malloc(sizeof(double)*steps);
double* xValues_it = malloc(sizeof(double)*steps);
double* yValues_it = malloc(sizeof(double)*steps);
double* zValues_it = malloc(sizeof(double)*steps);
double xmin = -4;
double xmax = 4;
double ymin = -4;
double ymax = 4;
double xsteps = 21;
double ysteps = 21;
struct Matrix* X = new_Matrix(xsteps,ysteps);
struct Matrix* Y = new_Matrix(xsteps,ysteps);
struct Matrix* Z = new_Matrix(xsteps,ysteps);
meshgrid(xmin,xmax,ymin,ymax,xsteps,ysteps,X,Y);
paraboloid(X,Y,Z);
struct Vector* vec0 = new_Vector(DIM);
vec0->values[0] = 1;
vec0->values[1] = 1;
vec0->values[2] = 2;
vec0->values[3] = 1-0.25;
vec0->values[4] = -1-0.25;
vec0->values[5] = -1;
struct Vector** vec = malloc(sizeof(struct Vector*)*steps);
for (int j=1; j < steps; j++) {
vec[j] = new_Vector(DIM);
}
//find geodesic iteratively
struct Vector** vec_it = malloc(sizeof(struct Vector*)*steps);
vec_it[0] = vec0;
for (int j=1; j < steps; j++) {
vec_it[j] = new_Vector(DIM);
geodesic_step(vec_it[j-1],h,vec_it[j]);
}
for (int j=0; j < steps; j++) {
xValues_it[j] = vec_it[j]->values[0];
yValues_it[j] = vec_it[j]->values[1];
zValues_it[j] = vec_it[j]->values[2];
}
//.........这里部分代码省略.........
示例14: EmitSDLError
bool Application::Init(int w, int h) {
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
return EmitSDLError("error initialize SDL");
return false;
}
#ifdef __APPLE__
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
#else
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
#endif
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
window_ = SDL_CreateWindow(nullptr, SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, w, h, SDL_WINDOW_OPENGL);
gl_context_ = SDL_GL_CreateContext(window_);
if (gl_context_ == nullptr) {
return EmitSDLError("error creating GL context");
}
// Enable vsync
SDL_GL_SetSwapInterval(1);
if (TTF_Init() != 0) {
return EmitSDLError("error initialize font system");
}
std::string resource_folder_name = std::string(kResourceFolderName) + "/";
std::string font_file_name = resource_folder_name + kFontFileName;
font_ = TTF_OpenFont(font_file_name.c_str(), 14);
if (font_ == nullptr) {
std::string message;
message += "error read font ";
message += kFontFileName;
message += "at size " + std::to_string(14);
return EmitSDLError(message.c_str());
}
std::string demo_file_name = resource_folder_name + kDemoFileName;
world_ = ParseWorld((CreateJsonObject(demo_file_name.c_str())));
world_->Start(World::EngineType::kLiquidFun);
// We have two camera setup: the first one for viewing the board and the
// second one for background. The second camera does not move.
cameras_.resize(2);
cameras_[0] =
make_unique<Camera>(Vector3f(0, -50, 650), Vector3f(0, 50, 0),
Vector3f(0, 1, 0), M_PI / 2.0, 1.0, 10.0, 500000.0);
cameras_[1] =
make_unique<Camera>(Vector3f(0, 0, 800), Vector3f(0, 0, 0),
Vector3f(0, 1, 0), M_PI / 2.0, 1.0, 10.0, 500000.0);
poly_drawer_ = make_unique<NodeGroupDrawer<NodeBuldgedDrawer>>(
LoadMesh3DGLProgram(), cameras_[0].get(), Vector2f(w, h));
path_drawer_ = make_unique<NodeGroupDrawer<NodePathDrawer>>(
LoadSimpleGLProgram(), cameras_[0].get(), Vector2f(w, h));
particle_drawer_ = make_unique<NodeGroupDrawer<SphereDrawer>>(
LoadSphereGLProgram(), cameras_[0].get(), Vector2f(w, h));
// Add Node to each group drawer.
for (size_t i = 0; i < world_->GetNumNodes(); ++i) {
auto& collision_shapes = world_->GetNodeByIndex(i)->collision_shapes;
if (collision_shapes.size() == 1 &&
collision_shapes[0]->shape_type == Shape2DType::kDisk) {
particle_drawer_->AddNode(world_->GetNodeByIndex(i));
} else {
poly_drawer_->AddNode(world_->GetNodeByIndex(i));
}
path_drawer_->AddNode(world_->GetNodeByIndex(i));
}
text_drawer_ = make_unique<TextDrawer>(font_);
// Use the fixed camera for background drawing.
canvas_drawer_ = make_unique<CanvasDrawer>(LoadBackgroundGLProgram(),
cameras_[1].get(), Vector2f(w, h));
return true;
}
示例15: engine_init
int
engine_init(void)
{
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS | SDL_INIT_AUDIO)) {
DBG_LOG("Failed to initialize SDL: %s", SDL_GetError());
return -1;
}
SDL_LogSetAllPriority(SDL_LOG_PRIORITY_WARN);
fullscreen = false;
/* require OpenGL 3.2 */
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
// SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
main_win = SDL_CreateWindow(RPG_WINDOW_TITLE,
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
RPG_OUT_WIDTH, RPG_OUT_HEIGHT, SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL);
if (!main_win) {
DBG_LOG("Failed to create window: %s", SDL_GetError());
goto failure;
}
main_ctx = SDL_GL_CreateContext(main_win);
if (!main_ctx) {
DBG_LOG("Failed to initialize GL context: %s", SDL_GetError());
goto failure;
}
SDL_GL_MakeCurrent(main_win, main_ctx);
/* now that context is created we can initialize GL */
int res = gl3wInit();
if (res != GL3W_OK) {
DBG_LOG("Error opening GL library (%d)", res);
goto failure;
}
if (!gl3wIsSupported(3, 2)) {
DBG_LOG("Error OpenGL 3.2 or later required");
goto failure;
}
/* register published key events */
engine_key_state.left = keystate_register("left");
engine_key_state.right = keystate_register("right");
engine_key_state.up = keystate_register("up");
engine_key_state.down = keystate_register("down");
engine_key_state.button_a = keystate_register("a");
engine_key_state.button_b = keystate_register("b");
engine_key_state.button_x = keystate_register("x");
engine_key_state.button_y = keystate_register("y");
engine_key_state.start = keystate_register("start");
engine_key_state.select = keystate_register("select");
return 0;
failure:
engine_fini();
return -1;
}