我尝试按照教程做一些基本的东西,如游戏循环和使用OpenGL绘图。在该教程中使用了SDL 1.2,但我使用的是2.0,所以我不得不用新版本替换一些旧函数。
不幸的是,我想绘制的线条没有出现。
我已经尝试过使用这些数字,以便该行不在窗口之外,但它不起作用。
#include "SDL.h"
#include "SDL_opengl.h"
#include <iostream>
int main(int argc, char* args[])
{
//initialize SDL
SDL_Init(SDL_INIT_EVERYTHING);
//Set openGL memory usage
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_Window *window;
window=SDL_CreateWindow("MyWindow",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
600,400,
SDL_WINDOW_OPENGL);
SDL_Renderer *renderer;
renderer = SDL_CreateRenderer(window,-1,SDL_RENDERER_ACCELERATED);
SDL_SetRenderDrawColor(renderer,255,255,255,255);//RGB/ALPHA
glShadeModel(GL_SMOOTH);
//2D rendering
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//Disable depth checking
glDisable(GL_DEPTH_TEST);
std::cout<<"OpenGL is running\n";
std::cout<<"Main loop has started\n";
//Handle GameLoop
bool isRunning=true;
//Handel events
SDL_Event event;
//main gameloop
while(isRunning)
{
//Events
while(SDL_PollEvent(&event))
{
if(event.type==SDL_QUIT)
isRunning=false;
//if esc button is released
if(event.type==SDL_KEYUP&&event.key.keysym.sym==SDLK_ESCAPE)
isRunning=false;
if(event.type==SDL_KEYUP&&event.key.keysym.sym==SDLK_r)
SDL_SetRenderDrawColor(renderer,255,0,0,255);
//Logic for certain events
}
//Logic
//Rendering
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);
glPushMatrix();
glOrtho(0,600,400,0,-1,1); //Set matrix
glBegin(GL_LINES);//start drawing
glColor4ub(255,0,0,255);
glVertex2i(0,0);
glVertex2i(600,400);
glEnd();// end drawing
glPopMatrix();
SDL_GL_SwapWindow(window);
}
SDL_Quit();
return 0;
}
答案 0 :(得分:1)
不要混合SDL_Renderer代码和OpenGL。 SDL2中没有规定(但是,可能曾经)用于重置渲染器GL状态(如果它使用GL后端),您使用原始GL代码进行了践踏。
答案 1 :(得分:0)
首先,不要混用OpenGL和SDL_Renderer。然后,在渲染可以工作之前,您实际上需要为窗口创建OpenGL上下文。在SDL_CreateWindow
电话之后使用此功能:
SDL_GLContext glcontext = SDL_GL_CreateContext(window);
最后你需要释放上下文:
SDL_GL_DeleteContext(glcontext);