This commit is contained in:
Zengtudor 2024-09-05 23:35:54 +08:00
parent 3bfce1e098
commit dbee9f57c6
2 changed files with 118 additions and 1 deletions

114
src/c1/circle.cpp Normal file
View File

@ -0,0 +1,114 @@
//circle.cpp
#include <cmath>
#include <cstdlib>
#include <format>
#include<glad/glad.h>
#include<GLFW/glfw3.h>
#include <chrono>
#include <iostream>
#include <numbers>
#include <ostream>
#include <stdexcept>
#include <thread>
const float POINT_SIZE = 32.0f;
const unsigned int F_PER_SECOND = 144;
const float PI = 3.141592653589793238462643383279502884L;
typedef unsigned int u8;
#ifdef NDEBUG
#define PRINT(v)
#else
#define PRINT(v){std::cout<<std::format("{} : {}\n",#v,(v));}
#endif
class BouncyBall{
public:
int bx=0,by=0,bsx=5,bsy=7;
BouncyBall(int x,int y,int speedX,int speedY):bx(x),by(y),bsx(speedX),bsy(speedY){}
BouncyBall* update(){
bx+=bsx,by+=bsy;
if(bx>=1000||bx<=-1000){
bsx=-bsx;
}
if(by>=1000||by<=-1000){
bsy=-bsy;
}
return this;
}
};
float angleToRad(float angle){
return angle/180*PI;
}
void render(){
glBegin(GL_TRIANGLES);
static int state = -1;
state=(state+1)%F_PER_SECOND;
const u8 anglePerCircle = 1;
static float circleSize = 0.1f;
if(state==0){
circleSize+=0.1f;
}
if(circleSize>1.0f){
circleSize=0.1f;
}
for (u8 i=0;i<u8(float(state)/120.0f*360.0f);i+=anglePerCircle) {
if(i==0){
glVertex3f(std::cos(angleToRad(i))*circleSize,std::sin(angleToRad(i))*circleSize,0.0f);
}else[[likely]]{
glVertex3f(std::cos(angleToRad(i))*circleSize,std::sin(angleToRad(i))*circleSize,0.0f);
glVertex3f(std::cos(angleToRad(i))*circleSize,std::sin(angleToRad(i))*circleSize,0.0f);
}
glVertex3f(0.0f,0.0f,0.0f);
}
glEnd();
}
int main(){
// 初始化GLFW
if(glfwInit()==0){
throw std::runtime_error("failed to init GLFW");
}
GLFWwindow *window = glfwCreateWindow(640,640,"Example",nullptr,nullptr);
if(window==nullptr){
glfwTerminate();
throw std::runtime_error("GLFW failed to create window");
}
glfwMakeContextCurrent(window);//创建上下文
if(gladLoadGL()==0){ //加载GL函数
glfwTerminate();
throw std::runtime_error("GLAD Load GL functions failed");
}
std::cout<<"OpenGL version:"<<glGetString(GL_VERSION)<<"\n";
glEnable(GL_POINT_SMOOTH);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
glPointSize(POINT_SIZE);
while(glfwWindowShouldClose(window)==false){
glClear(GL_COLOR_BUFFER_BIT);
render();
glfwSwapBuffers(window);
glfwPollEvents();
std::this_thread::sleep_for(std::chrono::milliseconds(1000/F_PER_SECOND));
}
return 0;
}
#ifdef _MSC_VER
#include<Windows.h>
int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
main();
return 0;
}
#endif

View File

@ -20,3 +20,6 @@ target("3Points")
target("circlePoints") target("circlePoints")
add_files("src/c1/circlePoints.cpp") add_files("src/c1/circlePoints.cpp")
target("circle")
add_files("src/c1/circle.cpp")