this is a simple car crash/race game , and till now i have only drawn the street and tiles and a car and for some reason it’s taking alot of gpu almost 70% on rx 5700xt and even sometime not running properly on other computers, here is the code , i am fairly new to opengl so , i would really appreciate it if someone helped
#include <gl/gl.h>
#include <gl/glut.h>
#include <gl/wglext.h>
#include <gl/gl.h>
#include <ctime>
#include <cstdio>
#include<synchapi.h>
#include<iostream>
#include<sstream>
#include<glfw3.h>
#include<tchar.h >
using namespace std;
#pragma comment(lib, "opengl32.lib")
#pragma comment(lib, "glu32.lib")
#pragma comment(lib, "glut32.lib")
void InitGraphics(int argc, char* argv[]);
void SetTransformations();
void OnDisplay();
float tile1_movement = -140;
float tile2_movement = -40;
float tile3_movement = 80;
float tile4_movement = 160;
float tile5_movement = 260;
float angle = 0.0;
float directionx=0;
float directiony = 0;
void OnKeyPress(unsigned char key, int x, int y) {
switch (key) {
case 'a':// a key
case 'A':
if (directionx - 3.8 > -55) {
directionx -= 3.8;
cout << "left" << directionx << endl;
break;
}
break;
case 'd':// d key
case 'D':
if (directionx + 3.8 < 55) {
directionx += 3.8;
cout << "right" << directionx << endl;
break;
}
break;
case 'w':// d key
case 'W':
if (directiony + 3.8 < 250) {
directiony += 3.8;
cout << "up" << directiony << endl;;
break;
}
cout << directiony;
break;
case 's':
case 'S':
if (directiony - 3.8 > -2) {
directiony -= 3.8;
cout << "down" << directiony << endl;;
break;
}
break;
}
}
int main(int argc, char* argv[]) {
InitGraphics(argc, argv);
return 0;
glfwSwapInterval(1);
}
void OnKeyPress(unsigned char key, int x, int y);
void InitGraphics(int argc, char* argv[]) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA);
glutInitWindowPosition(100, 100);
glutInitWindowSize(1020, 720);
glutCreateWindow("Bouncing car");
glutDisplayFunc(OnDisplay);
glutKeyboardFunc(OnKeyPress);
glutMainLoop();
}
void SetTransformations() {
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-200, 200, -200, 200);
}
void loop() {
}
void OnDisplay() {
glClearColor(0, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT);
SetTransformations();
glBegin(GL_LINE_STRIP);
glColor3f(1, 1, 1);
glVertex2d(0, -200);
glVertex2d(0, 200);
glEnd();
glBegin(GL_LINE_STRIP);
glVertex2d(200, 0);
glVertex2d(-200, 0);
glEnd();
//--------------------------------------------------------------
// STREET
//--------------------------------------------------------------
glBegin(GL_QUADS);
glColor4f(0.1, 0.1, 0.1, 0.60);
glVertex2i(-75, 200);
glVertex2i(75, 200);
glVertex2i(75, -200);
glVertex2i(-75, -200);
glEnd();
// TILES
// -------------------------------------------------------------
//first row tile 1
//--------------------------------------------------------------
glPushMatrix();
glTranslatef(0, tile1_movement, 0);
glBegin(GL_QUADS);
glColor3f(1, 1, 1);
glVertex2i(-25, -200);
glVertex2i(-30, -200);
glVertex2i(-30, -120);
glVertex2i(-25, -120);
glEnd();
tile1_movement -= 0.1;
if (tile1_movement <= -280) {
glLoadIdentity();
tile1_movement = 400;
glTranslatef(0, tile1_movement, 0);
}
glPopMatrix();
// --------------------------------------------------------------
//first row tile 2
//--------------------------------------------------------------
glPushMatrix();
glTranslatef(0, tile2_movement, 0);
glBegin(GL_QUADS);
glColor3f(1, 1, 1);//cyan
glVertex2i(-25, -100);
glVertex2i(-30, -100);
glVertex2i(-30, -20);
glVertex2i(-25, -20);
glEnd();
tile2_movement -= 0.1;
if (tile2_movement <= -280) {
glLoadIdentity();
tile2_movement = 400;
glTranslatef(0, tile2_movement, 0);
}
glPopMatrix();
// --------------------------------------------------------------
//first row tile 3
//--------------------------------------------------------------
glPushMatrix();
glTranslatef(0, tile3_movement, 0);
glBegin(GL_QUADS);
glColor3f(1, 1, 1);//cyan
glVertex2i(-25, 0);
glVertex2i(-30, 0);
glVertex2i(-30, 80);
glVertex2i(-25, 80);
glEnd();
tile3_movement -= 0.1;
if (tile3_movement <= -280) {
glLoadIdentity();
tile3_movement = 400;
glTranslatef(0, tile3_movement, 0);
}
glPopMatrix();
// --------------------------------------------------------------
//second row tile 1
//--------------------------------------------------------------
glPushMatrix();
glTranslatef(0, tile1_movement, 0);
glBegin(GL_QUADS);
glColor3f(1, 1, 1);
glVertex2i(25, -200);
glVertex2i(30, -200);
glVertex2i(30, -120);
glVertex2i(25, -120);
glEnd();
tile1_movement -= 0.1;
if (tile1_movement <= -280) {
glLoadIdentity();
tile1_movement = 400;
glTranslatef(0, tile1_movement, 0);
}
// --------------------------------------------------------------
//second row tile 2
//--------------------------------------------------------------
glPopMatrix();
glPushMatrix();
glTranslatef(0, tile2_movement, 0);
glBegin(GL_QUADS);
glColor3f(1, 1, 1);//cyan
glVertex2i(25, -100);
glVertex2i(30, -100);
glVertex2i(30, -20);
glVertex2i(25, -20);
glEnd();
tile2_movement -= 0.1;
if (tile2_movement <= -280) {
glLoadIdentity();
tile2_movement = 400;
glTranslatef(0, tile2_movement, 0);
}
glPopMatrix();
// --------------------------------------------------------------
//second row tile 3
//--------------------------------------------------------------
glPushMatrix();
glTranslatef(0, tile3_movement, 0);
glBegin(GL_QUADS);
glColor3f(1, 1, 1);//cyan
glVertex2i(25, 0);
glVertex2i(30, 0);
glVertex2i(30, 80);
glVertex2i(25, 80);
glEnd();
tile3_movement -= 0.1;
if (tile3_movement <= -280) {
glLoadIdentity();
tile3_movement = 400;
glTranslatef(0, tile3_movement, 0);
}
glPopMatrix();
//car drawing
glTranslatef(directionx, directiony, 0);
//glScalef(1, 1, 1);
glPushMatrix();
glTranslatef(0, -195, 0);
glScalef(0.18, 0.5, 0);
glRotatef(90, 0, 0, 1);
glLineWidth(1);
//car
glBegin(GL_POLYGON);
//main body quad
glColor3f(0.7, 0, 0);
glVertex2i(10, -85);
glVertex2i(10, 85);
glVertex2i(100, 85);
glVertex2i(100, -85);
glEnd();
glBegin(GL_POLYGON);
//front bumber
glVertex2i(100, 85);
glVertex2i(110, 75);
glVertex2i(115, 65);
glVertex2i(118, 55);
glVertex2i(120, 0);
glVertex2i(118, -55);
glVertex2i(115, -65);
glVertex2i(110, -75);
glVertex2i(100, -85);
glEnd();
glBegin(GL_LINE_STRIP);
//fornt bumper lining
glColor3f(0, 0, 0);
glVertex2i(90, 75);
glVertex2i(95, 70);
glVertex2i(105, 50);
glVertex2i(107, 0);
glVertex2i(105, -50);
glVertex2i(95, -70);
glVertex2i(90, -75);
glEnd();
//rear bumper
glBegin(GL_POLYGON);
glColor3f(0.7, 0, 0);
glVertex2i(10, 85);
glVertex2i(8, 75);
glVertex2i(3, 65);
glVertex2i(1, 55);
glVertex2i(-1, 0);
glVertex2i(1, -55);
glVertex2i(3, -65);
glVertex2i(8, -80);
glVertex2i(10, -85);
glEnd();
//rear bumper lining
glBegin(GL_LINE_STRIP);
glColor3f(0, 0, 0);
glVertex2i(14, 80);
glVertex2i(12, 75);
glVertex2i(7, 65);
glVertex2i(5, 55);
glVertex2i(3, 0);
glVertex2i(5, -55);
glVertex2i(7, -65);
glVertex2i(12, -75);
glVertex2i(14, -80);
glEnd();
//roof of the car
glBegin(GL_QUADS);
glColor3f(0.7, 0, 0);
glVertex2i(35, 40);
glVertex2i(55, 40);
glVertex2i(55, -40);
glVertex2i(35, -40);
//rear window of the car
glColor3f(0, 0.5, 0.5);
glVertex2i(35, 30);
glVertex2i(25, 30);
glVertex2i(25, -30);
glVertex2i(35, -30);
//tires of the car
glColor3f(0.5, 0.5, 0.5);
glVertex2i(20, 85);
glVertex2i(20, 90);
glVertex2i(40, 90);
glVertex2i(40, 85);
glColor3f(0.5, 0.5, 0.5);
glVertex2i(20, -85);
glVertex2i(20, -90);
glVertex2i(40, -90);
glVertex2i(40, -85);
glColor3f(0.5, 0.5, 0.5);
glVertex2i(80, 85);
glVertex2i(80, 90);
glVertex2i(100, 90);
glVertex2i(100, 85);
glColor3f(0.5, 0.5, 0.5);
glVertex2i(80, -85);
glVertex2i(80, -90);
glVertex2i(100, -90);
glVertex2i(100, -85);
glEnd();
glBegin(GL_LINE_LOOP);
// rear window lining
glColor3f(0, 0, 0);
glVertex2i(35, 30);
glVertex2i(25, 30);
glVertex2i(25, -30);
glVertex2i(35, -30);
glEnd();
glBegin(GL_LINE_LOOP);
//roof lining
glColor3f(0, 0, 0);
glVertex2i(35, 40);
glVertex2i(55, 40);
glVertex2i(55, -40);
glVertex2i(35, -40);
glEnd();
glBegin(GL_POLYGON);
//side window 1
glColor3f(0, 0.5, 0.5);
glVertex2i(40, 40);
glVertex2i(35, 65);
glVertex2i(60, 65);
glVertex2i(50, 40);
glEnd();
glBegin(GL_LINE_LOOP);
//side window 1
glColor3f(0, 0, 0);
glVertex2i(40, 40);
glVertex2i(35, 65);
glVertex2i(60, 65);
glVertex2i(50, 40);
glEnd();
glBegin(GL_POLYGON);
//side window 2
glColor3f(0, 0.5, 0.5);
glVertex2i(40, -40);
glVertex2i(35, -65);
glVertex2i(60, -65);
glVertex2i(50, -40);
glEnd();
glBegin(GL_LINE_LOOP);
//side window 2 lining
glColor3f(0, 0, 0);
glVertex2i(40, -40);
glVertex2i(35, -65);
glVertex2i(60, -65);
glVertex2i(50, -40);
glEnd();
glBegin(GL_POLYGON);
//front window
glColor3f(0, 0.5, 0.5);
glVertex2i(55, 40);
glVertex2i(70, 55);
glVertex2i(74, 35);
glVertex2i(77, 20);
glVertex2i(79, 0);
glVertex2i(77, -20);
glVertex2i(74, -35);
glVertex2i(70, -55);
glVertex2i(55, -40);
glEnd();
glBegin(GL_LINE_LOOP);
//front window lining
glColor3f(0, 0, 0);
glVertex2i(55, 40);
glVertex2i(70, 55);
glVertex2i(74, 35);
glVertex2i(77, 20);
glVertex2i(79, 0);
glVertex2i(77, -20);
glVertex2i(74, -35);
glVertex2i(70, -55);
glVertex2i(55, -40);
glEnd();
glBegin(GL_POLYGON);
glColor3f(1, 1, 0);
glVertex2i(96, 70);
glVertex2i(102, 75);
glVertex2i(106, 70);
glVertex2i(106, 55);
glVertex2i(106, 50);
glEnd();
glBegin(GL_POLYGON);
glColor3f(1, 1, 0);
glVertex2i(96, -70);
glVertex2i(102, -75);
glVertex2i(106, -70);
glVertex2i(106, -55);
glVertex2i(106, -50);
glEnd();
glBegin(GL_LINE_LOOP);
glColor3f(0, 0, 0);
glVertex2i(96, 70);
glVertex2i(102, 75);
glVertex2i(106, 70);
glVertex2i(106, 55);
glVertex2i(106, 50);
glEnd();
glBegin(GL_LINE_LOOP);
glColor3f(0, 0, 0);
glVertex2i(96, -70);
glVertex2i(102, -75);
glVertex2i(106, -70);
glVertex2i(106, -55);
glVertex2i(106, -50);
glEnd();
glBegin(GL_TRIANGLES);
glVertex2i(109, 5);
glVertex2i(111, 0);
glVertex2i(109, -5);
glEnd();
glBegin(GL_LINES);
glVertex2i(77, 35);
glVertex2i(95, 55);
glVertex2i(77, -35);
glVertex2i(95, -55);
glVertex2i(22, 35);
glVertex2i(12, 55);
glVertex2i(22, -35);
glVertex2i(12, -55);
glEnd();
glPopMatrix();
glFlush();
glutSwapBuffers();
glutPostRedisplay();
}