Anonymous avatar Anonymous committed 66e818f

SCENE 3: control box with WASD keys or click with mouse button where box should move

Comments (0)

Files changed (6)

CommandLineC/main.c

 // Scenes
 #include "scene1.h" // box in corner, no moving
 #include "scene2.h" // draw text
+#include "scene3.h" // controlliong object with mouse or keys
 
 int scene = 0;
 
             glutMotionFunc(scene2_mouseDragHandler);
             glutKeyboardFunc(scene2_keyboardHandler);
             break;
+        case 3: // F3
+            printf("Entering SCENE 3\n");
+            glutDisplayFunc(scene3_display);
+            glutIdleFunc(scene3_idle);
+            glutMouseFunc(scene3_mouseEventHandler);
+            glutPassiveMotionFunc(scene3_mouseMoveHandler);
+            glutMotionFunc(scene3_mouseDragHandler);
+            glutKeyboardFunc(scene3_keyboardHandler);
+            break;
             
         default:
             printf("Back to DEFAULT SCENE\n");

CommandLineC/scene1.c

     detectColision();
     drawBox2(box2X, box2Y);
     
+    // Rectangle.
+    glColor3f(0.0, 1.0, 0.0);
+    glRectf(10, 10, 20, 20);
+    
+    // Point.
+    glColor3f(0.0, 1.0, 1.0);
+    glBegin(GL_POINTS);
+    glVertex2d(30, 30);
+    glEnd();
+    
+    // Line.
+    glColor3f(1.0, 1.0, 0.0);
+    glBegin(GL_LINES);
+    glVertex2d(40, 40);
+    glVertex2d(50, 50);
+    glEnd();
+    
     glPopMatrix();
     
     glutSwapBuffers();

CommandLineC/scene2.c

 
 #ifdef SCENE2
 
-void outputAsStroke(GLfloat x, GLfloat y, char *text)
+int mouseX = 0;
+
+void textAsStrokes(GLfloat x, GLfloat y, char *text)
 {
 	char *p;
     
 {
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     
-    
     glColor3f( 255.0f, 0.0f, 0.0f);
     
     // Output as stroke
     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
     glEnable(GL_BLEND);
     glEnable(GL_LINE_SMOOTH);
-    glLineWidth(50.0);
+    glLineWidth(10.0);
+    
+    textAsStrokes(40, 40, "This is antialiased.");
+    
+
+    char str[256];
+    sprintf(str,"X:%d",mouseX);
+    
+    glColor3f( 255.0f, 0.0f, 55.0f);
+    textAsStrokes(20, 240, str);
     
-    outputAsStroke(40, 40, "This is antialiased.");
     
     glutSwapBuffers();
 }
 
 void
 scene2_mouseMoveHandler(int x, int y) {
+    mouseX = x;
     glutPostRedisplay();
 }
 

CommandLineC/scene3.c

+#include "scene3.h"
+
+
+#ifdef SCENE3
+
+float heroX = 0.0f;
+float heroY = 0.0f;
+int heroWidth = 40;
+int heroHeight = 60;
+float heroMoveX = 300.0f;
+float heroMoveY = 300.0f;
+float heroSpeed = 250.0f;
+
+// number of milliseconds elapsed at the last frame
+int lastFrameTime3 = 0;
+
+void drawHero(float x, float y) {
+    
+    int windowWidth = glutGet(GLUT_WINDOW_WIDTH);
+    int windowHeight = glutGet(GLUT_WINDOW_HEIGHT);
+    
+    glBegin(GL_QUADS);
+    
+    if(heroX == heroMoveX && heroY == heroMoveY) {
+        glColor3f( 0.0f, 255.0f, 0.0f);
+        
+        if(heroX == 0 || heroY == 0 || heroX == windowWidth - heroWidth || heroY == windowHeight - heroHeight) {
+            glColor3f( 0.0f, 255.0f, 255.0f);
+        }
+    }
+    else {
+        fprintf(stdout, "X: %f \t MOVE TO: %f\n", heroX, heroMoveX);
+        glColor3f( 255.0f, 0.0f, 0.0f);
+    }
+    glVertex2f(x,   y);
+    glVertex2f(x + heroWidth,   y);
+    glVertex2f(x + heroWidth, y + heroHeight);
+    glVertex2f(x, y + heroHeight);
+    glEnd();
+}
+
+void scene3_display(void)
+{
+    
+    int windowWidth = glutGet(GLUT_WINDOW_WIDTH);
+    int windowHeight = glutGet(GLUT_WINDOW_HEIGHT);
+    
+    if (lastFrameTime3 == 0)
+    {
+        lastFrameTime3 = glutGet(GLUT_ELAPSED_TIME);
+    }
+    
+    int now = glutGet(GLUT_ELAPSED_TIME);
+    int elapsedMilliseconds = now - lastFrameTime3;
+    float elapsedTime = elapsedMilliseconds / 1000.0f;
+    lastFrameTime3 = now;
+    
+    //check if hero already here
+    if(heroX < heroMoveX) {
+        heroX += heroSpeed * elapsedTime;
+    }
+    else if(heroX > heroMoveX) {
+        heroX -= heroSpeed * elapsedTime;
+    }
+    
+    if(heroY < heroMoveY) {
+        heroY += heroSpeed * elapsedTime;
+    }
+    else if(heroY > heroMoveY) {
+        heroY -= heroSpeed * elapsedTime;
+    }
+    
+    if(heroX+heroWidth > windowWidth) {
+        heroX = windowWidth - heroWidth;
+        heroMoveX = heroX;
+    }
+    else if (heroX < 0) {
+        heroX = heroMoveX = 0;
+    }
+    
+    
+    if(heroY+heroHeight > windowHeight) {
+        heroY = windowHeight - heroHeight;
+        heroMoveY = heroY;
+    }
+    else if (heroY < 0) {
+        heroY = heroMoveY = 0;
+    }
+    
+    
+    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+    
+    drawHero( heroX, heroY );
+    
+    glutSwapBuffers();
+}
+
+void scene3_reshape(int width, int height)
+{
+    glViewport(0, 0, width, height);
+    
+    glMatrixMode(GL_PROJECTION);
+    glLoadIdentity();
+    gluOrtho2D(0, width, 0, height);
+    glMatrixMode(GL_MODELVIEW);
+}
+
+void scene3_idle(void)
+{
+    glutPostRedisplay();
+}
+
+void
+scene3_mouseEventHandler(int button, int state, int x, int y) {
+    
+    int windowHeight = glutGet(GLUT_WINDOW_HEIGHT);
+    heroMoveX = x - heroWidth/2;
+    heroMoveY = windowHeight - y - heroHeight/2;
+    
+    glutPostRedisplay();
+}
+
+void
+scene3_mouseMoveHandler(int x, int y) {
+    glutPostRedisplay();
+}
+
+void
+scene3_mouseDragHandler(int x, int y) {
+    glutPostRedisplay();
+}
+
+void
+scene3_keyboardHandler(unsigned char key, int x, int y) {
+    int mod = 0;
+    int isAlt = 0;
+    int isShift = 0;
+    int isCtrl = 0;
+    
+    mod = glutGetModifiers();
+    isAlt = (mod & GLUT_ACTIVE_ALT) != 0;
+    isCtrl = (mod & GLUT_ACTIVE_CTRL) != 0;
+    isShift = (mod & GLUT_ACTIVE_SHIFT) != 0;
+    
+    
+    fprintf(stdout, "[SCENE 3] Key: %u, (%d, %d, %d)\n", key, isCtrl, isAlt, isShift);
+    
+    switch (key) {
+        case 'w':
+            heroMoveY += 10;
+            break;
+        case 'd':
+            heroMoveX += 10;
+            break;
+        case 's':
+            heroMoveY -= 10;
+            break;
+        case 'a':
+            heroMoveX -= 10;
+            break;
+            
+        default:
+            break;
+    }
+    
+}
+
+#endif

CommandLineC/scene3.h

+//
+//  scene2.h
+//  OpenGL_no_cocoa
+//
+//  Created by Mareks Ruskuls on 1/12/13.
+//
+//
+
+#ifndef SCENE3
+#define SCENE3
+
+#include "requirements.h"
+
+void scene3_display(void);
+void scene3_reshape(int width, int height);
+void scene3_idle(void);
+void scene3_mouseEventHandler(int button, int state, int x, int y);
+void scene3_mouseMoveHandler(int x, int y);
+void scene3_mouseDragHandler(int x, int y);
+void scene3_keyboardHandler(unsigned char key, int x, int y);
+void drawHero(float x, float y);
+
+
+
+
+#endif

OpenGL_no_cocoa.xcodeproj/project.pbxproj

 	objects = {
 
 /* Begin PBXBuildFile section */
+		2722CCD016A1BC7B00EB4026 /* scene3.c in Sources */ = {isa = PBXBuildFile; fileRef = 2722CCCF16A1BC7B00EB4026 /* scene3.c */; };
 		276FD460169F5E7100711514 /* main.c in Sources */ = {isa = PBXBuildFile; fileRef = 276FD45F169F5E7100711514 /* main.c */; };
 		276FD462169F5E7100711514 /* CommandLineC.1 in CopyFiles */ = {isa = PBXBuildFile; fileRef = 276FD461169F5E7100711514 /* CommandLineC.1 */; };
 		276FD467169F5E9900711514 /* GLUT.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 276FD466169F5E9900711514 /* GLUT.framework */; };
 /* End PBXCopyFilesBuildPhase section */
 
 /* Begin PBXFileReference section */
+		2722CCCF16A1BC7B00EB4026 /* scene3.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = scene3.c; sourceTree = "<group>"; };
+		2722CCD116A1BC8600EB4026 /* scene3.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = scene3.h; sourceTree = "<group>"; };
 		276FD45B169F5E7100711514 /* CommandLineC */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = CommandLineC; sourceTree = BUILT_PRODUCTS_DIR; };
 		276FD45F169F5E7100711514 /* main.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = main.c; sourceTree = "<group>"; };
 		276FD461169F5E7100711514 /* CommandLineC.1 */ = {isa = PBXFileReference; lastKnownFileType = text.man; path = CommandLineC.1; sourceTree = "<group>"; };
 /* End PBXFrameworksBuildPhase section */
 
 /* Begin PBXGroup section */
+		2722CCCE16A1BC6E00EB4026 /* scene3 */ = {
+			isa = PBXGroup;
+			children = (
+				2722CCCF16A1BC7B00EB4026 /* scene3.c */,
+				2722CCD116A1BC8600EB4026 /* scene3.h */,
+			);
+			name = scene3;
+			sourceTree = "<group>";
+		};
 		276FD44E169F5C3800711514 = {
 			isa = PBXGroup;
 			children = (
 		276FD45E169F5E7100711514 /* CommandLineC */ = {
 			isa = PBXGroup;
 			children = (
+				2722CCCE16A1BC6E00EB4026 /* scene3 */,
 				27AB3F0E16A19DA10083B34F /* scene2 */,
 				276FD45F169F5E7100711514 /* main.c */,
 				276FD461169F5E7100711514 /* CommandLineC.1 */,
 				276FD460169F5E7100711514 /* main.c in Sources */,
 				276FD46B16A15B5D00711514 /* scene1.c in Sources */,
 				27AB3F1116A19DBB0083B34F /* scene2.c in Sources */,
+				2722CCD016A1BC7B00EB4026 /* scene3.c in Sources */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.