[英]Resize the window while the loop is running with SDL2 and OpenGL in C++?
This is my current code : 这是我当前的代码:
All the include 所有包含
#define GLEW_STATIC
#include <stdlib.h>
#include <SDL2/SDL.h>
#include <math.h>
#include <stdio.h>
#include <iostream>
#include<conio.h>
#include<dos.h>
#include <GL/glew.h>
#include "SceneOpenGL.h"
#include <SDL2/SDL_image.h>
using namespace std;
#ifdef WIN32
#include <GL/glew.h>
#else
#define GL3_PROTOTYPES 1
#include <GL3/gl3.h>
#endif
#include <SDL2/SDL.h>
#include <iostream>
Voiding my function 违反我的职责
void DrawCircle(float cx, float cy, float r, int num_segments);
void DrawEllipse(float cx, float cy, float a, float b, int num_segments);
void Rotation(float a,float b,float r, float g_theta );
The main function 主要功能
int main(int argc, char **argv)
{
SDL_Window* fenetre(0);
SDL_GLContext contexteOpenGL(0);
SDL_Event evenements;
bool terminer(false);
if(SDL_Init(SDL_INIT_VIDEO) < 0)
{
std::cout << "Erreur lors de l'initialisation de la SDL : " << SDL_GetError() << std::endl;
SDL_Quit();
return -1;
}
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
fenetre = SDL_CreateWindow("Test SDL 2.0", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 600, 600, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
if(fenetre == 0)
{
std::cout << "Erreur lors de la creation de la fenetre : " << SDL_GetError() << std::endl;
SDL_Quit();
return -1;
}
contexteOpenGL = SDL_GL_CreateContext(fenetre);
if(contexteOpenGL == 0)
{
std::cout << SDL_GetError() << std::endl;
SDL_DestroyWindow(fenetre);
SDL_Quit();
return -1;
}
#ifdef WIN32
GLenum initialisationGLEW( glewInit() );
if(initialisationGLEW != GLEW_OK)
{
std::cout << "Erreur d'initialisation de GLEW : " << glewGetErrorString(initialisationGLEW) << std::endl;
SDL_GL_DeleteContext(contexteOpenGL);
SDL_DestroyWindow(fenetre);
SDL_Quit();
return -1;
}
#endif
Main loop with the display of the circle and the ellipse 主循环显示圆形和椭圆形
while(!terminer)
{
SDL_WaitEvent(&evenements);
if(evenements.window.event == SDL_WINDOWEVENT_CLOSE)
terminer = true;
glClear(GL_COLOR_BUFFER_BIT);
float g_theta = 0.0f;
while (g_theta<360)
{
glClear(GL_COLOR_BUFFER_BIT);
g_theta += 1.0f;
DrawCircle(0, 0, 0.3, 50);
Rotation(0.8,0.65,0.1,g_theta);
Rotation(0.5,0.5,0.2,g_theta);
Rotation(0.1,0.1,0.01,g_theta);
glDisableVertexAttribArray(0);
SDL_GL_SwapWindow(fenetre);
}
}
SDL_GL_DeleteContext(contexteOpenGL);
SDL_DestroyWindow(fenetre);
SDL_Quit();
return 0;
}
Function creating the circle 创建圆的功能
void DrawCircle(float cx, float cy, float r, int num_segments)
{
glBegin(GL_LINE_LOOP);
for(int ii = 0; ii < num_segments; ii++)
{
float theta = 2.0 * M_PI * float(ii) / float(num_segments);
float x_c = r * cosf(theta);//calculate the x component
float y_c = r * sinf(theta);//calculate the y component
glColor3f(1.0f,0.0f,0.0f);
glVertex2f(x_c + cx, y_c + cy);//output vertex
}
glEnd();
}
Function creating the ellipse 创建椭圆的功能
void DrawEllipse(float cx, float cy, float a, float b, int num_segments)
{
glBegin(GL_LINE_LOOP);
for(int ii = 0; ii < num_segments; ii++)
{
float theta = 2.0 * M_PI * float(ii) / float(num_segments);
float x_e = a * cosf(theta);//calculate the x component
float y_e = b * sinf(theta);//calculate the y component
glColor3f(0.0f,0.0f,1.0f);
glVertex2f(x_e + cx, y_e + cy);//output vertex
}
glEnd();
}
Function that links the circle and the ellispe 链接圆和椭圆的功能
void Rotation(float a,float b,float r, float g_theta )
{
float x = a * cosf(g_theta * M_PI / 180.0f);
float y = b * sinf(g_theta * M_PI / 180.0f);
float d = sqrtf( x*x + y*y );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glPushMatrix();
glRotatef( g_theta, 0, 0, 1 ); // rotation around the z axis
glTranslatef( d, 0, 0 ); // translation by the distance
DrawCircle(0, 0, r, 50);
glPopMatrix();
DrawEllipse(0, 0, a, b, 50);
}
this code displays circles moving on ellipses. 此代码显示在椭圆上移动的圆。
I would like to resize the window while the circles move. 我想在圆圈移动时调整窗口大小。
I have already set the window as resizable: 我已经将窗口设置为可调整大小:
fenetre = SDL_CreateWindow("Test SDL 2.0", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 600, 600, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
But when I run the code, I can't resize. 但是当我运行代码时,无法调整大小。
How can I resize the window while the loop while(!terminer)
is running ? 在运行
while(!terminer)
循环while(!terminer)
如何调整窗口的大小?
The issue is, that you have 2 nested loops, while the outer loops has an event handling, the inner loop has it not. 问题是,您有2个嵌套循环,而外部循环具有事件处理,而内部循环则没有。
You current design is like this: 您当前的设计是这样的:
while not end
event handling
theta = 0
while (theta < 360)
theta ++;
draw geometry
This causes that all the window like resize are not handled for a long period of time in the inner loop. 这导致内部循环中的所有窗口(如调整大小)都无法长时间处理。
You have to change the design like this, to solve your issue: 您必须像这样更改设计以解决您的问题:
theta = 0
while not end
event handling
draw geometry
theta ++;
if theta > 360 then theta = 0
Further you have to suit the viewport rectangle to the window size, every time when the size of the window has changed. 此外,每次窗口大小更改时,都必须使视口矩形适合窗口大小。 This can be done by
glViewport
: 这可以通过
glViewport
完成:
Change the main loop of your program somehow like this: 以某种方式更改程序的主循环:
int vp_cx = 0;
int vp_cy = 0;
float g_theta = 0.0f;
while(!terminer)
{
SDL_Event event;
while ( SDL_PollEvent( &event ) );
{
switch (event.type)
{
case SDL_QUIT: terminer = true; break;
}
}
int cx, cy;
SDL_GetWindowSize( fenetre, &cx, &cy );
if ( vp_cx != cx || vp_cy != cy )
{
vp_cx = cx; vp_cy = cy;
glViewport( 0, 0, vp_cx, vp_cy );
}
glClear(GL_COLOR_BUFFER_BIT);
glClear(GL_COLOR_BUFFER_BIT);
DrawCircle(0, 0, 0.3, 50);
Rotation(0.8,0.65,0.1,g_theta);
Rotation(0.5,0.5,0.2,g_theta);
Rotation(0.1,0.1,0.01,g_theta);
glDisableVertexAttribArray(0);
SDL_GL_SwapWindow(fenetre);
g_theta += 1.0f;
if ( g_theta >= 360.0f-0.001f )
g_theta = 0.0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.