Last active
May 30, 2023 21:41
-
-
Save matheusmv/961dc1c6e805d6df5698ceb4bdb39f58 to your computer and use it in GitHub Desktop.
SDL + OpenGL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// gcc -Wall -Werror -Wextra -std=c11 orbiting-spheres-gl.c -lSDL2 -lGL -lglut -lm | |
#include <math.h> | |
#include <stdbool.h> | |
#include <SDL2/SDL.h> | |
#include <SDL2/SDL_opengl.h> | |
#include <GL/gl.h> | |
#include <GL/glut.h> | |
const int WINDOW_WIDTH = 800; | |
const int WINDOW_HEIGHT = 600; | |
void drawCircle(float centerX, float centerY, float radius, int numSegments) { | |
glBegin(GL_LINE_LOOP); | |
for (int i = 0; i < numSegments; i++) { | |
float theta = 2.0f * 3.1415926f * (float)i / (float)numSegments; | |
float x = radius * cos(theta); | |
float y = radius * sin(theta); | |
glVertex2f(centerX + x, centerY + y); | |
} | |
glEnd(); | |
} | |
int main(int argc, char** argv) { | |
SDL_Init(SDL_INIT_VIDEO); | |
SDL_Window* window = SDL_CreateWindow("Orbiting Spheres", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN); | |
SDL_GLContext context = SDL_GL_CreateContext(window); | |
glutInit(&argc, argv); | |
// Inicialização do OpenGL | |
glMatrixMode(GL_PROJECTION); | |
glLoadIdentity(); | |
glOrtho(0, WINDOW_WIDTH, WINDOW_HEIGHT, 0, -1, 1); | |
glMatrixMode(GL_MODELVIEW); | |
glLoadIdentity(); | |
// Definição das variáveis para controle da órbita | |
float orbitRadius = 200.0f; // Raio da órbita | |
float orbitSpeed = 0.01f; // Velocidade de rotação da esfera menor | |
float angle = 0.0f; // Ângulo de rotação | |
SDL_Event event; | |
// Loop principal do jogo | |
bool quit = false; | |
while (!quit) { | |
while (SDL_PollEvent(&event)) { | |
if (event.type == SDL_QUIT) { | |
quit = true; | |
} | |
} | |
// Atualizar a posição da esfera menor | |
angle += orbitSpeed; | |
float smallerSphereX = WINDOW_WIDTH / 2 + orbitRadius * cos(angle); | |
float smallerSphereY = WINDOW_HEIGHT / 2 + orbitRadius * sin(angle); | |
// Renderização OpenGL | |
glClear(GL_COLOR_BUFFER_BIT); | |
// Desenhar a esfera maior no centro | |
glColor3f(1.0f, 1.0f, 1.0f); | |
glPushMatrix(); | |
glTranslatef(WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2, 0.0f); | |
glutWireSphere(100.0f, 20, 20); | |
glPopMatrix(); | |
// Desenhar a esfera menor orbitando | |
glColor3f(1.0f, 0.0f, 0.0f); | |
glPushMatrix(); | |
glTranslatef(smallerSphereX, smallerSphereY, 0.0f); | |
glutWireSphere(50.0f, 10, 10); | |
glPopMatrix(); | |
// Atualizar a tela | |
SDL_GL_SwapWindow(window); | |
} | |
// Limpeza e finalização | |
SDL_GL_DeleteContext(context); | |
SDL_DestroyWindow(window); | |
SDL_Quit(); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// gcc -Wall -Werror -Wextra -std=c11 orbiting-spheres.c -lSDL2 -lm | |
#include <math.h> | |
#include <stdbool.h> | |
#include <SDL2/SDL.h> | |
const int WINDOW_WIDTH = 800; | |
const int WINDOW_HEIGHT = 600; | |
void drawCircle(SDL_Renderer* renderer, int centerX, int centerY, int radius) { | |
int x = 0; | |
int y = radius; | |
int d = 1 - radius; | |
while (y >= x) { | |
SDL_RenderDrawPoint(renderer, centerX + x, centerY + y); | |
SDL_RenderDrawPoint(renderer, centerX + y, centerY + x); | |
SDL_RenderDrawPoint(renderer, centerX - x, centerY + y); | |
SDL_RenderDrawPoint(renderer, centerX - y, centerY + x); | |
SDL_RenderDrawPoint(renderer, centerX + x, centerY - y); | |
SDL_RenderDrawPoint(renderer, centerX + y, centerY - x); | |
SDL_RenderDrawPoint(renderer, centerX - x, centerY - y); | |
SDL_RenderDrawPoint(renderer, centerX - y, centerY - x); | |
if (d < 0) { | |
d += 2 * x + 3; | |
} else { | |
d += 2 * (x - y) + 5; | |
y--; | |
} | |
x++; | |
} | |
} | |
int main(void) { | |
SDL_Init(SDL_INIT_VIDEO); | |
SDL_Window* window = SDL_CreateWindow("Orbiting Spheres", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_SHOWN); | |
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); | |
// Definição das variáveis para controle da órbita | |
float orbitRadius = 200.0f; // Raio da órbita | |
float orbitSpeed = 0.01f; // Velocidade de rotação da esfera menor | |
float angle = 0.0f; // Ângulo de rotação | |
// Eventos de entrada | |
SDL_Event event; | |
// Loop principal do jogo | |
bool quit = false; | |
while (!quit) { | |
while (SDL_PollEvent(&event)) { | |
if (event.type == SDL_QUIT) { | |
quit = true; | |
} | |
} | |
// Atualizar a posição da esfera menor | |
angle += orbitSpeed; | |
float smallerSphereX = WINDOW_WIDTH / 2 + orbitRadius * cos(angle); | |
float smallerSphereY = WINDOW_HEIGHT / 2 + orbitRadius * sin(angle); | |
// Renderização SDL | |
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); | |
SDL_RenderClear(renderer); | |
// Desenhar a esfera maior no centro | |
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); | |
drawCircle(renderer, WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2, 100); | |
// Desenhar a esfera menor orbitando | |
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255); | |
drawCircle(renderer, (int)smallerSphereX, (int)smallerSphereY, 50); | |
// Atualizar a tela | |
SDL_RenderPresent(renderer); | |
SDL_Delay(20); | |
} | |
// Limpeza e finalização | |
SDL_DestroyRenderer(renderer); | |
SDL_DestroyWindow(window); | |
SDL_Quit(); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// gcc -Wall -Werror -Wextra -std=c11 sdl-gl-test.c -lSDL2 -lGL | |
#include <stdbool.h> | |
#include <SDL2/SDL.h> | |
#include <SDL2/SDL_opengl.h> | |
#include <GL/gl.h> | |
int main(void) { | |
SDL_Init(SDL_INIT_VIDEO); | |
SDL_Window* window = SDL_CreateWindow("Meu Jogo", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 800, 600, SDL_WINDOW_OPENGL); | |
SDL_GLContext context = SDL_GL_CreateContext(window); | |
glViewport(0, 0, 800, 600); | |
glMatrixMode(GL_PROJECTION); | |
glLoadIdentity(); | |
glOrtho(0, 800, 0, 600, -1, 1); | |
glMatrixMode(GL_MODELVIEW); | |
glLoadIdentity(); | |
// Eventos de entrada | |
SDL_Event event; | |
// Loop principal do jogo | |
bool quit = false; | |
while (!quit) { | |
while (SDL_PollEvent(&event)) { | |
if (event.type == SDL_QUIT) { | |
quit = true; | |
} | |
} | |
// Limpar a tela | |
glClear(GL_COLOR_BUFFER_BIT); | |
// Desenhar um ponto vermelho | |
glColor3f(1.0f, 0.0f, 0.0f); | |
glBegin(GL_POINTS); | |
glVertex2i(400, 300); | |
glEnd(); | |
// Desenhar uma linha verde | |
glColor3f(0.0f, 1.0f, 0.0f); | |
glBegin(GL_LINES); | |
glVertex2i(100, 100); | |
glVertex2i(700, 500); | |
glEnd(); | |
// Desenhar um retângulo azul | |
glColor3f(0.0f, 0.0f, 1.0f); | |
glBegin(GL_LINE_LOOP); | |
glVertex2i(200, 200); | |
glVertex2i(200, 400); | |
glVertex2i(600, 400); | |
glVertex2i(600, 200); | |
glEnd(); | |
// Atualizar a tela | |
SDL_GL_SwapWindow(window); | |
} | |
// Limpeza e finalização | |
SDL_GL_DeleteContext(context); | |
SDL_DestroyWindow(window); | |
SDL_Quit(); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// gcc -Wall -Werror -Wextra -std=c11 sdl-test.c -lSDL2 | |
#include <stdbool.h> | |
#include <SDL2/SDL.h> | |
// desenha um círculo utilizando o algoritmo de Bresenham | |
void drawCircle(SDL_Renderer* renderer, int centerX, int centerY, int radius) { | |
int x = 0; // Coordenada x inicial | |
int y = radius; // Coordenada y inicial | |
int d = 1 - radius; // Valor inicial da variável de decisão | |
while (y >= x) { | |
// Desenha pontos nas oito posições simétricas do círculo | |
SDL_RenderDrawPoint(renderer, centerX + x, centerY + y); | |
SDL_RenderDrawPoint(renderer, centerX + y, centerY + x); | |
SDL_RenderDrawPoint(renderer, centerX - x, centerY + y); | |
SDL_RenderDrawPoint(renderer, centerX - y, centerY + x); | |
SDL_RenderDrawPoint(renderer, centerX + x, centerY - y); | |
SDL_RenderDrawPoint(renderer, centerX + y, centerY - x); | |
SDL_RenderDrawPoint(renderer, centerX - x, centerY - y); | |
SDL_RenderDrawPoint(renderer, centerX - y, centerY - x); | |
if (d < 0) { | |
// Atualiza o valor da variável de decisão e a coordenada x | |
d += 2 * x + 3; | |
} else { | |
// Atualiza o valor da variável de decisão, a coordenada x e a coordenada y | |
d += 2 * (x - y) + 5; | |
y--; | |
} | |
x++; | |
} | |
} | |
int main(void) { | |
// Inicialização do SDL | |
SDL_Init(SDL_INIT_VIDEO); | |
// Criação da janela | |
SDL_Window* window = SDL_CreateWindow( | |
"Meu Jogo", | |
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, | |
800, 600, | |
0 | |
); | |
// Criação do objeto de renderização | |
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, 0); | |
// Limpar a tela (pintar de preto) | |
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); | |
SDL_RenderClear(renderer); | |
// Desenhar um retângulo azul | |
SDL_Rect rect = {200, 200, 400, 200}; | |
SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255); | |
SDL_RenderDrawRect(renderer, &rect); | |
// Desenhar um circulo vermelho | |
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255); | |
drawCircle(renderer, 400, 300, 100); | |
// Desenhar uma linha verde | |
SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255); | |
SDL_RenderDrawLine(renderer, 100, 100, 700, 500); | |
// Eventos de entrada | |
SDL_Event event; | |
// Loop principal do jogo | |
bool quit = false; | |
while (!quit) { | |
while (SDL_PollEvent(&event)) { | |
if (event.type == SDL_QUIT) { | |
quit = true; | |
} | |
} | |
// Atualizar a tela | |
SDL_RenderPresent(renderer); | |
} | |
// Limpeza e finalização | |
SDL_DestroyRenderer(renderer); | |
SDL_DestroyWindow(window); | |
SDL_Quit(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment