void LightMatrixManager::refreshDisplay() { clearDisplay(); clearReservationsMappedToPixels(); for (int i=0; i<NUM_COLUMNS * NUM_ROWS; i++) { if (_reservations[i].allocated) { _reservation[currentWeek][_reservations[i].day][_reservations[i].hour] = _reservations[i].color; } } for (int day = 0; day < NUM_COLUMNS; day++) { for (int hour = 0; hour < NUM_ROWS; hour++) { setPixel(day, hour, _reservation[currentWeek][day][hour]); } } displayCursor(); _pixels.Show(); }
/** @brief The game loop function * @param p_structCommon : Struct with all program informations */ void playGame(structProgramInfo* p_structCommon) { unsigned char l_cKey; char* l_sTopText; unsigned int l_iWatchdog; unsigned int l_iCursorX; unsigned int l_iCursorY; unsigned int l_iMovement; /* store the wanted move, if impossible this variable */ unsigned int l_iCurrentSocketIndex; /* allow the program to go back */ int l_iNewCoordinates; char l_bForceRedraw; l_bForceRedraw = TRUE; l_cKey = 0; l_iNewCoordinates = -1; l_iMovement = 0; l_iCursorX = 1; l_iCursorY = 1; p_structCommon->iLastXUsed = l_iCursorX; p_structCommon->iLastYUsed = l_iCursorY; l_iCurrentSocketIndex = 0; p_structCommon->iOffsetX = (p_structCommon->iCol / 2) - (p_structCommon->iSizeX / 2); p_structCommon->iOffsetY = (p_structCommon->iRow / 2) - (p_structCommon->iSizeY / 2); l_sTopText = (char*)malloc((p_structCommon->iCol + 1) * sizeof(char)); if(l_sTopText == NULL) exit(-ENOMEM); p_structCommon->iCurrentUserColor = enumRouge; /* Main user, or server always red. If multiplayer and client, it receive another color suring connection */ p_structCommon->cUserMove = 0; /* Init the game, screen stuff etc... */ gameInit(p_structCommon); do { snprintf( l_sTopText, p_structCommon->iCol, "Your turn to play [ ] | Connected [ ] | Current port [%d] | Nickname %s", p_structCommon->iTcpPort, p_structCommon->sUserName); if(p_structCommon->bMyTurnToPlay == TRUE) { l_sTopText[19] = 'X'; } else { l_sTopText[19] = ' '; } if(p_structCommon->bMutexInitialized == TRUE) { l_sTopText[35] = 'X'; } else { l_sTopText[35] = ' '; } if(p_structCommon->bMutexInitialized != TRUE) {pthread_mutex_lock(p_structCommon->pthreadMutex);} usleep(TIME_BETWEEN_TWO_REQUEST); initBar(); topText(l_sTopText); /* Display wursor each time */ displayCursor(l_iCursorX, l_iCursorY, p_structCommon->iOffsetX, p_structCommon->iOffsetY, FALSE, p_structCommon->cGrid); pointCounting(p_structCommon->cGrid, p_structCommon->iPoints, p_structCommon->iSizeX, p_structCommon->iSizeY); displayRanking(p_structCommon->iPoints, p_structCommon->iCol, p_structCommon->iRow); topText(l_sTopText); refresh(); usleep(TIME_BETWEEN_TWO_REQUEST); if(p_structCommon->bMutexInitialized != TRUE) {pthread_mutex_unlock(p_structCommon->pthreadMutex);} l_cKey = getch(); /* In order to allow an alone player to play alone */ if(p_structCommon->bMutexInitialized == FALSE) { p_structCommon->bMyTurnToPlay = TRUE; } switch(l_cKey) { case 'D': { /* LEFT */ l_iCursorX = (l_iCursorX < 1) ? p_structCommon->iSizeX - 1 : l_iCursorX - 1; p_structCommon->cUserMove = 'd'; l_iMovement = DIRECTION_LEFT; break; } case 'C': { /* RIGHT */ l_iCursorX = (l_iCursorX > p_structCommon->iSizeX - 2) ? 0 : l_iCursorX + 1; p_structCommon->cUserMove = 'c'; l_iMovement = DIRECTION_RIGHT; break; } case 'A': { /* UP */ l_iCursorY = (l_iCursorY < 1) ? p_structCommon->iSizeY - 1 : l_iCursorY - 1; p_structCommon->cUserMove = 'a'; l_iMovement = DIRECTION_UP; break; } case 'B': { /* DOWN */ l_iCursorY = (l_iCursorY > p_structCommon->iSizeY - 2) ? 0 : l_iCursorY + 1; p_structCommon->cUserMove = 'b'; l_iMovement = DIRECTION_DOWN; break; } case ':': { /* Command mode */ l_bForceRedraw = TRUE; logBar(p_structCommon, CLEAN_L2, ""); if(p_structCommon->bMutexInitialized != TRUE) {pthread_mutex_lock(p_structCommon->pthreadMutex);} logBar(p_structCommon, DISPLAY, ""); if(p_structCommon->bMutexInitialized != TRUE) {pthread_mutex_unlock(p_structCommon->pthreadMutex);} /* Get command from the user, the command set by the user will be saved in p_structCommon->sUserCommand */ userCommandGetter(p_structCommon); /* Analyse user command */ l_iNewCoordinates = userCommandExecute(p_structCommon); if(l_iNewCoordinates > - 1) { if(l_iNewCoordinates > - 1 && l_iNewCoordinates < (signed)p_structCommon->iSizeX) { /* belongs to the interval [0 ; p_structCommon->iSizeX[ ==> redefine X coordinate */ l_iCursorX = l_iNewCoordinates; l_bForceRedraw = FALSE; } else if(l_iNewCoordinates >= (signed)p_structCommon->iSizeX && l_iNewCoordinates < (signed)p_structCommon->iSizeX + (signed)p_structCommon->iSizeY) { /* belongs to the interval [p_structCommon->iSizeX ; p_structCommon->iSizeX + p_structCommon->iSizeY[ ==> redefine Y coordinate */ l_iCursorY = l_iNewCoordinates - (p_structCommon->iSizeX); l_bForceRedraw = FALSE; } } if(p_structCommon->bMutexInitialized != TRUE) {pthread_mutex_lock(p_structCommon->pthreadMutex);} displayCursor(l_iCursorX, l_iCursorY, p_structCommon->iOffsetX, p_structCommon->iOffsetY, l_bForceRedraw, p_structCommon->cGrid); refresh(); /* Clean the screen */ logBar(p_structCommon, DISPLAY, ""); if(p_structCommon->bMutexInitialized != TRUE) {pthread_mutex_unlock(p_structCommon->pthreadMutex);} break; } case 'q': case 'Q': { p_structCommon->bNetworkDisconnectionRequiered = TRUE; /* Started threads have to down this flag -- bMutexInitialized means we have at least one thread started */ while(p_structCommon->bNetworkDisconnectionRequiered == TRUE && p_structCommon->bMutexInitialized == TRUE) { usleep(TIME_BETWEEN_TWO_REQUEST); for(l_iCurrentSocketIndex = 0; l_iCurrentSocketIndex < MAX_CONNECTED_CLIENTS ; l_iCurrentSocketIndex++) { if(p_structCommon->iClientsSockets[l_iCurrentSocketIndex] != 0) { break; } } if(l_iCurrentSocketIndex >= MAX_CONNECTED_CLIENTS - 1) { break; } } break; /* Normally, network.c have clean the mutex at the end of all connexions */ if(p_structCommon->bMutexInitialized == TRUE) { pthread_mutex_destroy(p_structCommon->pthreadMutex); p_structCommon->bMutexInitialized = FALSE; } } case ' ': { if(p_structCommon->bMyTurnToPlay != TRUE) { break; } /* When the user drop a rock */ p_structCommon->cUserMove = 'r'; p_structCommon->iLastXUsed = l_iCursorX; p_structCommon->iLastYUsed = l_iCursorY; /* Put the color information in the matrix */ p_structCommon->cGrid[COLOR_MATRIX][l_iCursorY][l_iCursorX] = p_structCommon->iCurrentUserColor; /* Put the text information in the matrix */ p_structCommon->cGrid[TEXT_MATRIX][l_iCursorY][l_iCursorX] = ' '; /* declare this point to be synchronized with all clients */ p_structCommon->cGrid[SYNC_MATRIX][l_iCursorY][l_iCursorX] = POINT_TO_SYNC; /* Draw the block of the current user (the other blocks are draw by another function) */ if(p_structCommon->bMutexInitialized != TRUE) {pthread_mutex_lock(p_structCommon->pthreadMutex);} drawElement(l_iCursorX + p_structCommon->iOffsetX, l_iCursorY + p_structCommon->iOffsetY, p_structCommon->cGrid[TEXT_MATRIX][l_iCursorY][l_iCursorX], p_structCommon->iCurrentUserColor); if(p_structCommon->bMutexInitialized != TRUE) {pthread_mutex_unlock(p_structCommon->pthreadMutex);} /* Check neighborhood - If there is two contigous blocks of the player's color that means there is maybee a loop */ loopCompletion(l_iCursorX, l_iCursorY, p_structCommon->iCurrentUserColor, p_structCommon); /* Reset this player turn */ p_structCommon->bMyTurnToPlay = FALSE; break; } default: { /* Else, do nothing */ break; } } /* Check code, in order to forbid access to already reserved boxes */ if((unsigned int)p_structCommon->cGrid[COLOR_MATRIX][l_iCursorY][l_iCursorX] != enumNoir) { /* So there is something here and it is not me */ switch(l_iMovement) { case DIRECTION_UP: l_iCursorY++; break; case DIRECTION_DOWN: l_iCursorY--; break; case DIRECTION_LEFT: l_iCursorX++; break; case DIRECTION_RIGHT: l_iCursorX--; break; default: /* Who, error ! */ perror("Unknown code"); break; } /* If we go out of the screen by the upper or wester side put it in 0:0 and let the next block check if this block is empty or not */ if(l_iCursorY >= p_structCommon->iSizeY || l_iCursorX >= p_structCommon->iSizeX) { l_iCursorX = 0; l_iCursorY = 0; } /* If with bad luck you go over the grid */ l_iWatchdog = 0; while((unsigned int)p_structCommon->cGrid[COLOR_MATRIX][l_iCursorY][l_iCursorX] != enumNoir) { /* Find random coordinate with the simplest way */ l_iCursorX = rand() % p_structCommon->iSizeX; l_iCursorY = rand() % p_structCommon->iSizeY; l_iWatchdog++; if(l_iWatchdog > (p_structCommon->iSizeX * p_structCommon->iSizeY) / 2) { if(isTheGridFull(p_structCommon) == TRUE) { endOfTheGame(p_structCommon); return; } } } } }while((l_cKey != 'q') && (l_cKey != 'Q')); /* until q/Q pressed */ free(l_sTopText); }
void sludgeDisplay () { #if defined(HAVE_GLES2) #ifndef GL_DEPTH24_STENCIL8 #define GL_DEPTH24_STENCIL8 0x88F0 #endif // create an FBO static GLuint fbo = 0; static GLuint fbo_tex = 0; static GLuint fbo_rbo = 0; static float fbo_tex_w, fbo_tex_h; static GLuint fbo_shad, fbo_vert, fbo_frag; if(fbo==0) { // create FBO int width = 1; while (width<realWinWidth) width *= 2; int height = 1; while (height<realWinHeight) height *= 2; glGenFramebuffers(1, &fbo); glGenTextures(1, &fbo_tex); glGenRenderbuffers(1, &fbo_rbo); glBindTexture(GL_TEXTURE_2D, fbo_tex); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0); glBindTexture(GL_TEXTURE_2D, 0); glBindRenderbuffer(GL_RENDERBUFFER, fbo_rbo); glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, width, height); glBindRenderbuffer(GL_RENDERBUFFER, 0); glBindFramebuffer(GL_FRAMEBUFFER, fbo); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fbo_tex, 0); glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, fbo_rbo); glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, fbo_rbo); GLenum ret = glCheckFramebufferStatus(GL_FRAMEBUFFER); printf("Screen is %dx%d, FBO(%dx%d) Status = 0x%04X\n", realWinWidth, realWinHeight, width, height, ret); glBindFramebuffer(GL_FRAMEBUFFER, 0); glBindFramebuffer(GL_FRAMEBUFFER, fbo); fbo_tex_w = (float)realWinWidth / width; fbo_tex_h = (float)realWinHeight / height; // create shader for blitting the fbo... const char _blit_vsh[] = " \n\t" \ "attribute highp vec2 aPosition; \n\t" \ "attribute highp vec2 aTexCoord; \n\t" \ "varying mediump vec2 vTexCoord; \n\t" \ "void main(){ \n\t" \ "gl_Position = vec4(aPosition.x, aPosition.y, 0.0, 1.0);\n\t" \ "vTexCoord = aTexCoord; \n\t" \ "} \n\t"; const char _blit_fsh[] = " \n\t" \ "uniform sampler2D uTex; \n\t" \ "varying mediump vec2 vTexCoord; \n\t" \ "void main(){ \n\t" \ "gl_FragColor = texture2D(uTex, vTexCoord); \n\t" \ "} \n\t"; GLint success; fbo_frag = glCreateShader( GL_FRAGMENT_SHADER ); const char* src[1]; src[0] = _blit_fsh; glShaderSource( fbo_frag, 1, src, NULL ); glCompileShader( fbo_frag ); glGetShaderiv( fbo_frag, GL_COMPILE_STATUS, &success ); if (!success) { printf("Failed to produce default fragment shader.\n"); } fbo_vert = glCreateShader( GL_VERTEX_SHADER ); src[0] = _blit_vsh; glShaderSource( fbo_vert, 1, src, NULL ); glCompileShader( fbo_vert ); glGetShaderiv( fbo_vert, GL_COMPILE_STATUS, &success ); if( !success ) { printf( "Failed to produce default vertex shader.\n" ); } fbo_shad = glCreateProgram(); glBindAttribLocation( fbo_shad, 0, "aPosition" ); glBindAttribLocation( fbo_shad, 1, "aTexCoord" ); glAttachShader( fbo_shad, fbo_frag ); glAttachShader( fbo_shad, fbo_vert ); glLinkProgram( fbo_shad ); glGetProgramiv( fbo_shad, GL_LINK_STATUS, &success ); if( !success ) { printf( "Failed to link default program.\n" ); } glUniform1i( glGetUniformLocation( fbo_shad, "uTex" ), 0 ); } #endif glDepthMask (GL_TRUE); // glClearColor(0.5, 0.5, 1.0, 0.0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen glDepthMask (GL_FALSE); drawBackDrop (); // Draw the room drawZBuffer(cameraX, cameraY, false); glEnable(GL_DEPTH_TEST); drawPeople (); // Then add any moving characters... glDisable(GL_DEPTH_TEST); viewSpeech (); // ...and anything being said drawStatusBar (); displayCursor (); if (brightnessLevel < 255) fixBrightness (); // This is for transitionLevel special effects glFlush(); #if !defined(HAVE_GLES2) SDL_GL_SwapBuffers(); #else if(fbo) { // blit the FBO now glBindFramebuffer(GL_FRAMEBUFFER, 0); GLuint old_prog; glGetIntegerv(GL_CURRENT_PROGRAM, (GLint*)&old_prog); glUseProgram(fbo_shad); glViewport(0, 0, realWinWidth, realWinHeight); const float vert[] = { -1.0, -1.0, +0.0, +0.0, +1.0, -1.0, fbo_tex_w, +0.0, -1.0, +1.0, +0.0, fbo_tex_h, +1.0, +1.0, fbo_tex_w, fbo_tex_h }; glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, fbo_tex); glEnableVertexAttribArray(0); glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (float*)vert); glEnableVertexAttribArray(1); glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (float*)vert + 2); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); glBindTexture(GL_TEXTURE_2D, 0); glUseProgram(old_prog); glViewport (viewportOffsetX, viewportOffsetY, viewportWidth, viewportHeight); } EGL_SwapBuffers(); if(fbo) { // Rebind FBO now glBindFramebuffer(GL_FRAMEBUFFER, fbo); } #endif }