Graphics in C++ [closed]
Want to improve this question? Reformulate the question so that it can be answered based on facts and quotations.
Closed 5 years ago.
Improve the questionThere is a task: to write a program with a graphical interface. Use C++ in conjunction with OpenGL or DirectX.
Tell me where you can get information on this topic.
5 answers
According to the C++/DirectX bundle, a high - quality source of information is MSDN.
DirectX Tutorial is a good resource.
Besides:
- There are code examples on CodeProject.
- On GameDev there are examples on OpenGL.
- A lot of useful information on gamedev.ru.
Http://www.opengl.org/ The official website, there just needs to be a LOT of information, of course in English). It seems that there were lessons at NeHe, but I did not succeed with them. Russian-language resources are game dev.
There is also a certain unity
. There are opengl
for c#
, and much more. Suddenly useful:)
To begin with, we recommend using opengl, as it is simpler and more compact.
Here is a full working example of the simplest application. The SDL library is used. For opengl, this is the same as STL for C++. Provides a convenient event handler, working with sound, devices.
Reference to SDL.
Draws a red square on a black background.
#define МАКСИМУМ_КНОПОК 423
bool нажатые_кнопки[МАКСИМУМ_КНОПОК];
#include <iostream>
#include <string>
#include <cstdlib>
#include <sdl/include/SDL.h>
#include <sdl/include/SDL_mixer.h>
#include <sdl/include/SDL_syswm.h>
#undef main
#include <GL/glew.h>
#include <GL/glut.h>
#pragma comment (lib , "sdl.lib")
int ширина_экрана = 1920;
int высота_экрана = 1080;
bool приложение_выполняется = true;
int OnEvent(SDL_Event* Event)
{
if(Event->type == SDL_KEYDOWN)
{
std::cout << "\nsym=" << (int)Event->key.keysym.sym;
нажатые_кнопки[Event->key.keysym.sym] = true; }
if(Event->type == SDL_KEYUP){нажатые_кнопки[Event->key.keysym.sym] = false; }
return true;
}
// события
SDL_Event Event;
inline void events(){while(SDL_PollEvent(&Event)){OnEvent(&Event);}}
void base_draw()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor4f(1,1,1,1);// важно
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, ширина_экрана, 0.0, высота_экрана, -1550000, 1500000);
glEnable(GL_DEPTH_TEST);
}
int глубина_цвета = 32;
int _tmain(int argc, _TCHAR* argv[])
{
std::system("chcp 1251");
SDL_Surface * Surf_Display = NULL;
if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) {return 0;}
SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, 0);
glMatrixMode(GL_PROJECTION);
glEnable(GL_TEXTURE_2D);
glLoadIdentity();
unsigned int flags = SDL_OPENGL | SDL_RESIZABLE;
if((Surf_Display = SDL_SetVideoMode(ширина_экрана, высота_экрана, глубина_цвета, flags)) == NULL){return 0;}
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // цвет экрана при очищении
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor4f(1,1,1,1);// важно
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glEnable(GL_DEPTH_TEST);
while(приложение_выполняется==true)
{
events();
base_draw();
glColor4ub(255, 0, 0, 255);
glRectd(500 , 500 , 1000 , 1000);
SDL_GL_SwapBuffers();
};
std::system("pause");
return 0;
}