This commit is contained in:
Zengtudor 2024-09-05 23:10:21 +08:00
parent c69c6eaada
commit 81578e5e8c
2 changed files with 105 additions and 1 deletions

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

@ -0,0 +1,101 @@
//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 = 120;
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_POINTS);
static int state = -1;
state=(state+1)%F_PER_SECOND;
const float anglePerCircle = 10.0f;
for (u8 i=0;i<u8(float(state)/120.0f*360.0f);i+=anglePerCircle) {
glVertex3f(std::cos(angleToRad(i)),std::sin(angleToRad(i)),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

@ -16,4 +16,7 @@ add_requires("glfw","glad","glm")
add_packages("glfw","glad","glm") add_packages("glfw","glad","glm")
target("3Points") target("3Points")
add_files("src/c1/threePoints.cpp") add_files("src/c1/threePoints.cpp")
target("circle")
add_files("src/c1/circle.cpp")