Skip to content

Commit

Permalink
Commit 2
Browse files Browse the repository at this point in the history
  • Loading branch information
Manan committed Dec 5, 2023
1 parent bb5e7d4 commit 15b280f
Show file tree
Hide file tree
Showing 48 changed files with 2,588 additions and 133 deletions.
13 changes: 13 additions & 0 deletions .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [],
"compilerPath": ""
}
],
"version": 4
}
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"C_Cpp.errorSquiggles": "disabled"
}
Binary file added Globe.zip
Binary file not shown.
Binary file added LogicLabs/InteractiveLogicCircuit
Binary file not shown.
149 changes: 149 additions & 0 deletions LogicLabs/InteractiveLogicCircuit.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
#include <GL/glut.h>
#include <cmath>
#include "ShapeRenderer.h"

class InteractiveLogicCircuit {
public:
InteractiveLogicCircuit() : windowWidth(400), windowHeight(400), dragging(0), rectX(0.0), rectY(-0.9), rectWidth(0.1), boxInSpace(false) {
instance = this;
}

void run(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(windowWidth, windowHeight);
glutCreateWindow("Interactive Logic Circuit");

glutDisplayFunc(displayCallback);
glutReshapeFunc(reshapeCallback);
glutMouseFunc(mouseCallback);
glutMotionFunc(motionCallback);

glutMainLoop();
}

private:
int windowWidth;
int windowHeight;
int dragging;
float rectX;
float rectY;
float rectWidth;
bool boxInSpace;

static InteractiveLogicCircuit* instance;

static void displayCallback() {
instance->display();
}

static void reshapeCallback(int width, int height) {
instance->reshape(width, height);
}

static void mouseCallback(int button, int state, int x, int y) {
instance->mouse(button, state, x, y);
}

static void motionCallback(int x, int y) {
instance->motion(x, y);
}

void display() {
glClear(GL_COLOR_BUFFER_BIT);

glColor3f(1.0, 1.0, 1.0);
glLineWidth(2.0);

// Draw the static line in the middle
glBegin(GL_LINES);
glVertex2f(-0.5, 0.0);
glVertex2f(-0.25, 0.0);
glEnd();
glBegin(GL_LINES);
glVertex2f(0.25, 0.0);
glVertex2f(0.5, 0.0);
glEnd();

float squareSize = 0.24;
glBegin(GL_LINE_LOOP);
glVertex2f(-squareSize, squareSize);
glVertex2f(squareSize, squareSize);
glVertex2f(squareSize, -squareSize);
glVertex2f(-squareSize, -squareSize);
glEnd();


// Set color for the circle
glColor3f(1.0, 1.0, 1.0);

// Draw the movable shape (scaled down)
glPushMatrix();
glTranslatef(rectX, rectY, 0.0f);
glScalef(0.5f, 0.5f, 0.5f); // Scale down the shape
ShapeRenderer::NotG();
glPopMatrix();

// Check if the shape is in a specific area to change its color
boxInSpace = rectX >= -0.25 && rectX <= 0.25 && rectY >= -0.1 && rectY <= 0.1;
if (boxInSpace) {
glColor3f(1.0, 1.0, 0.0); // Change color if in space
}

// // Draw the static circle
float circleRadius = 0.05;
glBegin(GL_TRIANGLE_FAN);
for (int i = 0; i < 360; i++) {
float angle = i * M_PI / 180;
float x = 0.7 + circleRadius * cos(angle);
float y = 0.0 + circleRadius * sin(angle);
glVertex2f(x, y);
}
glEnd();

glFlush();
}

void reshape(int width, int height) {
windowWidth = width;
windowHeight = height;

glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
}

void mouse(int button, int state, int x, int y) {
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
float mouseX = (float)x / windowWidth * 2.0 - 1.0;
float mouseY = 1.0 - (float)y / windowHeight * 2.0;

// Adjust hitbox for the scaled-down shape
float hitboxSize = rectWidth * 2.0f; // Scale down the hitbox size
if (mouseY >= rectY - hitboxSize && mouseY <= rectY + hitboxSize &&
mouseX >= rectX - hitboxSize && mouseX <= rectX + hitboxSize) {
dragging = 1;
}
} else if (button == GLUT_LEFT_BUTTON && state == GLUT_UP) {
dragging = 0;
}
}

void motion(int x, int y) {
if (dragging) {
rectX = (float)x / windowWidth * 2.0 - 1.0;
rectY = 1.0 - (float)y / windowHeight * 2.0;
glutPostRedisplay();
}
}
};

InteractiveLogicCircuit* InteractiveLogicCircuit::instance = nullptr;

int main(int argc, char** argv) {
InteractiveLogicCircuit app;
app.run(argc, argv);
return 0;
}
Binary file added LogicLabs/MainApp
Binary file not shown.
Binary file added LogicLabs/MainUi
Binary file not shown.
143 changes: 143 additions & 0 deletions LogicLabs/MainUi.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
#include <iostream>
#include <GL/glut.h>
#include <string>
#include <functional>
#include <vector>
#include "Tutorials.h"

// Forward declaration
void drawText(float x, float y, const std::string& text);

class Button {
public:
Button(float x1, float y1, float x2, float y2, const std::string& text, std::function<void()> callback)
: x1(x1), y1(y1), x2(x2), y2(y2), text(text), onClick(std::move(callback)) {}

void draw() const {
// Set the button color
glColor3f(colorR, colorG, colorB);
// Draw the button as a quadrilateral
glBegin(GL_QUADS);
glVertex2f(x1, y1);
glVertex2f(x2, y1);
glVertex2f(x2, y2);
glVertex2f(x1, y2);
glEnd();

// Draw the button text
glColor3f(0.0f, 0.0f, 0.0f); // Black color for text
drawText((x1 + x2) / 2, (y1 + y2) / 2, text);
}

bool isClicked(float mouseX, float mouseY) const {
return mouseX >= x1 && mouseX <= x2 && mouseY >= y1 && mouseY <= y2;
}

void performAction() const {
if (onClick) {
onClick();
}
}

private:
float x1, y1, x2, y2;
float colorR = 0.7f, colorG = 0.0f, colorB = 0.0f; // Default button color is red
std::string text;
std::function<void()> onClick;
};

class Application {
public:
Application() : playButton(-0.2f, 0.3f, 0.2f, 0.5f, "Play", [this]() { onPlayClicked(); }),
exitButton(-0.2f, 0.1f, 0.2f, 0.3f, "Exit", [this]() { onExitClicked(); }) {
mainWindow = glutCreateWindow("OpenGL Buttons Example");

// Set up the display and mouse callback functions
glutDisplayFunc(displayCallback);
glutMouseFunc(mouseCallback);
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);

// Assign the instance to the static member
instance = this;
}

void run() {
glutMainLoop();
}

static void displayCallback() {
instance->render();
}

static void mouseCallback(int button, int state, int x, int y) {
instance->mouseEvent(button, state, x, y);
}

private:
int mainWindow;
Button playButton;
Button exitButton;
static Application* instance;

void render() {
glClear(GL_COLOR_BUFFER_BIT);

if (!isPlayClicked) {
playButton.draw();
exitButton.draw();
}

glutSwapBuffers();
}

void mouseEvent(int button, int state, int x, int y) {
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
float mouseX = static_cast<float>(x - glutGet(GLUT_WINDOW_WIDTH) / 2) / (glutGet(GLUT_WINDOW_WIDTH) / 2);
float mouseY = static_cast<float>(glutGet(GLUT_WINDOW_HEIGHT) / 2 - y) / (glutGet(GLUT_WINDOW_HEIGHT) / 2);

if (playButton.isClicked(mouseX, mouseY)) {
playButton.performAction();
} else if (exitButton.isClicked(mouseX, mouseY)) {
exitButton.performAction();
}
}
}

void onPlayClicked() {
isPlayClicked = true;
std::cout << "Play clicked" << std::endl;

// Call the function to initialize and show tutorials
showTutorials();
}

void onExitClicked() {
std::exit(0); // Exit the application
}

bool isPlayClicked = false;
};

// Define static instance pointer
Application* Application::instance = nullptr;

// Utility function for drawing text
void drawText(float x, float y, const std::string& text) {
glRasterPos2f(x, y);
for (char c : text) {
glutBitmapCharacter(GLUT_BITMAP_9_BY_15, c);
}
}

int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(800, 600);

// Create the application instance
static Application app;

app.run(); // This will enter the GLUT event processing loop

return 0;
}
51 changes: 51 additions & 0 deletions LogicLabs/ShapeRenderer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// ShapeRenderer.cpp
#include "ShapeRenderer.h"
#include <GL/glut.h>
#include <cmath>

void ShapeRenderer::NotG() {
// Drawing the triangle
glPushMatrix();
glRotatef(-90.0f, 0.0f, 0.0f, 1.0f);
glBegin(GL_LINE_LOOP);
glVertex2f(-0.2f, -0.2f);
glVertex2f(0.2f, -0.2f);
glVertex2f(0.0f, 0.2f);
glEnd();
glPopMatrix();

// Drawing lines
glBegin(GL_LINES);
glVertex2f(-0.4f, 0.1f);
glVertex2f(-0.2f, 0.1f);
glVertex2f(-0.4f, -0.1f);
glVertex2f(-0.2f, -0.1f);
glEnd();

// Drawing another line
glBegin(GL_LINES);
glVertex2f(0.3f, 0.0f);
glVertex2f(0.4f, 0.0f);
glEnd();

// Drawing a circle
glBegin(GL_LINE_LOOP);
for (int i = 0; i < 360; ++i) {
float angle = static_cast<float>(i) * 3.14159265f / 180.0f;
float x = 0.05f * std::cos(angle);
float y = 0.05f * std::sin(angle);
glVertex2f(0.25f + x, y);
}
glEnd();

// Drawing a stippled rectangle
glLineStipple(1, 0x00FF);
glEnable(GL_LINE_STIPPLE);
glBegin(GL_LINE_LOOP);
glVertex2f(-0.4f, -0.3f);
glVertex2f(0.4f, -0.3f);
glVertex2f(0.4f, 0.3f);
glVertex2f(-0.4f, 0.3f);
glEnd();
glDisable(GL_LINE_STIPPLE);
}
10 changes: 10 additions & 0 deletions LogicLabs/ShapeRenderer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// ShapeRenderer.h
#ifndef SHAPERENDERER_H
#define SHAPERENDERER_H

class ShapeRenderer {
public:
static void NotG();
};

#endif // SHAPERENDERER_H
Binary file added LogicLabs/Tutorials
Binary file not shown.
Loading

0 comments on commit 15b280f

Please sign in to comment.