bool CTextClass::Initialize(ID3D11Device* device, ID3D11DeviceContext* deviceContext, HWND hwnd, int screenWidth, int screenHeight, XMMATRIX& baseViewMatrix) { bool result; //Guardamos el alto y ancho de la pantalla, junto con la matriz base de vista m_screenWidth = screenWidth; m_screenHeight = screenHeight; m_baseViewMatrix = baseViewMatrix; //Inicializamos la clase Font m_Font = new CFontClass(); if (!m_Font) { return false; } result = m_Font->Initialize(device, "../Resources/Fonts/DaFontTextureCoord.txt", L"../Resources/Fonts/DaFont.dds"); if (!result) { MessageBox(hwnd, L"No se pudo inicializar la Font", L"Error", MB_OK); return false; } //Ahorael shader de la clase Font m_FontShader = new CFontShaderClass(); if (!m_FontShader) { return false; } result = m_FontShader->Initialize(device, hwnd); if (!result) { MessageBox(hwnd, L"No se pudo inicializar la Font Shader", L"Error", MB_OK); return false; } //Ahora las sentencias, primero la primera // result = initializeSentence(&m_Sentence1, 16, device); if (!result) { return false; } result = UpdateSentence(m_Sentence1, "Hello", 100, 100, 1.0f, 1.0f, 1.0f, deviceContext); if (!result) { return false; } //Ahora la segunda result = initializeSentence(&m_Sentence2, 16, device); if (!result) { return false; } result = UpdateSentence(m_Sentence2, "Goodbye", 100, 200, 1.0f, 1.0f, 0.0f, deviceContext); if (!result) { return false; } return true; }
/******************************************************************** * Clear an existing sentence ********************************************************************/ void clearSentence(struct Sentence *stSentence) { int i; DEBUG ? printf("initializeSentence\n") : 0; for (i = 0; i < stSentence->iLength; i++) { if (stSentence->szSentence[i]) free(stSentence->szSentence[i]); } free(stSentence->szSentence); initializeSentence(stSentence); }
/******************************************************************** * Read a sentence from the socket * A Sentence struct is returned ********************************************************************/ struct Sentence readSentence(int fdSock) { struct Sentence stReturnSentence; char *szWord; int i = 0; int iReturnLength = 0; DEBUG ? printf("readSentence\n") : 0; initializeSentence(&stReturnSentence); while (szWord = readWord(fdSock)) { addWordToSentence(&stReturnSentence, szWord); // check to see if we can get a return value from the API if (strstr(szWord, "!done") != NULL) { DEBUG ? printf("return sentence contains !done\n") : 0; stReturnSentence.iReturnValue = DONE; } else if (strstr(szWord, "!trap") != NULL) { DEBUG ? printf("return sentence contains !trap\n") : 0; stReturnSentence.iReturnValue = TRAP; } else if (strstr(szWord, "!fatal") != NULL) { DEBUG ? printf("return sentence contains !fatal\n") : 0; stReturnSentence.iReturnValue = FATAL; } free(szWord); } // if any errors, get the next sentence if ((stReturnSentence.iReturnValue == TRAP) || (stReturnSentence.iReturnValue == FATAL)) { readSentence(fdSock); } if (DEBUG) { for (i = 0; i < stReturnSentence.iLength; i++) { printf("stReturnSentence.szSentence[%d] = %s\n", i, stReturnSentence.szSentence[i]); } } return stReturnSentence; }
/******************************************************************** * Login to the API * 1 is returned on successful login * 0 is returned on unsuccessful login ********************************************************************/ int login(int fdSock, char *username, char *password) { struct Sentence stReadSentence; struct Sentence stWriteSentence; char *szMD5Challenge; char *szMD5ChallengeBinary; char *szMD5PasswordToSend; char *szLoginUsernameResponseToSend; char *szLoginPasswordResponseToSend; md5_state_t state; md5_byte_t digest[16]; char cNull[1] = { 0 }; writeWord(fdSock, "/login"); writeWord(fdSock, ""); stReadSentence = readSentence(fdSock); DEBUG ? printSentence(&stReadSentence) : 0; if (stReadSentence.iReturnValue != DONE) { //printf("error.\n"); //exit(0); return 0; } // extract md5 string from the challenge sentence szMD5Challenge = strtok(stReadSentence.szSentence[1], "="); szMD5Challenge = strtok(NULL, "="); DEBUG ? printf("MD5 of challenge = %s\n", szMD5Challenge) : 0; // convert szMD5Challenge to binary szMD5ChallengeBinary = md5ToBinary(szMD5Challenge); // get md5 of the password + challenge concatenation md5_init(&state); md5_append(&state, cNull, 1); md5_append(&state, (const md5_byte_t *)password, strlen(password)); md5_append(&state, (const md5_byte_t *)szMD5ChallengeBinary, 16); md5_finish(&state, digest); // convert this digest to a string representation of the hex values // digest is the binary format of what we want to send // szMD5PasswordToSend is the "string" hex format szMD5PasswordToSend = md5DigestToHexString(digest); DEBUG ? printf("szPasswordToSend = %s\n", szMD5PasswordToSend) : 0; // put together the login sentence initializeSentence(&stWriteSentence); addWordToSentence(&stWriteSentence, "/login"); addWordToSentence(&stWriteSentence, "=name="); addPartWordToSentence(&stWriteSentence, username); addWordToSentence(&stWriteSentence, "=response=00"); addPartWordToSentence(&stWriteSentence, szMD5PasswordToSend); free(szMD5ChallengeBinary); free(szMD5PasswordToSend); DEBUG ? printSentence(&stWriteSentence) : 0; writeSentence(fdSock, &stWriteSentence); stReadSentence = readSentence(fdSock); DEBUG ? printSentence(&stReadSentence) : 0; if (stReadSentence.iReturnValue == DONE) { return 1; } else { return 0; } }
bool TextClass::initialize( ID3D11Device* aD3DDevice, ID3D11DeviceContext* aD3DDeviceContext, HWND aHwnd, int aWidth, int aHeight, D3DXMATRIX aBaseViewMatrix) { bool result; m_screenWidth = aWidth; m_screenHeight = aHeight; m_baseViewMatrix = aBaseViewMatrix; m_font = new FontClass; if(!m_font) { return false; } result = m_font->initialize( aD3DDevice, "./obj/fontdata.txt", L"./tex/font.dds"); if(!result) { MessageBox(aHwnd, L"初始化字体对象失败", L"Error", MB_OK); return false; } m_fontShader = new FontShaderClass; if(!m_fontShader) { return false; } result = m_fontShader->initialize(aD3DDevice, aHwnd); if(!result) { MessageBox(aHwnd, L"初始化字体着色器对象失败", L"Error", MB_OK); return false; } result = initializeSentence(&m_sentence1, 16, aD3DDevice); if(!result) { return false; } result = updateSentence( m_sentence1, "Hello", 100, 100, 1.0f, 1.0f, 1.0f, aD3DDeviceContext); if(!result) { return false; } result = initializeSentence(&m_sentence2, 16, aD3DDevice); if(!result) { return false; } result = updateSentence( m_sentence2, "Goodbye", 100, 200, 1.0f, 1.0f, 0.0f, aD3DDeviceContext); if(!result) { return false; } return true; }