在C ++中使用SDL2和OpenGL运行循环时,调整窗口大小吗?

时间:2018-10-20 22:30:19

标签: c++ opengl sdl-2

这是我当前的代码:

所有包含

from kivy.app import App

from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.label import Label


class SimpleApp(App):
    def build(self):
        self.label = Label(text="You have found yourself in a dungeon,\nsomewhere is your escape path,\nwill you make it out, and if so, what with?")
        self.button = Button(text="Press Me", on_press=self.on_clicked, size_hint=(1.0, None))
        layout = BoxLayout(orientation="vertical")
        layout.add_widget(self.label)
        layout.add_widget(self.button)
        return layout

    def on_clicked(self, instance):
        self.label.text = "These are the basic controls-\nThe controls-\nA- approach enemy/ attack enemy\nC- Go to chest"


if __name__ == "__main__":
    SimpleApp().run()

无效我的功能

#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>

主要功能

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 );

带有圆形和椭圆形显示的主循环

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

创建圆的功能

 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;

}

创建椭圆的功能

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();
}

将圆与椭圆连接的功能

    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();
}

此代码显示在椭圆上移动的圆。

我想在圆圈移动时调整窗口大小。

我已经将窗口设置为可调整大小:

    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);

}

但是当我运行代码时,无法调整大小。

在循环fenetre = SDL_CreateWindow("Test SDL 2.0", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 600, 600, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE); 运行时,如何调整窗口的大小?

1 个答案:

答案 0 :(得分:0)

问题是,您有2个嵌套循环,而外部循环具有事件处理,而内部循环则没有。 您当前的设计是这样的:

while not end

    event handling

    theta = 0
    while (theta < 360)

        theta ++;
        draw geometry

这导致内部循环中的所有窗口(如调整大小)都没有长时间处理。

您必须像这样更改设计以解决您的问题:

theta = 0
while not end

    event handling

    draw geometry

    theta ++;
    if theta > 360 then theta = 0

此外,每次窗口大小更改时,都必须使视口矩形适合窗口大小。这可以通过glViewport完成:

以某种方式更改程序的主循环:

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;
}