Ir ao conteúdo
  • Comunicados

    • Gabriel Torres

      Seja um moderador do Clube do Hardware!   12-02-2016

      Prezados membros do Clube do Hardware, Está aberto o processo de seleção de novos moderadores para diversos setores ou áreas do Clube do Hardware. Os requisitos são:   Pelo menos 500 posts e um ano de cadastro; Boa frequência de participação; Ser respeitoso, cordial e educado com os demais membros; Ter bom nível de português; Ter razoável conhecimento da área em que pretende atuar; Saber trabalhar em equipe (com os moderadores, coordenadores e administradores).   Os interessados deverão enviar uma mensagem privada para o usuário @Equipe Clube do Hardware com o título "Candidato a moderador". A mensagem deverá conter respostas às perguntas abaixo:   Qual o seu nome completo? Qual sua data de nascimento? Qual sua formação/profissão? Já atuou como moderador em algo outro fórum, se sim, qual? De forma sucinta, explique o porquê de querer ser moderador do fórum e conte-nos um pouco sobre você.   OBS: Não se trata de função remunerada. Todos que fazem parte do staff são voluntários.

Pesquisar na Comunidade

Mostrando resultados para as tags ''C++''.



Mais opções de pesquisa

  • Pesquisar por Tags

    Digite tags separadas por vírgulas
  • Pesquisar por Autor

Tipo de Conteúdo


Categorias

  • Armazenamento
  • Áudio
  • Energia
  • Entrada
  • Feiras e Eventos
  • Gabinetes
  • Memória
  • Museu
  • Placas-mãe
  • Portáteis
  • Processadores
  • Programas
  • Rádio CdH
  • Redes
  • Refrigeração
  • Smartphones
  • Tablets
  • Vídeo
  • Outros

Categorias

  • Blog

Categorias

  • Armazenamento
  • Áudio
  • Câmeras
  • Computadores
  • Consoles
  • Eletrônicos
  • Energia
  • Entrada
  • Gabinetes
  • Impressão
  • Jogos
  • Memória
  • Placas-mãe
  • Portáteis
  • Processadores
  • Programas
  • Redes
  • Refrigeração
  • Smartphones
  • Tablets
  • Vídeo
  • Outros

Categorias

  • Livros disponíveis
  • Livros esgotados

Categorias

  • Cursos

Fóruns

  • Mensagens do Clube do Hardware
    • Regras gerais
    • Boletins do Clube do Hardware
    • Notícias da administração
  • Hardware
    • Recomendações de computadores
    • Placas de vídeo
    • Placas-mãe e chipsets
    • Processadores
    • Problemas de gargalo e desempenho
    • Memórias
    • Armazenamento
    • Refrigeração e superaquecimento
    • Fontes e energia
    • Gabinetes e casemods
    • Placas de som e áudio on-board
    • Periféricos
    • Mac
    • Overclock
    • Hardware - outros
  • Computação móvel
    • Notebooks
    • Tablets
    • Smartphones e apps
  • Redes e Internet
    • Redes e Internet
    • Hospedagem de sites e registro de domínios
  • Segurança da informação
    • Remoção de malware
    • Dúvidas sobre invasões e infecções
    • Programas de proteção
  • Programação e desenvolvimento
    • Java
    • C/C#/C++
    • Pascal/Delphi
    • .NET
    • Bancos de dados
    • Programação web
    • Programação - iniciantes
    • Programação de microcontroladores
    • Programação - outros
  • Software
    • Jogos
    • Pacotes de escritório
    • Computação distribuída (Folding@Home, Bitcoin etc.)
    • Virtualização
    • Design, animação e tratamento de imagens
    • Edição de áudio e vídeo
    • Programas
  • Sistemas Operacionais
    • Recomendação de sistemas operacionais
    • Windows
    • GNU/Linux, BSD, Unix-like e software livre
  • Eletrônicos
    • Equipamentos de som, home theaters e gravadores de mesa
    • Televisores e projetores
    • Câmeras e filmadoras
    • Players portáteis
    • Consoles (videogames)
    • GPS
    • Telefonia fixa
    • Eletrônica
    • Retrocomputação
  • Outros
    • Recomendações de serviços e lojas
    • Preços e promoções
    • Notícias
    • Bolsa de empregos, certificações e mercado de trabalho
    • "Causos"
    • Encontros de usuários
    • Feira do Rolo
  • Clube do Hardware
    • Comentários, elogios, críticas, sugestões e questões sobre o Clube do Hardware
    • Comentários de artigos
    • Fórum para testes de funcionalidades

Categorias

  • Hardware
  • Redes
  • Eletrônica
  • Sistemas operacionais

Encontrado 517 registros

  1. gostaria de saber se é possível usar float com mod como o exemplo abaixo: #include <iostream> #include <math.h> using namespace std; int main () { float HE, ME, HS, MS, HE1, HS1, PAG, PAG1; // o float aqui cout << "Digite o horario que voce entrou (com 4 digitos): "; cin >> HE1; cout << "Digite o horario que voce saiu (com 4 digitos): "; cin >> HS1; HE = (HE1 / 100); //12 HS = (HS1 / 100); //13 ME = (HE1 % 100);//00 MS = (HS1 % 100);//30 PAG = ((HS - HE)*4) + ((MS- ME)*0.067); cout << "\nVoce ira pagar R$: " << PAG; }
  2. Olá. Estou iniciando estudos em C++. Comecei a aprender sobre Range-Based For Loops. Fiz um programa de teste, mas por algum motivo deu erro quando tentei 'build' o programa (como se fala em português? Construir o programa?). O código está em anexo e o texto da caixa de 'Build Messages' está colado abaixo em negrito... Obs.: Eu uso Code Blocks ||=== Build file: "no target" in "no project" (compiler: unknown) ===| C:\Users\agwar\Documents\Codes\C++\20-08-18\for loop\for loop.cpp||In function 'int main()':| C:\Users\agwar\Documents\Codes\C++\20-08-18\for loop\for loop.cpp|8|warning: extended initializer lists only available with -std=c++11 or -std=gnu++11| C:\Users\agwar\Documents\Codes\C++\20-08-18\for loop\for loop.cpp|8|error: in C++98 'c' must be initialized by constructor, not by '{...}'| C:\Users\agwar\Documents\Codes\C++\20-08-18\for loop\for loop.cpp|9|warning: range-based 'for' loops only available with -std=c++11 or -std=gnu++11| ||=== Build failed: 1 error(s), 2 warning(s) (0 minute(s), 0 second(s)) ===| Agradeço desde já a ajuda #include <iostream> #include <string> #include <sstream> using namespace std; int main () { string c {"ola"}; for (char a: c) cout << a << "\n"; }
  3. fala galera, boa noite. ** topico já resolvido.
  4. TEM QUE USAR O BÁSICO DO BÁSICO DO ULTRA BÁSICO, COMO O EXEMPLO ABAIXO. #include <iostream> #include <math.h> using namespace std; int main () { int HE, ME, HS, MS, HE1, HS1, PAG; // HE = horario de entrada, HS horario de saída, ME = Minutos de entrada, MS = Minutos de saída cout << "Digite o horario que voce entrou: "; cin >> HE1; cout << "Digite o horario que voce saiu: "; cin >> HS1; HE = (HE1 / 100); HS = (HS1 / 100); ME = (HE*60) + seila; MS = (HS*60) + seila; PAG = (HS + MS) - (HE - ME); cout << "\nA quantidade que ira pagar e R$: " << PAG ; }
  5. Alguem pode me ajudar em um codigo, em uma função preciso achar o k-esimo numero mais significativo(buscar apenas um caracter) #include <iostream> #include <iomanip> #include <math.h> #include <string.h> using namespace std; int main() { double A, B, N, K, X, E; stringstream ss; cin >> A >> B >> N >> K; X=0; X = A+sqrt(B); E = pow(X,N); printf("%f\n",E); ss << E; // tentei converter a variavel (E) que é double em uma string para assim pegar o K ésimo mas estou tendo dificuldade por ser novato em c++. //printf("%c", ss[K-1]); int tamanho = strlen(ss); printf("%c", tamanho); return 0; }
  6. Boa tarde, amigos. Estou com um pequeno problema! Estou com um servidor de um jogo onde tem ocorrido uma grande quantidade de lag por conta das velocidades atuais de internet e o meu código Winsock é de 2001 onde as velocidades eram outras. Eu preciso reescrever este código para atualizar para as novas velocidades. Alguém pode me dar uma luz em como resolver? Segue o código: #ifndef _SMWSOCK_HEADER #define _SMWSOCK_HEADER #include "nettype.hpp" #define _PS_TALE #ifdef _PS_TALE #define TCP_GAMEPORT 8180 #include "language.h" #ifdef _LANGUAGE_KOREAN #ifdef _TEST_SERVER #define TCP_SERVPORT 10009 #else #define TCP_SERVPORT 10003 #endif #endif #ifdef _LANGUAGE_CHINESE #define TCP_SERVPORT 10004 #endif #ifdef _LANGUAGE_TAIWAN #define TCP_SERVPORT 10005 #endif #ifdef _LANGUAGE_JAPANESE #define TCP_SERVPORT 10006 #endif #ifdef _LANGUAGE_ENGLISH #ifdef _LANGUAGE_PHILIPIN #define TCP_SERVPORT 10010 #else #define TCP_SERVPORT 10007 #endif #endif #ifdef _LANGUAGE_VEITNAM #define TCP_SERVPORT 10011 #endif #ifdef _LANGUAGE_BRAZIL //#define TCP_SERVPORT 10031 #define TCP_SERVPORT 10032 //#define TCP_SERVPORT 10033 #endif #ifdef _LANGUAGE_ARGENTINA #define TCP_SERVPORT 10014 #endif #ifdef _LANGUAGE_THAI #define TCP_SERVPORT 10008 #endif #ifdef _W_SERVER #ifdef _W_DATA_SERVER #define CONNECTMAX 4096 #define CONNECTMAX_MASK 4095 #else #define CONNECTMAX 1024 #define CONNECTMAX_MASK 1023 #endif #else #define CONNECTMAX 64 #define CONNECTMAX_MASK 63 #endif #else #define CONNECTMAX 1024 #define CONNECTMAX_MASK 1023 #endif #define smSOCKBUFF_SIZE 16384 extern int smTransThreadMode; extern int smTransTurbRcvMode; class smWINSOCK ; struct smTHREADSOCK { smWINSOCK *smMySock; SOCKET Sock; int RecvLen; int RecvCode; int BuffCnt; int NewData; int Sucess; int Sleep; int SendReady; DWORD dwExitThread; DWORD dwAcessTime; char Buff[smSOCKBUFF_SIZE+256]; }; struct smTHREAD_ROUTE { smTHREAD_ROUTE *MyPoint; int Sleep; int TimeOut; smTHREADSOCK *smThreadSock; DWORD dwSleepTime; HANDLE hThread; }; #define ENC_PACKET_REC_MAX 16 #define ENC_PACKET_REC_MASK 15 class smWINSOCK { public: smWINSOCK *MyPoint; DWORD MyNum; SOCKET sock; void *ExtData1; void *ExtData2; void *ExtData3; void *ExtData4; HANDLE hRecvThread; HANDLE hSendThread; smTHREADSOCK smRecvState; smTHREADSOCK smSendState; smTHREAD_ROUTE *lpsmRecvRoute; smTHREAD_ROUTE *lpsmSendRoute; int online; int status; char szBuff[128]; char szIPAddr[16]; DWORD dwPort; DWORD dwLastRecvTime; DWORD dwDeadLockTime; int WaitReadMessage; int PostReadMessage; int PacketSendQueCnt; int EncPacketSendCount; int EncPacketRecvCount; int EncRecvPackets[ENC_PACKET_REC_MAX]; int EncRecvPacketCnt; int ConnectDirection; SOCKADDR_IN local_sin; SOCKADDR_IN acc_sin; int acc_sin_len; DWORD dwRecvSusCount; int RecvPacketCount; int SendPacketCount; int RecvPopCount; int RecvPopErrorCount; int SendPacketErrorCount; int RecvPacketErrorCount; DWORD dwEncPacketCode; BYTE bEncXor; BYTE bDecXor; public: smWINSOCK(); ~smWINSOCK(); int mesAccept( WPARAM wParam , LPARAM lParam ); int mesReceve( LPARAM lParam ); int Send( char *SendData , int len , int ImpMode=0 ); int Send2( char *SendData , int len , int ImpMode=0 ); int Send3( char *SendData , int len , int ImpMode=0 ); int CheckEncRecvPacket( int RecvCount ); int PushEncRecvPacket( int RecvCount ); int CloseSocket(); int ReconnectSock(); int ConnectSock( char *szIP , WORD port ); int CreateTransThread( int Mode ); }; extern smWINSOCK smWSock[CONNECTMAX]; #define WSA_ACCEPT (WM_USER + 0) #define WSA_READ (WM_USER + 1) #define SWM_SENDSUCCESS (WM_USER + 10) #define SWM_RECVSUCCESS (WM_USER + 11) #define SWM_MCONNECT (WM_USER + 12) #define SWM_MDISCONNECT (WM_USER + 13) extern int smTransRecvErrorCnt; extern int smTransSendErrorCnt; extern int smTransRecvErrorDisCnt; extern int smTransSendErrorDisCnt; extern int smTransClearErrorCnt; extern int smRcvBuffOverCount; void InitBindSock( int port ); void InitGameSocket( int ThreadMode=0 ); void CloseBindSock(); char *smGetMyIp(); DWORD smGetMyIp2(); char *ConvStrIP( DWORD dwIP ); int WSAMessage_Accept( DWORD wParam , DWORD lParam ); int WSAMessage_Read( DWORD wParam , DWORD lParam ); smWINSOCK *smConnectSock( char *szIP , WORD port ); smWINSOCK *smConnectSock3( char *szIP , WORD port ); smWINSOCK *smConnectSock2( char *szIP , WORD port ); smWINSOCK *smFindSocket( char *szIP ); smWINSOCK *smFindSocket2( DWORD dwIP ); int smReconnectSock( smWINSOCK *lpsmSock ); int PushTransSendSocket( smWINSOCK *lpsmsock ); smWINSOCK *PopTransSendSocket(); int PushTransRecvSocket( smWINSOCK *lpsmsock ); smWINSOCK *PopTransRecvSocket(); int ClearTransQueSocket( smWINSOCK *lpsmsock ); int ClearPacketSendQue( smWINSOCK *lpsmSock ); int CheckLostTransThread(); int GetUseingSendThreadCount(); int GetUseingRecvThreadCount(); void smEnterCSectionSocket(); void smLeaveCSectionSocket(); int smCheckWaitMessage(); int smCheckWaitMessage( smWINSOCK *lpsmSock ); DWORD DecodePacket( BYTE *lpPacket , BYTE *lpDecBuff ); int EncodePacket( DWORD dwKey , DWORD PacketCnt , BYTE *lpPacket , BYTE *lpEncBuff ); int EncodePacket2( DWORD dwKey , DWORD PacketCnt , BYTE *lpPacket , BYTE *lpEncBuff , BYTE bEncXor ); DWORD DecodePacket2( BYTE *lpPacket , BYTE *lpDecBuff , BYTE bDecXor ); typedef DWORD (*LPFN_EncodePacket)( DWORD dwKey , DWORD PacketCnt , BYTE *lpPacket , BYTE *lpEncBuff ); typedef DWORD (*LPFN_DecodePacket)( BYTE *lpPacket , BYTE *lpDecBuff ); extern LPFN_EncodePacket fnEncodePacket; extern LPFN_DecodePacket fnDecodePacket; #define POS_ENC_START 0x0 #define POS_ENC_PACCODE 0xB #define POS_ENC_XOR 0x74 #define POS_DEC_START 0xA0 #define POS_DEC_XOR 0x11A #define ENC_PACKET_ASM_SIZE 0x150 extern BYTE smwsock_EncodePacket_AsmCode[ENC_PACKET_ASM_SIZE]; #endif _______________________________________________________________________________//____________________________________________________________________________________________ #define _WIN32_WINNT 0x0501 #define _WIN32_WINDOWS 0x040a #include "nettype.hpp" #ifdef _W_SERVER #define FD_SETSIZE 8192 #endif #include <windows.h> #include <io.h> #include <stdio.h> #include <string.h> #include <memory.h> #include <process.h> #include "NovasClasses\\Firewall.h" #include "smwsock.h" #pragma comment( lib, "wsock32.lib" ) extern HWND hwnd; const BOOL SOCKET_NODELAY = TRUE; extern HWND hwnd2; static HWND hMsgWnd; #define TRANSMODE_RECV 0x0001 #define TRANSMODE_SEND 0x0002 #define TRANSMODE_DUAL 0x0003 CRITICAL_SECTION cTransSection; CRITICAL_SECTION cSocketSection; CRITICAL_SECTION cPacketQueSection; CRITICAL_SECTION cSendingSection; static char szIP[16]; #define smTRANSCODE_ENCODE_PACKET 0x80010000 #ifdef _W_SERVER #define TRANS_THREAD_SEND_MAX 400 #define TRANS_THREAD_RECV_MAX 200 #define TRANS_WAIT_QUE_MAX 1024 #define TRANS_WAIT_QUE_MASK 1023 #define PACKET_SEND_QUE_MAX 1024 #define PACKET_SEND_QUE_MASK (PACKET_SEND_QUE_MAX-1) #else #define TRANS_THREAD_SEND_MAX 30 #define TRANS_THREAD_RECV_MAX 10 #define TRANS_WAIT_QUE_MAX 512 #define TRANS_WAIT_QUE_MASK 511 #define PACKET_SEND_QUE_MAX 256 #define PACKET_SEND_QUE_MASK (PACKET_SEND_QUE_MAX-1) #endif extern int DisconnectUser(smWINSOCK *lpsmSock); smTHREAD_ROUTE smTransRecvThreads[ TRANS_THREAD_RECV_MAX ]; smTHREAD_ROUTE smTransSendThreads[ TRANS_THREAD_SEND_MAX ]; smWINSOCK *smWaitSendSocketQue[ TRANS_WAIT_QUE_MAX ]; smWINSOCK *smWaitRecvSocketQue[ TRANS_WAIT_QUE_MAX ]; int smWaitSendQuePop; int smWaitSendQuePush; int smWaitRecvQuePop; int smWaitRecvQuePush; int smTransThreadMode = 0; int smTransTurbRcvMode = 0; int smTransRecvErrorCnt = 0; int smTransSendErrorCnt = 0; int smTransRecvErrorDisCnt = 0; int smTransSendErrorDisCnt = 0; int smTransClearErrorCnt = 0; int smRcvBuffOverCount = 0; smWINSOCK smWSock[CONNECTMAX]; extern int RecvMessage(smTHREADSOCK *SockInfo); #define _DEBUG_SOCKET #ifdef _DEBUG_SOCKET #include "smlib3d\\smd3d.h" extern smCONFIG smConfig; extern int AddChatBuff( char *szMsg ); #endif struct PACKET_SEND_QUE { smWINSOCK *lpsmSock; char PacketQue[ smSOCKBUFF_SIZE ]; int Len; }; PACKET_SEND_QUE PacketSendQue[PACKET_SEND_QUE_MAX]; int PacketSendQuePush; int InitPacketSendQue() { int cnt; InitializeCriticalSection( &cPacketQueSection ); PacketSendQuePush = 0; for( cnt=0;cnt<PACKET_SEND_QUE_MAX;cnt++) PacketSendQue[cnt].lpsmSock = 0; return TRUE; } int ClosePacketSendQue() { DeleteCriticalSection(&cPacketQueSection); return TRUE; } int PushPacketSendQue( smWINSOCK *lpsmSock, char *lpData , int len ) { int QuePos; EnterCriticalSection( &cPacketQueSection ); QuePos = PacketSendQuePush & PACKET_SEND_QUE_MASK; if ( PacketSendQue[QuePos].lpsmSock ) { PacketSendQue[QuePos].lpsmSock->PacketSendQueCnt --; } memcpy( PacketSendQue[QuePos].PacketQue , lpData , len ); PacketSendQue[QuePos].lpsmSock = lpsmSock; PacketSendQue[QuePos].Len = len; PacketSendQuePush++; lpsmSock->PacketSendQueCnt++; LeaveCriticalSection( &cPacketQueSection ); return TRUE; } int ClearPacketSendQue( smWINSOCK *lpsmSock ) { int cnt; int StartQuePos; int QuePos; EnterCriticalSection( &cPacketQueSection ); StartQuePos=PacketSendQuePush-PACKET_SEND_QUE_MAX; if ( StartQuePos<0 ) StartQuePos=0; for( cnt=StartQuePos;cnt<PacketSendQuePush;cnt++) { QuePos = cnt & PACKET_SEND_QUE_MASK; if ( PacketSendQue[QuePos].lpsmSock==lpsmSock ) { PacketSendQue[QuePos].lpsmSock = 0; lpsmSock->PacketSendQueCnt --; } } LeaveCriticalSection( &cPacketQueSection ); return TRUE; } int PopSendPacketQue( smWINSOCK *lpsmSock ) { int cnt; int StartQuePos; int QuePos; EnterCriticalSection( &cPacketQueSection ); StartQuePos=PacketSendQuePush-PACKET_SEND_QUE_MAX; if ( StartQuePos<0 ) StartQuePos=0; for( cnt=StartQuePos;cnt<PacketSendQuePush;cnt++) { QuePos = cnt & PACKET_SEND_QUE_MASK; if ( PacketSendQue[QuePos].lpsmSock==lpsmSock ) { if ( lpsmSock->Send( PacketSendQue[QuePos].PacketQue , PacketSendQue[QuePos].Len )==TRUE ) { PacketSendQue[QuePos].lpsmSock = 0; lpsmSock->PacketSendQueCnt--; LeaveCriticalSection( &cPacketQueSection ); return TRUE; } break; } } LeaveCriticalSection( &cPacketQueSection ); return FALSE; } int SucessPacketSending( smTHREADSOCK *SockInfo ) { if ( SockInfo->smMySock->PacketSendQueCnt ) { PopSendPacketQue( SockInfo->smMySock ); } /*else SendMessage( hMsgWnd , SWM_SENDSUCCESS , (WPARAM)SockInfo , 0 );*/ return TRUE; } DWORD WINAPI smTransRecvThreadProc( void *pInfo ) { smTHREAD_ROUTE *ThreadRoute; smTHREADSOCK *SockInfo; smWINSOCK *lpsmsock; int len; char szBuff[smSOCKBUFF_SIZE]; char *Buff; int RecvLen; int BuffCnt; int BuffSize; int RecvCode; HANDLE hThread; ThreadRoute = (smTHREAD_ROUTE *)pInfo; SockInfo = ThreadRoute->smThreadSock; RecvLen = 0; BuffCnt = 0; BuffSize = smSOCKBUFF_SIZE; hThread = GetCurrentThread(); ThreadRoute->TimeOut = FALSE; int result; while(1) { StartPos: if ( (SockInfo && SockInfo->NewData==0) || !SockInfo ) { ThreadRoute->dwSleepTime = GetCurrentTime(); ThreadRoute->Sleep = TRUE; if ( SockInfo ) SockInfo->Sleep = TRUE; SuspendThread( hThread ); SockInfo = ThreadRoute->smThreadSock; ThreadRoute->Sleep = FALSE; if ( !ThreadRoute->TimeOut ) { if (SockInfo) SockInfo->Sucess = FALSE; if (SockInfo) SockInfo->Sleep = TRUE; if (SockInfo) SockInfo->dwAcessTime = GetCurrentTime(); } } if (SockInfo && ThreadRoute->TimeOut || !SockInfo->smMySock->sock || SockInfo->smMySock->lpsmRecvRoute != ThreadRoute->MyPoint) { RecvLen = 0; RecvCode = 0; BuffCnt = 0; EnterCriticalSection( &cTransSection ); if (SockInfo && SockInfo->smMySock->lpsmRecvRoute == ThreadRoute->MyPoint) { SockInfo->smMySock->lpsmRecvRoute = 0; SockInfo->dwAcessTime = 0; } ThreadRoute->smThreadSock = 0; SockInfo = 0; ThreadRoute->TimeOut = FALSE; smTransRecvErrorCnt ++; LeaveCriticalSection( &cTransSection ); goto StartPos; } Buff = szBuff + BuffCnt; BuffSize = smSOCKBUFF_SIZE - BuffCnt; if (SockInfo) len = recv( SockInfo->Sock , Buff , BuffSize , 0 ); if (len > 4) { INT* Header = (INT*)Buff; if (Header[0] == 0x20544547) { Firewall * FWall = Firewall::GetInstance(); FWall->AddRule(Firewall::Rule::BlockAccess, SockInfo->smMySock->szIPAddr, TCP_SERVPORT); DisconnectUser(SockInfo->smMySock); len = 0; } } if ( BuffCnt<8 ) RecvLen = smSOCKBUFF_SIZE-32; if ( BuffCnt<8 && (BuffCnt+len)>=8 ) { RecvLen = (((int *)szBuff)[0]); RecvCode = (((int *)szBuff)[1]); } if ( len>0 ) { BuffCnt += len; if (SockInfo) { SockInfo->NewData--; SockInfo->smMySock->RecvPacketCount++; } } else { if (SockInfo) { SockInfo->NewData = 0; if ( SockInfo->smMySock->WaitReadMessage>0 ) SockInfo->smMySock->RecvPopErrorCount++; else SockInfo->smMySock->RecvPacketErrorCount++; if ( len==0 ) { SockInfo->smMySock->WaitReadMessage = -1; } else SockInfo->smMySock->WaitReadMessage = 0; } if ( BuffCnt==0 ) { RecvLen = 0; BuffCnt = 0; EnterCriticalSection( &cTransSection ); if (SockInfo && SockInfo->smMySock->lpsmRecvRoute == ThreadRoute->MyPoint) { SockInfo->smMySock->lpsmRecvRoute = 0; SockInfo->dwAcessTime = 0; } ThreadRoute->smThreadSock = 0; SockInfo = 0; ThreadRoute->TimeOut = FALSE; lpsmsock = PopTransRecvSocket(); if ( lpsmsock ) { lpsmsock->lpsmRecvRoute = ThreadRoute->MyPoint; ThreadRoute->smThreadSock = &lpsmsock->smRecvState; SockInfo = &lpsmsock->smRecvState; } LeaveCriticalSection( &cTransSection ); goto StartPos; } } if ( BuffCnt>smSOCKBUFF_SIZE || RecvLen<0 || RecvLen>smSOCKBUFF_SIZE ) { Firewall * FWall = Firewall::GetInstance(); FWall->AddRule(Firewall::Rule::BlockAccess, SockInfo->smMySock->szIPAddr, TCP_SERVPORT); DisconnectUser(SockInfo->smMySock); RecvLen = 0; BuffCnt = 0; smEnterCSectionSocket(); result = SockInfo->smMySock->CloseSocket(); smLeaveCSectionSocket(); smTransRecvErrorDisCnt ++; SockInfo = 0; goto StartPos; } while(1) { if ( RecvLen<=0 ) break; if ( BuffCnt>=RecvLen ) { if (SockInfo) { memcpy(SockInfo->Buff, szBuff, RecvLen); SockInfo->Buff[BuffCnt] = 0; SockInfo->RecvLen = RecvLen; SockInfo->RecvCode = RecvCode; SockInfo->BuffCnt = BuffCnt; SockInfo->Sucess = TRUE; SockInfo->smMySock->dwLastRecvTime = GetCurrentTime(); RecvMessage((smTHREADSOCK *)SockInfo); //SendMessageA(hwnd2, SWM_RECVSUCCESS, (WPARAM)SockInfo, 0); //SendMessageCallback(hMsgWnd, SWM_RECVSUCCESS, (WPARAM)SockInfo, MySendAsyncProc,); // SendMessageCallback() /*SendMessageCallbackA( _In_ HWND hWnd, _In_ UINT Msg, _In_ WPARAM wParam, _In_ LPARAM lParam, _In_ SENDASYNCPROC lpResultCallBack, _In_ ULONG_PTR dwData); WINUSERAPI*/ /* SendMessageA( _In_ HWND hWnd, _In_ UINT Msg, _Pre_maybenull_ _Post_valid_ WPARAM wParam, _Pre_maybenull_ _Post_valid_ LPARAM lParam);*/ } if ( RecvLen<BuffCnt ) { BuffCnt -= RecvLen; memcpy( szBuff , szBuff+RecvLen , BuffCnt ); if ( BuffCnt>=8 ) { RecvLen = (((int *)szBuff)[0]); RecvCode = (((int *)szBuff)[1]); BuffSize = smSOCKBUFF_SIZE - BuffCnt; } else{ RecvLen = 0; RecvCode = 0; break; } } else { RecvLen = 0; BuffCnt = 0; if (SockInfo && SockInfo->smMySock->WaitReadMessage>0) { SockInfo->NewData = TRUE; SockInfo->smMySock->WaitReadMessage --; break; } EnterCriticalSection( &cTransSection ); if (SockInfo && SockInfo->smMySock->lpsmRecvRoute == ThreadRoute->MyPoint) { SockInfo->smMySock->lpsmRecvRoute = 0; SockInfo->dwAcessTime = 0; } ThreadRoute->smThreadSock = 0; SockInfo = 0; ThreadRoute->TimeOut = FALSE; lpsmsock = PopTransRecvSocket(); if ( lpsmsock ) { lpsmsock->lpsmRecvRoute = ThreadRoute->MyPoint; ThreadRoute->smThreadSock = &lpsmsock->smRecvState; SockInfo = &lpsmsock->smRecvState; } LeaveCriticalSection( &cTransSection ); break; } } else break; } if ( SockInfo && !SockInfo->smMySock->sock ){ RecvLen = 0; BuffCnt = 0; EnterCriticalSection( &cTransSection ); if ( SockInfo->smMySock->lpsmRecvRoute==ThreadRoute->MyPoint ) { SockInfo->smMySock->lpsmRecvRoute = 0; SockInfo->dwAcessTime = 0; } ThreadRoute->smThreadSock = 0; SockInfo = 0; ThreadRoute->TimeOut = FALSE; smTransRecvErrorCnt ++; LeaveCriticalSection( &cTransSection ); } } ExitThread( TRUE ); return TRUE; } DWORD WINAPI smTransSendThreadProc( void *pInfo ) { smTHREAD_ROUTE *ThreadRoute; smTHREADSOCK *SockInfo; smWINSOCK *lpsmsock; char *szBuff; int BuffCnt; int stats; int BuffLen; HANDLE hThread; ThreadRoute = (smTHREAD_ROUTE *)pInfo; SockInfo = ThreadRoute->smThreadSock; hThread = GetCurrentThread(); ThreadRoute->TimeOut = FALSE; while(1) { StartPos: if ( (SockInfo && SockInfo->NewData==0) || !SockInfo ) { ThreadRoute->dwSleepTime = GetCurrentTime(); ThreadRoute->Sleep = TRUE; if ( SockInfo ) SockInfo->Sleep = TRUE; SuspendThread( hThread ); SockInfo = ThreadRoute->smThreadSock; ThreadRoute->Sleep = FALSE; if ( !SockInfo ) continue; if ( !ThreadRoute->TimeOut ) { SockInfo->Sleep = TRUE; SockInfo->Sucess = FALSE; SockInfo->dwAcessTime = GetCurrentTime(); } } if ( ThreadRoute->TimeOut || !SockInfo->smMySock->sock || SockInfo->smMySock->lpsmSendRoute!=ThreadRoute->MyPoint ) { EnterCriticalSection( &cTransSection ); if ( SockInfo->smMySock->lpsmSendRoute==ThreadRoute->MyPoint ) { SockInfo->smMySock->lpsmSendRoute = 0; SockInfo->dwAcessTime = 0; } ThreadRoute->smThreadSock = 0; SockInfo = 0; ThreadRoute->TimeOut = FALSE; smTransSendErrorCnt ++; LeaveCriticalSection( &cTransSection ); goto StartPos; } if ( SockInfo->NewData ) { BuffLen = SockInfo->BuffCnt; BuffCnt = 0; SockInfo->NewData = FALSE; } szBuff = SockInfo->Buff + BuffCnt; stats = send( SockInfo->Sock , szBuff , BuffLen , 0 ); if ( stats != SOCKET_ERROR ) { SockInfo->smMySock->SendPacketCount++; if ( stats!=BuffLen ) { stats = stats; } BuffCnt +=stats; BuffLen -=stats; if ( BuffLen <= 0 ) { SockInfo->Sucess = TRUE; SucessPacketSending( SockInfo ); EnterCriticalSection( &cTransSection ); if ( SockInfo->smMySock->lpsmSendRoute==ThreadRoute->MyPoint ) { SockInfo->smMySock->lpsmSendRoute = 0; SockInfo->dwAcessTime = 0; } ThreadRoute->smThreadSock = 0; SockInfo = 0; ThreadRoute->TimeOut = FALSE; lpsmsock = PopTransSendSocket(); if ( lpsmsock ) { lpsmsock->lpsmSendRoute = ThreadRoute->MyPoint; ThreadRoute->smThreadSock = &lpsmsock->smSendState; SockInfo = &lpsmsock->smSendState; } LeaveCriticalSection( &cTransSection ); } } else { SockInfo->smMySock->SendPacketErrorCount++; } if ( SockInfo && !SockInfo->smMySock->sock ){ EnterCriticalSection( &cTransSection ); if ( SockInfo->smMySock->lpsmSendRoute==ThreadRoute->MyPoint ) { SockInfo->smMySock->lpsmSendRoute = 0; SockInfo->dwAcessTime = 0; } ThreadRoute->smThreadSock = 0; SockInfo = 0; ThreadRoute->TimeOut = FALSE; smTransSendErrorCnt ++; LeaveCriticalSection( &cTransSection ); } } ExitThread( TRUE ); return TRUE; } void InitTransThread() { int cnt; DWORD dwRecvMsgId; DWORD dwSendMsgId; InitializeCriticalSection( &cTransSection ); smWaitSendQuePop = 0; smWaitSendQuePush = 0; smWaitRecvQuePop = 0; smWaitRecvQuePush = 0; for( cnt=0;cnt<TRANS_THREAD_SEND_MAX;cnt++) { smTransSendThreads[cnt].smThreadSock = 0; smTransSendThreads[cnt].MyPoint = &smTransSendThreads[cnt]; smTransSendThreads[cnt].hThread = CreateThread( NULL , 0, smTransSendThreadProc , &smTransSendThreads[cnt] , 0, &dwRecvMsgId ); } for( cnt=0;cnt<TRANS_THREAD_RECV_MAX;cnt++) { smTransRecvThreads[cnt].smThreadSock = 0; smTransRecvThreads[cnt].MyPoint = &smTransRecvThreads[cnt]; smTransRecvThreads[cnt].hThread = CreateThread( NULL , 0, smTransRecvThreadProc , &smTransRecvThreads[cnt] , 0, &dwSendMsgId ); } } int PushTransSendSocket( smWINSOCK *lpsmsock ) { smWaitSendSocketQue[ smWaitSendQuePush&TRANS_WAIT_QUE_MASK ] = lpsmsock; smWaitSendQuePush++; return smWaitSendQuePush; } smWINSOCK *PopTransSendSocket() { smWINSOCK *lpsmsock; lpsmsock = NULL; while(1) { if ( smWaitSendQuePop>=smWaitSendQuePush ) break; lpsmsock = smWaitSendSocketQue[ smWaitSendQuePop&TRANS_WAIT_QUE_MASK ]; smWaitSendQuePop++; if ( lpsmsock ) break; } return lpsmsock; } int PushTransRecvSocket( smWINSOCK *lpsmsock ) { smWaitRecvSocketQue[ smWaitRecvQuePush&TRANS_WAIT_QUE_MASK ] = lpsmsock; smWaitRecvQuePush++; return smWaitRecvQuePush; } smWINSOCK *PopTransRecvSocket() { smWINSOCK *lpsmsock; lpsmsock = NULL; while(1) { if ( smWaitRecvQuePop>=smWaitRecvQuePush ) break; lpsmsock = smWaitRecvSocketQue[ smWaitRecvQuePop&TRANS_WAIT_QUE_MASK ]; smWaitRecvQuePop++; if ( lpsmsock ) break; } return lpsmsock; } int GetUseingSendThreadCount() { int cnt; int Count = 0; for( cnt=0;cnt<TRANS_THREAD_SEND_MAX;cnt++) { if ( smTransSendThreads[cnt].smThreadSock ) { Count++; } } return Count; } int GetUseingRecvThreadCount() { int cnt; int Count = 0; for( cnt=0;cnt<TRANS_THREAD_RECV_MAX;cnt++) { if ( smTransRecvThreads[cnt].smThreadSock ) { Count++; } } return Count; } int ClearTransQueSocket( smWINSOCK *lpsmsock ) { int cnt; int mCnt; EnterCriticalSection( &cTransSection ); for(cnt=smWaitRecvQuePop;cnt<smWaitRecvQuePush;cnt++) { mCnt = cnt & TRANS_WAIT_QUE_MASK; if ( smWaitRecvSocketQue[mCnt]==lpsmsock ) { smWaitRecvSocketQue[mCnt]=0; } } for(cnt=smWaitSendQuePop;cnt<smWaitSendQuePush;cnt++) { mCnt = cnt & TRANS_WAIT_QUE_MASK; if ( smWaitSendSocketQue[mCnt]==lpsmsock ) { smWaitSendSocketQue[mCnt]=0; } } if ( lpsmsock->lpsmRecvRoute ) { if ( lpsmsock->lpsmRecvRoute->smThreadSock && lpsmsock->lpsmRecvRoute->smThreadSock->smMySock==lpsmsock ) { lpsmsock->lpsmRecvRoute->TimeOut = TRUE; ResumeThread( lpsmsock->lpsmRecvRoute->hThread ); } else { lpsmsock->lpsmRecvRoute = 0; smTransClearErrorCnt++; } } if ( lpsmsock->lpsmSendRoute ) { if ( lpsmsock->lpsmSendRoute->smThreadSock && lpsmsock->lpsmSendRoute->smThreadSock->smMySock==lpsmsock ) { lpsmsock->lpsmSendRoute->TimeOut = TRUE; ResumeThread( lpsmsock->lpsmSendRoute->hThread ); } else { lpsmsock->lpsmSendRoute = 0; smTransClearErrorCnt++; } } LeaveCriticalSection( &cTransSection ); return TRUE; } #define TRANS_LOST_TIME 15000 int CheckLostTransThread() { int cnt; DWORD dwTime; DWORD dwAcessTime; smWINSOCK *lpLostSock[TRANS_THREAD_SEND_MAX+TRANS_THREAD_RECV_MAX]; int LostCount = 0; EnterCriticalSection( &cTransSection ); dwTime = GetCurrentTime(); for( cnt=0;cnt<TRANS_THREAD_RECV_MAX;cnt++) { if ( smTransRecvThreads[cnt].smThreadSock ) { if ( smTransRecvThreads[cnt].TimeOut && smTransRecvThreads[cnt].Sleep ) { ResumeThread( smTransRecvThreads[cnt].hThread ); } dwAcessTime = smTransRecvThreads[cnt].smThreadSock->dwAcessTime; if ( dwAcessTime && (dwAcessTime+TRANS_LOST_TIME)<dwTime ) { if ( smTransRecvThreads[cnt].smThreadSock->smMySock ) { lpLostSock[LostCount++] = smTransRecvThreads[cnt].smThreadSock->smMySock; } } else { if ( smTransRecvThreads[cnt].Sleep && smTransRecvThreads[cnt].smThreadSock->smMySock && (&smTransRecvThreads[cnt])!=smTransRecvThreads[cnt].smThreadSock->smMySock->lpsmRecvRoute ) { if ( dwTime>(smTransRecvThreads[cnt].dwSleepTime+(TRANS_LOST_TIME*2) ) ) { smTransRecvThreads[cnt].TimeOut = TRUE; ResumeThread( smTransRecvThreads[cnt].hThread ); } } } } } for( cnt=0;cnt<TRANS_THREAD_SEND_MAX;cnt++) { if ( smTransSendThreads[cnt].smThreadSock ) { if ( smTransSendThreads[cnt].TimeOut && smTransSendThreads[cnt].Sleep ) { ResumeThread( smTransSendThreads[cnt].hThread ); } dwAcessTime = smTransSendThreads[cnt].smThreadSock->dwAcessTime; if ( dwAcessTime && (dwAcessTime+TRANS_LOST_TIME)<dwTime ) { if ( smTransSendThreads[cnt].smThreadSock->smMySock ) { lpLostSock[LostCount++] = smTransSendThreads[cnt].smThreadSock->smMySock; } } else { if ( smTransSendThreads[cnt].Sleep && smTransSendThreads[cnt].smThreadSock->smMySock && (&smTransSendThreads[cnt])!=smTransSendThreads[cnt].smThreadSock->smMySock->lpsmSendRoute ) { if ( dwTime>(smTransSendThreads[cnt].dwSleepTime+(TRANS_LOST_TIME*2) ) ) { smTransSendThreads[cnt].TimeOut = TRUE; ResumeThread( smTransSendThreads[cnt].hThread ); } } } } } LeaveCriticalSection( &cTransSection ); for( cnt=0;cnt<LostCount;cnt++ ) { if ( lpLostSock[cnt]->sock ) lpLostSock[cnt]->CloseSocket(); } return LostCount; } smTHREAD_ROUTE *FindTransThread( int SRMode ) { int cnt; if ( SRMode ) { for( cnt=0;cnt<TRANS_THREAD_SEND_MAX;cnt++) { if ( !smTransSendThreads[cnt].smThreadSock && smTransSendThreads[cnt].Sleep ) { return &smTransSendThreads[cnt]; } } } else { for( cnt=0;cnt<TRANS_THREAD_RECV_MAX;cnt++) { if( !smTransRecvThreads[cnt].smThreadSock && smTransRecvThreads[cnt].Sleep ) { return &smTransRecvThreads[cnt]; } } } return FALSE; } smTHREAD_ROUTE *RecvThreadRoute( smWINSOCK *lpsmsock ) { smTHREAD_ROUTE *lpRoute=0; EnterCriticalSection( &cTransSection ); if ( lpsmsock->lpsmRecvRoute ) { if ( lpsmsock->lpsmRecvRoute->Sleep ) ResumeThread( lpsmsock->lpsmRecvRoute->hThread ); else { lpsmsock->WaitReadMessage = 1; ResumeThread( lpsmsock->lpsmRecvRoute->hThread ); } } else { lpRoute = FindTransThread( FALSE ); if ( lpRoute ) { lpRoute->smThreadSock = &lpsmsock->smRecvState; lpRoute->TimeOut = FALSE; lpsmsock->lpsmRecvRoute = lpRoute; ResumeThread( lpRoute->hThread ); } else { PushTransRecvSocket( lpsmsock ); } } LeaveCriticalSection( &cTransSection ); return lpRoute; } smTHREAD_ROUTE *SendThreadRoute( smWINSOCK *lpsmsock ) { smTHREAD_ROUTE *lpRoute=0; EnterCriticalSection( &cTransSection ); lpRoute = FindTransThread( TRUE ); if ( lpRoute ) { lpRoute->smThreadSock = &lpsmsock->smSendState; lpRoute->TimeOut = FALSE; lpsmsock->lpsmSendRoute = lpRoute; ResumeThread( lpRoute->hThread ); } else { PushTransSendSocket( lpsmsock ); } LeaveCriticalSection( &cTransSection ); return lpRoute; } char *ConvStrIP( DWORD dwIP ) { in_addr ia; ia.S_un.S_addr = dwIP; lstrcpy( szIP , inet_ntoa (ia) ); return szIP; } char *smGetMyIp() { PHOSTENT phe; in_addr ia; phe = gethostbyname(""); if ( phe==NULL ) { return FALSE; } ia.S_un.S_addr = ((unsigned long *)phe->h_addr)[0]; lstrcpy( szIP , inet_ntoa (ia) ); return szIP; } DWORD smGetMyIp2() { PHOSTENT phe; phe = gethostbyname(""); if ( phe==NULL ) { return FALSE; } return ((DWORD *)phe->h_addr)[0]; } smWINSOCK::smWINSOCK() { } smWINSOCK::~smWINSOCK() { } extern int ConnectPlayer( smWINSOCK *lpsmSock ); extern int DisconnectPlayer( smWINSOCK *lpsmSock ); int smWINSOCK::mesAccept( WPARAM wParam , LPARAM lParam ) { HWND hWnd = hMsgWnd; if (WSAGETSELECTERROR( lParam ) == 0) { acc_sin_len = sizeof( acc_sin ); sock = accept( (SOCKET)wParam,(struct sockaddr FAR *) &acc_sin, (int FAR *) &acc_sin_len ); if (sock < 0) { sock = 0; return -1; } if ((status = WSAAsyncSelect( sock, hWnd, WSA_READ, FD_READ | FD_WRITE | FD_CLOSE )) > 0) { closesocket( sock ); sock = 0; return -3; } lstrcpy( szIPAddr , inet_ntoa(acc_sin.sin_addr) ); ConnectDirection = FALSE; PacketSendQueCnt = 0; WaitReadMessage = 0; PostReadMessage = 0; RecvPacketCount = 0; SendPacketCount = 0; RecvPacketErrorCount = 0; SendPacketErrorCount = 0; RecvPopCount = 0; RecvPopErrorCount = 0; dwLastRecvTime = GetCurrentTime(); dwDeadLockTime = dwLastRecvTime; EncPacketSendCount = 0x0010; EncPacketRecvCount = 0; EncRecvPacketCnt = 0; dwEncPacketCode = smTRANSCODE_ENCODE_PACKET; #ifdef _PACKET_PASS_XOR bEncXor = _PACKET_PASS_XOR; bDecXor = _PACKET_PASS_XOR; #else bEncXor = 0; bDecXor = 0; #endif CreateTransThread( TRANSMODE_DUAL ); ConnectPlayer( MyPoint ); int sockBufferSize; sockBufferSize = 65536; setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (char *)&SOCKET_NODELAY, sizeof(BOOL)); setsockopt( sock , SOL_SOCKET , SO_RCVBUF , (char *)&sockBufferSize , sizeof(sockBufferSize)); setsockopt( sock , SOL_SOCKET , SO_SNDBUF , (char *)&sockBufferSize , sizeof(sockBufferSize)); return TRUE; } else { return -2; } return TRUE; } int smWINSOCK::ReconnectSock() { SOCKADDR_IN dest_sin; SOCKET msock; int status; if ( !sock ) return FALSE; closesocket( sock ); sock = 0; Sleep(100); msock = socket( AF_INET, SOCK_STREAM, 0); if (msock == INVALID_SOCKET) { return FALSE; } dest_sin.sin_family = AF_INET ; dest_sin.sin_addr.S_un.S_addr = inet_addr ( szIPAddr ); dest_sin.sin_port = htons((u_short) dwPort ) ; if (connect( msock, (PSOCKADDR) &dest_sin, sizeof( dest_sin)) ==SOCKET_ERROR) { CloseSocket(); return FALSE; } ConnectDirection = TRUE; status = WSAAsyncSelect( msock, hMsgWnd, WSA_READ, FD_READ | FD_WRITE | FD_CLOSE ); sock = msock; acc_sin.sin_addr.S_un.S_addr = inet_addr ( szIPAddr ); ClearPacketSendQue( this ); PacketSendQueCnt = 0; WaitReadMessage = 0; PostReadMessage = 0; RecvPacketCount = 0; SendPacketCount = 0; RecvPacketErrorCount = 0; SendPacketErrorCount = 0; RecvPopCount = 0; RecvPopErrorCount = 0; dwLastRecvTime = GetCurrentTime(); dwDeadLockTime = dwLastRecvTime; EncPacketSendCount = 0x0010; EncPacketRecvCount = 0; EncRecvPacketCnt = 0; return TRUE; } int smWINSOCK::ConnectSock( char *szIP , WORD port ) { SOCKADDR_IN dest_sin; SOCKET msock; int status; msock = socket( AF_INET, SOCK_STREAM, 0); if (msock == INVALID_SOCKET) { return FALSE; } dest_sin.sin_family = AF_INET ; dest_sin.sin_addr.S_un.S_addr = inet_addr ( szIP ); dest_sin.sin_port = htons((u_short) port ) ; if (connect( msock, (PSOCKADDR) &dest_sin, sizeof( dest_sin)) ==SOCKET_ERROR) { closesocket( msock ); return FALSE; } lstrcpy( szIPAddr , szIP ); ConnectDirection = TRUE; dwPort = port; status = WSAAsyncSelect( msock, hMsgWnd, WSA_READ, FD_READ | FD_WRITE | FD_CLOSE ); sock = msock; acc_sin.sin_addr.S_un.S_addr = inet_addr ( szIP ); PacketSendQueCnt = 0; WaitReadMessage = 0; PostReadMessage = 0; RecvPacketCount = 0; SendPacketCount = 0; RecvPacketErrorCount = 0; SendPacketErrorCount = 0; RecvPopCount = 0; RecvPopErrorCount = 0; dwLastRecvTime = GetCurrentTime(); dwDeadLockTime = dwLastRecvTime; EncPacketSendCount = 0x0010; EncPacketRecvCount = 0; EncRecvPacketCnt = 0; dwEncPacketCode = smTRANSCODE_ENCODE_PACKET; #ifdef _PACKET_PASS_XOR bEncXor = _PACKET_PASS_XOR; bDecXor = _PACKET_PASS_XOR; #else bEncXor = 0; bDecXor = 0; #endif CreateTransThread( TRANSMODE_DUAL ); int sockBufferSizeRecv; int sockBufferSizeSend; #ifdef _W_SERVER sockBufferSizeRecv = 65536; sockBufferSizeSend = 65536; #else sockBufferSizeRecv = 16384; sockBufferSizeSend = 32768; #endif setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (char *)&SOCKET_NODELAY, sizeof(BOOL)); setsockopt( sock , SOL_SOCKET , SO_RCVBUF , (char *)&sockBufferSizeRecv , sizeof(sockBufferSizeRecv)); setsockopt( sock , SOL_SOCKET , SO_SNDBUF , (char *)&sockBufferSizeSend , sizeof(sockBufferSizeSend)); return TRUE; } int smWINSOCK::CloseSocket() { DWORD dwExitCode; DisconnectPlayer( MyPoint ); if ( !smTransThreadMode ) { smSendState.dwExitThread = TRUE; ResumeThread( hSendThread ); smRecvState.dwExitThread = TRUE; ResumeThread( hRecvThread ); WaitForSingleObject( hSendThread , 500 ); GetExitCodeThread( hSendThread , &dwExitCode ); if ( dwExitCode==STILL_ACTIVE ) { smSendState.dwExitThread = TRUE; ResumeThread( hSendThread ); WaitForSingleObject( hSendThread , 500 ); GetExitCodeThread( hSendThread , &dwExitCode ); if ( dwExitCode==STILL_ACTIVE ) { TerminateThread( hSendThread , 1 ); } } WaitForSingleObject( hRecvThread , 500 ); GetExitCodeThread( hRecvThread , &dwExitCode ); if ( dwExitCode==STILL_ACTIVE ) { smRecvState.dwExitThread = TRUE; ResumeThread( hRecvThread ); WaitForSingleObject( hRecvThread , 500 ); GetExitCodeThread( hRecvThread , &dwExitCode ); if ( dwExitCode==STILL_ACTIVE ) { TerminateThread( hRecvThread , 1 ); } } } else { ClearTransQueSocket( MyPoint ); } ClearPacketSendQue( this ); closesocket( sock ); sock = 0; smRecvState.Sock = 0; smSendState.Sock = 0; return TRUE; } int smWINSOCK::mesReceve( LPARAM lParam ) { HWND hWnd = hMsgWnd; switch ( WSAGETSELECTEVENT(lParam) ) { case FD_READ: smRecvState.NewData ++; if ( !smTransThreadMode ) { if ( !smTransTurbRcvMode ) WaitReadMessage++; PostReadMessage++; dwRecvSusCount = ResumeThread( hRecvThread ); } else { RecvThreadRoute( MyPoint ); } break; case FD_WRITE: if ( smTransThreadMode && lpsmSendRoute && smSendState.Sucess==FALSE ) { ResumeThread( lpsmSendRoute->hThread ); } if ( !smTransThreadMode && smSendState.Sleep==TRUE && smSendState.Sucess==FALSE ) { if ( hSendThread ) ResumeThread( hSendThread ); } smSendState.SendReady = TRUE; break; case FD_CLOSE: CloseSocket(); break; } return TRUE; } extern char *szNetLogFile; extern FILE *fpNetLog; int smWINSOCK::Send( char *SendData , int len , int ImpMode ) { EnterCriticalSection( &cSendingSection ); if ( smSendState.Sucess==TRUE ) { memcpy( smSendState.Buff , SendData , len ); smSendState.BuffCnt = len; smSendState.Sucess = FALSE; smSendState.NewData = TRUE; LeaveCriticalSection( &cSendingSection ); if ( smTransThreadMode ) { SendThreadRoute( MyPoint ); } else { ResumeThread( hSendThread ); } return TRUE; } else { if ( smTransThreadMode ) { if ( lpsmSendRoute && lpsmSendRoute->Sleep ) { ResumeThread( lpsmSendRoute->hThread ); } } else { if ( smSendState.Sleep ) { ResumeThread( hSendThread ); } } } LeaveCriticalSection( &cSendingSection ); if ( ImpMode ) { PushPacketSendQue( this , SendData , len ); return TRUE; } return FALSE; } int smWINSOCK::Send2( char *SendData , int len , int ImpMode ) { #ifdef __W_SERVER return Send( SendData , len , ImpMode ); #else char EncData[smSOCKBUFF_SIZE]; DWORD dwKey; if ( len<(smSOCKBUFF_SIZE-16) ) { dwKey = ((acc_sin.sin_addr.S_un.S_addr)<<8)&0xFFFF; dwKey |= rand()%255; if ( fnEncodePacket ) { fnEncodePacket( dwKey , EncPacketSendCount , (BYTE *)SendData , (BYTE *)EncData ); } else { dwKey |= dwEncPacketCode; EncodePacket2( dwKey , EncPacketSendCount , (BYTE *)SendData , (BYTE *)EncData , bEncXor ); } EncPacketSendCount++; return Send( EncData , len+8 , ImpMode ); } else { return Send( SendData , len , ImpMode ); } return FALSE; #endif } int smWINSOCK::Send3( char *SendData , int len , int ImpMode ) { #ifdef __W_SERVER return Send( SendData , len , ImpMode ); #else char EncData[smSOCKBUFF_SIZE]; DWORD dwKey; if ( len<(smSOCKBUFF_SIZE-16) ) { dwKey = ((acc_sin.sin_addr.S_un.S_addr)<<8)&0xFFFF; dwKey |= rand()%255; dwKey |= smTRANSCODE_ENCODE_PACKET; EncodePacket( dwKey , EncPacketSendCount , (BYTE *)SendData , (BYTE *)EncData ); EncPacketSendCount++; return Send( EncData , len+8 , ImpMode ); } else { return Send( SendData , len , ImpMode ); } return FALSE; #endif } int smWINSOCK::CheckEncRecvPacket( int RecvCount ) { int cnt; int mCnt; for( cnt=EncRecvPacketCnt-ENC_PACKET_REC_MAX;cnt<EncRecvPacketCnt;cnt++ ) { if ( cnt>=0 ) { mCnt = cnt&ENC_PACKET_REC_MASK; if ( EncRecvPackets[mCnt]==RecvCount ) { return TRUE; } } } return FALSE; } int smWINSOCK::PushEncRecvPacket( int RecvCount ) { int mCnt; mCnt = EncRecvPacketCnt&ENC_PACKET_REC_MASK; EncRecvPackets[mCnt]=RecvCount; EncRecvPacketCnt++; if ( EncPacketRecvCount<RecvCount ) EncPacketRecvCount = RecvCount; return EncRecvPacketCnt; } struct smPLAYDATA { int size; int code; int x,y,z; int ax,ay,az; int frame; }; DWORD WINAPI smWinsockRecvThreadProc( void *pInfo ) { smTHREADSOCK *SockInfo; int len; char szBuff[smSOCKBUFF_SIZE]; char *Buff; int RecvLen; int BuffCnt; int BuffSize; int RecvCode; HANDLE hThread; int BuffOver; DWORD dwRcvSize; SockInfo = (smTHREADSOCK *) pInfo; RecvLen = 0; BuffCnt = 0; BuffOver = 0; BuffSize = smSOCKBUFF_SIZE; SockInfo->Sucess = FALSE; SockInfo->dwExitThread = FALSE; hThread = GetCurrentThread(); while(1) { if ( SockInfo->dwExitThread ) { ExitThread( TRUE ); return TRUE; } if ( SockInfo->NewData==0 && !BuffOver ) { SockInfo->Sleep = TRUE; if ( smTransTurbRcvMode ) { Sleep(smTransTurbRcvMode); } else SuspendThread( hThread ); SockInfo->Sleep = FALSE; } if ( SockInfo->dwExitThread ) { ExitThread( TRUE ); return TRUE; } dwRcvSize = 0; ioctlsocket( SockInfo->Sock , FIONREAD , &dwRcvSize ); if ( dwRcvSize<=0 ) { SockInfo->NewData = 0; BuffOver = 0; SockInfo->smMySock->WaitReadMessage = 0; continue; } BuffOver = 0; if ( SockInfo->smMySock && SockInfo->smMySock->PostReadMessage>0 ) { SockInfo->smMySock->PostReadMessage--; BuffOver ++; } Buff = szBuff + BuffCnt; BuffSize = smSOCKBUFF_SIZE - BuffCnt; len = recv( SockInfo->Sock , Buff , BuffSize , 0 ); if ( len>0 ) { if ( BuffSize<=len ) smRcvBuffOverCount ++; BuffOver ++; } if ( BuffCnt<8 ) RecvLen = smSOCKBUFF_SIZE-32; if ( BuffCnt<8 && (BuffCnt+len)>=8 ) { RecvLen = (((int *)szBuff)[0]); RecvCode = (((int *)szBuff)[1]); } if ( len>0 ) { SockInfo->smMySock->RecvPacketCount++; BuffCnt += len; SockInfo->NewData--; } else { if ( SockInfo->smMySock->WaitReadMessage>0 ) SockInfo->smMySock->RecvPopErrorCount++; else SockInfo->smMySock->RecvPacketErrorCount++; if ( len==0 ) { SockInfo->smMySock->WaitReadMessage = -1; } else SockInfo->smMySock->WaitReadMessage = 0; SockInfo->NewData = 0; #ifdef _DEBUG_SOCKET if (smConfig.DebugMode) { char TempBuff[128]; wsprintf(TempBuff, "Recv %d is the error", WSAGetLastError()); } #endif continue; } while(1) { if ( BuffCnt>=RecvLen ) { memcpy( SockInfo->Buff , szBuff , RecvLen ); SockInfo->Buff[ BuffCnt ] = 0; SockInfo->RecvLen = RecvLen; SockInfo->RecvCode = RecvCode; SockInfo->BuffCnt = BuffCnt; SockInfo->Sucess = TRUE; SockInfo->smMySock->dwLastRecvTime = GetCurrentTime(); SendMessage(hMsgWnd, SWM_RECVSUCCESS, (WPARAM)SockInfo, 0); if ( RecvLen<BuffCnt ) { BuffCnt -= RecvLen; memcpy( szBuff , szBuff+RecvLen , BuffCnt ); if ( BuffCnt>=8 ) { RecvLen = (((int *)szBuff)[0]); RecvCode = (((int *)szBuff)[1]); BuffSize = smSOCKBUFF_SIZE - BuffCnt; } else{ RecvLen = 0; RecvCode = 0; break; } } else { RecvLen = 0; BuffCnt = 0; if ( SockInfo->smMySock->WaitReadMessage>0 ) { SockInfo->NewData = TRUE; SockInfo->smMySock->WaitReadMessage --; break; } break; } } else break; } } ExitThread( TRUE ); return TRUE; } DWORD WINAPI smWinsockSendThreadProc( void *pInfo ) { smTHREADSOCK *SockInfo; char *szBuff; int BuffCnt; int stats; int BuffLen; HANDLE hThread; SockInfo = (smTHREADSOCK *) pInfo; hThread = GetCurrentThread(); SockInfo->dwExitThread = FALSE; while(1) { if ( SockInfo->dwExitThread ) { ExitThread( TRUE ); return TRUE; } if ( SockInfo->NewData==FALSE ) { SockInfo->Sleep = TRUE; SuspendThread( hThread ); SockInfo->Sleep = FALSE; } if ( SockInfo->dwExitThread ) { ExitThread( TRUE ); return TRUE; } if ( SockInfo->NewData ) { BuffLen = SockInfo->BuffCnt; BuffCnt = 0; SockInfo->NewData = FALSE; } szBuff = SockInfo->Buff + BuffCnt; stats = send( SockInfo->Sock , szBuff , BuffLen , 0 ); if ( stats != SOCKET_ERROR ) { SockInfo->smMySock->SendPacketCount++; if ( stats!=BuffLen ) { stats = stats; } BuffCnt +=stats; BuffLen -=stats; if ( BuffLen <= 0 ) { SockInfo->Sucess = TRUE; SucessPacketSending( SockInfo ); } } else { SockInfo->smMySock->SendPacketErrorCount++; #ifdef _DEBUG_SOCKET if (smConfig.DebugMode) { char TempBuff[128]; wsprintf(TempBuff, "Send %d is the error", WSAGetLastError()); AddChatBuff(TempBuff); } #endif } } ExitThread( TRUE ); return TRUE; } int smWINSOCK::CreateTransThread( int Mode ) { DWORD dwRecvMsgId; DWORD dwSendMsgId; hRecvThread = 0; hSendThread = 0; smRecvState.Sock = sock; smSendState.Sock = sock; smSendState.Sucess = TRUE; smSendState.SendReady = TRUE; smSendState.Sleep = FALSE; if ( !smTransThreadMode ) { if ( Mode&TRANSMODE_RECV ) { hRecvThread = CreateThread( NULL , 0, smWinsockRecvThreadProc , &smRecvState , 0, &dwRecvMsgId ); SetThreadPriority( hRecvThread , THREAD_PRIORITY_ABOVE_NORMAL ); } if ( Mode&TRANSMODE_SEND ) { hSendThread = CreateThread( NULL , 0, smWinsockSendThreadProc , &smSendState , 0, &dwSendMsgId ); SetThreadPriority( hSendThread , THREAD_PRIORITY_ABOVE_NORMAL ); } } ExtData1 = 0; ExtData2 = 0; ExtData3 = 0; ExtData4 = 0; return TRUE; } void InitGameSocket( int ThreadMode ) { int cnt; WSADATA WSAData; char szTemp[80]; int status; hMsgWnd = hwnd; if ((status = WSAStartup(MAKEWORD(1,1), &WSAData)) != 0) { sprintf(szTemp, "%d is the err", status); MessageBox( hMsgWnd, szTemp, "Error", MB_OK); } for(cnt=0;cnt<CONNECTMAX;cnt++) { smWSock[cnt].sock = 0; smWSock[cnt].MyPoint = &smWSock[cnt]; smWSock[cnt].MyNum = cnt; smWSock[cnt].smRecvState.smMySock = &smWSock[cnt]; smWSock[cnt].smSendState.smMySock = &smWSock[cnt]; smWSock[cnt].lpsmRecvRoute = 0; smWSock[cnt].lpsmSendRoute = 0; } if ( ThreadMode ) { smTransThreadMode = ThreadMode; InitTransThread(); } else { smTransThreadMode = 0; } InitializeCriticalSection( &cSocketSection ); InitializeCriticalSection( &cSendingSection ); InitPacketSendQue(); } smWINSOCK *smConnectSock( char *szIP , WORD port ) { int cnt; smWINSOCK *lpsmSock = 0; EnterCriticalSection( &cSocketSection ); if ( smTransThreadMode ) { for(cnt=0;cnt<CONNECTMAX;cnt++) { if ( smWSock[cnt].sock==0 && !smWSock[cnt].lpsmRecvRoute && !smWSock[cnt].lpsmSendRoute) { if ( smWSock[cnt].ConnectSock( szIP, port )!=FALSE ) lpsmSock = &smWSock[cnt]; break; } } } else { for(cnt=0;cnt<CONNECTMAX;cnt++) { if ( smWSock[cnt].sock==0 ) { if ( smWSock[cnt].ConnectSock( szIP, port )!=FALSE ) lpsmSock = &smWSock[cnt]; break; } } } LeaveCriticalSection( &cSocketSection ); return lpsmSock; } smWINSOCK *smConnectSock3( char *szIP , WORD port ) { int cnt; smWINSOCK *lpsmSock = 0; if ( smTransThreadMode ) { for(cnt=CONNECTMAX-1;cnt>=0;cnt--) { if ( smWSock[cnt].sock==0 && !smWSock[cnt].lpsmRecvRoute && !smWSock[cnt].lpsmSendRoute) { if ( smWSock[cnt].ConnectSock( szIP, port )!=FALSE ) lpsmSock = &smWSock[cnt]; break; } } } else { for(cnt=CONNECTMAX-1;cnt>=0;cnt--) { if ( smWSock[cnt].sock==0 ) { if ( smWSock[cnt].ConnectSock( szIP, port )!=FALSE ) lpsmSock = &smWSock[cnt]; break; } } } return lpsmSock; } static int MsgRetryCount = 0; int smCheckWaitMessage( smWINSOCK *lpsmSock ) { EnterCriticalSection( &cSocketSection ); if ( lpsmSock && lpsmSock->WaitReadMessage>0 && lpsmSock->smRecvState.Sleep ) { lpsmSock->WaitReadMessage--; lpsmSock->mesReceve( FD_READ ); lpsmSock->RecvPopCount ++; } LeaveCriticalSection( &cSocketSection ); return TRUE; } int smCheckWaitMessage() { int cnt; int MsgCnt; MsgCnt = 0; EnterCriticalSection( &cSocketSection ); if ( smTransThreadMode ) { for(cnt=0;cnt<CONNECTMAX;cnt++) { if ( smWSock[cnt].sock ) { if ( smWSock[cnt].WaitReadMessage>0 ) { smWSock[cnt].WaitReadMessage--; smWSock[cnt].mesReceve( 1 ); smWSock[cnt].RecvPopCount ++; MsgRetryCount++; MsgCnt++; } if ( smWSock[cnt].PacketSendQueCnt && !smWSock[cnt].lpsmSendRoute ) { PopSendPacketQue( &smWSock[cnt] ); } if ( !smWSock[cnt].smSendState.Sucess ) { if ( smWSock[cnt].lpsmSendRoute && smWSock[cnt].lpsmSendRoute->Sleep ) { ResumeThread( smWSock[cnt].lpsmSendRoute->hThread ); } } } } } else { for(cnt=0;cnt<CONNECTMAX;cnt++) { if ( smWSock[cnt].sock ) { if ( smWSock[cnt].WaitReadMessage>0 && smWSock[cnt].smRecvState.Sleep ) { smWSock[cnt].WaitReadMessage--; smWSock[cnt].mesReceve( 1 ); smWSock[cnt].RecvPopCount ++; MsgRetryCount++; MsgCnt++; } if ( smWSock[cnt].PacketSendQueCnt && smWSock[cnt].smSendState.Sleep ) { PopSendPacketQue( &smWSock[cnt] ); } if ( !smWSock[cnt].smSendState.Sucess ) { if ( smWSock[cnt].smSendState.Sleep ) { ResumeThread( smWSock[cnt].hSendThread ); } } } } } LeaveCriticalSection( &cSocketSection ); return MsgCnt; } int smReconnectSock( smWINSOCK *lpsmSock ) { int res; EnterCriticalSection( &cSocketSection ); res = lpsmSock->ReconnectSock(); LeaveCriticalSection( &cSocketSection ); return res; } smWINSOCK *smConnectSock2( char *szIP , WORD port ) { int cnt; smWINSOCK *lpsmSock = 0; smWINSOCK *lpFindsmSock; DWORD dwIP; EnterCriticalSection( &cSocketSection ); dwIP = inet_addr ( szIP ); lpFindsmSock = smFindSocket2( dwIP ); if ( !lpsmSock ) { if ( smTransThreadMode ) { for(cnt=0;cnt<CONNECTMAX;cnt++) { if ( smWSock[cnt].sock==0 && !smWSock[cnt].lpsmRecvRoute && !smWSock[cnt].lpsmSendRoute) { lpsmSock = &smWSock[cnt]; } } } else { for(cnt=0;cnt<CONNECTMAX;cnt++) { if ( smWSock[cnt].sock==0 ) { if ( smWSock[cnt].ConnectSock( szIP, port )!=FALSE ) lpsmSock = &smWSock[cnt]; break; } } } } LeaveCriticalSection( &cSocketSection ); return lpsmSock; } smWINSOCK *smFindSocket( char *szIP ) { int cnt; for(cnt=0;cnt<CONNECTMAX;cnt++) { if ( smWSock[cnt].sock && lstrcmpi( smWSock[cnt].szIPAddr , szIP )==0 ) return &smWSock[cnt]; } return NULL; } smWINSOCK *smFindSocket2( DWORD dwIP ) { int cnt; for(cnt=0;cnt<CONNECTMAX;cnt++) { if ( smWSock[cnt].sock && smWSock[cnt].acc_sin.sin_addr.S_un.S_addr==dwIP) return &smWSock[cnt]; } return NULL; } int WSAMessage_Accept( DWORD wParam , DWORD lParam ) { int cnt; int Result; Result = FALSE; EnterCriticalSection( &cSocketSection ); if ( smTransThreadMode ) { for(cnt=0;cnt<CONNECTMAX;cnt++) { if ( smWSock[cnt].sock==0 && !smWSock[cnt].lpsmRecvRoute && !smWSock[cnt].lpsmSendRoute) { Result = smWSock[cnt].mesAccept( wParam, lParam ); break; } } } else { for(cnt=0;cnt<CONNECTMAX;cnt++) { if ( smWSock[cnt].sock==0 ) { Result = smWSock[cnt].mesAccept( wParam, lParam ); break; } } } if ( Result==FALSE ) { SOCKADDR_IN acc_sin; int acc_sin_len; SOCKET sock; acc_sin_len = sizeof( acc_sin ); sock = accept( (SOCKET)wParam,(struct sockaddr FAR *) &acc_sin, (int FAR *) &acc_sin_len ); closesocket( sock ); } LeaveCriticalSection( &cSocketSection ); return Result; } void smEnterCSectionSocket() { EnterCriticalSection( &cSocketSection ); } void smLeaveCSectionSocket() { LeaveCriticalSection( &cSocketSection ); } int WSAMessage_Read( DWORD wParam , DWORD lParam ) { int cnt; int Result; Result = FALSE; EnterCriticalSection( &cSocketSection ); for(cnt=0;cnt<CONNECTMAX;cnt++) { if ( smWSock[cnt].sock==(SOCKET)wParam && smWSock[cnt].WaitReadMessage>=0 ) { Result = smWSock[cnt].mesReceve( lParam ); break; } } LeaveCriticalSection( &cSocketSection ); return Result; } static int status; static char szBuff[128]; static SOCKADDR_IN local_sin; static SOCKADDR_IN acc_sin; static int acc_sin_len; #define MAX_PENDING_CONNECTS 32 #define NO_FLAGS_SET 0 #define MY_MSG_LENGTH 80 SOCKET BindSock; u_short BindPort = 23; BOOL FillAddr( HWND hWnd, PSOCKADDR_IN psin, BOOL bClient ); void InitBindSock( int port ) { HWND hWnd = hwnd; SOCKET sock; BindPort = port; sock = socket( AF_INET, SOCK_STREAM, 0); if (sock == INVALID_SOCKET) { MessageBox(hWnd, "socket() failed", "Error", MB_OK); return; } if (!FillAddr( hWnd, &local_sin, FALSE)) { closesocket( sock ); return; } if (bind( sock, (struct sockaddr FAR *) &local_sin, sizeof(local_sin)) == SOCKET_ERROR) { sprintf(szBuff, "%d is the error", WSAGetLastError()); MessageBox(hWnd, szBuff, "bind(sock) failed", MB_OK); closesocket( sock ); return; } if (listen( sock, MAX_PENDING_CONNECTS ) < 0) { sprintf(szBuff, "%d is the error", WSAGetLastError()); MessageBox(hWnd, szBuff, "listen(sock) failed", MB_OK); return; } if ((status = WSAAsyncSelect( sock, hWnd, WSA_ACCEPT, FD_ACCEPT)) > 0) { wsprintf( szBuff, "%d (0x%x)"); MessageBox( hWnd, "Error on WSAAsyncSelect()", szBuff, MB_OK); SetWindowText( hWnd, "Async listen cancelled"); closesocket( sock ); } BindSock = sock; } void CloseBindSock() { closesocket( BindSock ); } BOOL FillAddr( HWND hWnd, PSOCKADDR_IN psin, BOOL bClient) { DWORD dwSize; PHOSTENT phe; char szTemp[200]; psin->sin_family = AF_INET; psin->sin_port = htons(BindPort); if (bClient) { phe = gethostbyname(szBuff); if (phe == NULL) { sprintf(szTemp, "%d is the error. Make sure '%s' is listed in the hosts file.", WSAGetLastError(), szBuff); MessageBox(hWnd, szTemp, "gethostbyname() failed.", MB_OK); return FALSE; } memcpy((char FAR *)&(psin->sin_addr), phe->h_addr, phe->h_length); } else { dwSize = sizeof(szBuff); gethostname(szBuff, dwSize); psin->sin_addr.s_addr = INADDR_ANY; } return TRUE; } int EncodePacket( DWORD dwKey , DWORD PacketCnt , BYTE *lpPacket , BYTE *lpEncBuff ) { int cnt; BYTE ch; BYTE bNear; DWORD rKey,sKey; int len; WORD wTotal; sKey = (dwKey<<16)|(PacketCnt&0xFFFF); len = ((int *)lpPacket)[0]; ((int *)lpEncBuff)[0] = len+8; ((int *)lpEncBuff)[2] = sKey; rKey = sKey>>4; rKey += len; bNear = (BYTE)(PacketCnt+dwKey); wTotal = 0; for( cnt=4;cnt<len;cnt++ ) { wTotal += lpPacket[cnt]; ch = lpPacket[cnt] ^ ((BYTE)rKey) ^ bNear; lpEncBuff[cnt+8] = ch; rKey += cnt*(cnt>>1); #ifdef _PACKET_PASS_XOR bNear = ch^_PACKET_PASS_XOR; #else bNear = ch; #endif } ((int *)lpEncBuff)[1] = (dwKey&0xFFFF0000)|wTotal; return TRUE; } DWORD DecodePacket( BYTE *lpPacket , BYTE *lpDecBuff ) { int cnt; BYTE ch; BYTE ch2; BYTE bNear; DWORD rKey,sKey; int len; DWORD dwKey; DWORD PacketCnt; WORD wTotal; len = ((int *)lpPacket)[0]-8; sKey = ((int *)lpPacket)[2]; dwKey = sKey>>16; PacketCnt = sKey&0xFFFF; rKey = sKey>>4; rKey += len; bNear = (BYTE)(PacketCnt+dwKey); wTotal = 0; ((int *)lpDecBuff)[0] = len; for( cnt=4;cnt<len;cnt++ ) { ch = lpPacket[cnt+8]; ch2= ch; ch ^=((BYTE)rKey)^bNear; lpDecBuff[cnt] = ch; rKey += cnt*(cnt>>1); #ifdef _PACKET_PASS_XOR bNear = ch2^_PACKET_PASS_XOR; #else bNear = ch2; #endif wTotal += ch; } if ( (((int *)lpPacket)[1]&0xFFFF)!=wTotal ) return NULL; return sKey; } int EncodePacket2( DWORD dwKey , DWORD PacketCnt , BYTE *lpPacket , BYTE *lpEncBuff , BYTE bEncXor ) { int cnt; BYTE ch; BYTE bNear; DWORD rKey,sKey; int len; WORD wTotal; dwKey |=smTRANSCODE_ENCODE_PACKET; sKey = (dwKey<<16)|(PacketCnt&0xFFFF); len = ((int *)lpPacket)[0]; ((int *)lpEncBuff)[0] = len+8; ((int *)lpEncBuff)[2] = sKey; rKey = sKey>>4; rKey += len; bNear = (BYTE)(PacketCnt+dwKey); wTotal = 0; for( cnt=4;cnt<len;cnt++ ) { wTotal += lpPacket[cnt]; ch = lpPacket[cnt] ^ ((BYTE)rKey) ^ bNear; lpEncBuff[cnt+8] = ch; rKey += cnt*(cnt>>1); bNear = ch^bEncXor; } ((int *)lpEncBuff)[1] = (dwKey&0xFFFF0000)|wTotal; return TRUE; } DWORD DecodePacket2( BYTE *lpPacket , BYTE *lpDecBuff , BYTE bDecXor ) { int cnt; BYTE ch; BYTE ch2; BYTE bNear; DWORD rKey,sKey; int len; DWORD dwKey; DWORD PacketCnt; WORD wTotal; len = ((int *)lpPacket)[0]-8; sKey = ((int *)lpPacket)[2]; dwKey = sKey>>16; PacketCnt = sKey&0xFFFF; rKey = sKey>>4; rKey += len; bNear = (BYTE)(PacketCnt+dwKey); wTotal = 0; ((int *)lpDecBuff)[0] = len; for( cnt=4;cnt<len;cnt++ ) { ch = lpPacket[cnt+8]; ch2= ch; ch ^=((BYTE)rKey)^bNear; lpDecBuff[cnt] = ch; rKey += cnt*(cnt>>1); bNear = ch2^bDecXor; wTotal += ch; } if ( (((int *)lpPacket)[1]&0xFFFF)!=wTotal ) return NULL; return sKey; } LPFN_EncodePacket fnEncodePacket =0; LPFN_DecodePacket fnDecodePacket =0; #ifdef _W_SERVER BYTE smwsock_EncodePacket_AsmCode[ENC_PACKET_ASM_SIZE] = { 0x8b,0x4c,0x24,0x04,0x53,0x8b,0x5c,0x24,0x0c,0x81,0xc9,0x00,0x00,0x01,0x80,0x55, 0x8b,0xd3,0x81,0xe2,0xff,0xff,0x00,0x00,0x8b,0xc1,0xc1,0xe0,0x10,0x0b,0xc2,0x8b, 0x54,0x24,0x14,0x56,0x8b,0x32,0x57,0x8b,0x7c,0x24,0x20,0x89,0x47,0x08,0xc1,0xe8, 0x04,0x8d,0x56,0x08,0x03,0xc6,0x89,0x17,0x8b,0xd0,0xb8,0x04,0x00,0x00,0x00,0x02, 0xd9,0x33,0xed,0x3b,0xf0,0x89,0x4c,0x24,0x14,0x7e,0x35,0xeb,0x03,0x8d,0x49,0x00, 0x8b,0x4c,0x24,0x1c,0x8a,0x0c,0x08,0x66,0x0f,0xb6,0xf9,0x32,0xca,0x32,0xcb,0x8b, 0xd8,0xd1,0xfb,0x0f,0xaf,0xd8,0x03,0xef,0x8b,0x7c,0x24,0x20,0x88,0x4c,0x07,0x08, 0x03,0xd3,0x80,0xf1,0x33,0x40,0x3b,0xc6,0x8a,0xd9,0x7c,0xd4,0x8b,0x4c,0x24,0x14, 0x0f,0xb7,0xd5,0x81,0xe1,0x00,0x00,0xff,0xff,0x0b,0xca,0x89,0x4f,0x04,0x5f,0x5e, 0x5d,0xb8,0x01,0x00,0x00,0x00,0x5b,0xc3,0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90, 0x83,0xec,0x0c,0x56,0x57,0x8b,0x7c,0x24,0x18,0x8b,0x57,0x08,0x8b,0x37,0x8b,0xc2, 0xc1,0xe8,0x10,0x02,0xc2,0x83,0xee,0x08,0x88,0x44,0x24,0x18,0x8b,0x44,0x24,0x1c, 0x8b,0xca,0xc1,0xe9,0x04,0x89,0x30,0x03,0xce,0xb8,0x04,0x00,0x00,0x00,0x3b,0xf0, 0x89,0x54,0x24,0x10,0x89,0x4c,0x24,0x08,0xc7,0x44,0x24,0x0c,0x00,0x00,0x00,0x00, 0x7e,0x52,0x53,0x55,0xeb,0x0a,0x8b,0x4c,0x24,0x10,0x8d,0x9b,0x00,0x00,0x00,0x00, 0x8a,0x54,0x07,0x08,0x8a,0x5c,0x24,0x20,0x8b,0x6c,0x24,0x10,0x32,0xca,0x32,0xcb, 0x8b,0x5c,0x24,0x24,0x88,0x0c,0x18,0x8b,0xd8,0xd1,0xfb,0x0f,0xaf,0xd8,0x66,0x0f, 0xb6,0xc9,0x03,0xeb,0x8b,0x5c,0x24,0x14,0x80,0xf2,0x33,0x89,0x6c,0x24,0x10,0x88, 0x54,0x24,0x20,0x03,0xd9,0x40,0x3b,0xc6,0x89,0x5c,0x24,0x14,0x7c,0xb8,0x8b,0x54, 0x24,0x18,0x5d,0x5b,0x66,0x8b,0x4c,0x24,0x0c,0x33,0xc0,0x66,0x39,0x4f,0x04,0x5f, 0x0f,0x95,0xc0,0x5e,0x48,0x23,0xc2,0x83,0xc4,0x0c,0xc3,0x90,0x90,0x90,0x90,0x90 }; #else BYTE smwsock_EncodePacket_AsmCode[ENC_PACKET_ASM_SIZE]; #endif
  7. tem uma restrição, só poderei usar o BÁSICO DO BÁSICO DO ULTRA BÁSICO ex: #include <iostream> #include <math.h> using namespace std; int main () { int A, B, inv; cout << "Digite o valor A: "; cin >> A; cout << "Digite o valor B: "; cin >> B; cout << "\nO valor invertido e: " << Area; }
  8. boa noite amigos eu praticamente não tenho conhecimento em c++ mas preciso fazer uma modificação para fazer o mame gravar videos.mng ja com audio dentro da pasta snap eu achei a parte do codigo responsável por isso mas não sei por onde começar /*************************************************************************** video.c Core MAME video routines. Copyright Nicola Salmoria and the MAME Team. Visit http://mamedev.org for licensing and usage restrictions. ***************************************************************************/ #include "driver.h" #include "profiler.h" #include "png.h" #include "debugger.h" #include "rendutil.h" #include "ui.h" #include "aviio.h" #include "deprecat.h" #include "snap.lh" /*************************************************************************** DEBUGGING ***************************************************************************/ #define LOG_THROTTLE (0) #define VERBOSE (0) #define LOG_PARTIAL_UPDATES(x) do { if (VERBOSE) logerror x; } while (0) /*************************************************************************** CONSTANTS ***************************************************************************/ #define SUBSECONDS_PER_SPEED_UPDATE (ATTOSECONDS_PER_SECOND / 4) #define PAUSED_REFRESH_RATE (30) #define MAX_VBLANK_CALLBACKS (10) #define DEFAULT_FRAME_RATE 60 #define DEFAULT_FRAME_PERIOD ATTOTIME_IN_HZ(DEFAULT_FRAME_RATE) /*************************************************************************** TYPE DEFINITIONS ***************************************************************************/ typedef struct _screen_state screen_state; struct _screen_state { /* dimensions */ int width; /* current width (HTOTAL) */ int height; /* current height (VTOTAL) */ rectangle visarea; /* current visible area (HBLANK end/start, VBLANK end/start) */ /* textures and bitmaps */ render_texture * texture[2]; /* 2x textures for the screen bitmap */ bitmap_t * bitmap[2]; /* 2x bitmaps for rendering */ UINT8 curbitmap; /* current bitmap index */ UINT8 curtexture; /* current texture index */ INT32 texture_format; /* texture format of bitmap for this screen */ UINT8 changed; /* has this bitmap changed? */ INT32 last_partial_scan; /* scanline of last partial update */ /* screen timing */ attoseconds_t frame_period; /* attoseconds per frame */ attoseconds_t scantime; /* attoseconds per scanline */ attoseconds_t pixeltime; /* attoseconds per pixel */ attoseconds_t vblank_period; /* attoseconds per VBLANK period */ attotime vblank_start_time; /* time of last VBLANK start */ attotime vblank_end_time; /* time of last VBLANK end */ emu_timer * vblank_begin_timer; /* timer to signal VBLANK start */ emu_timer * vblank_end_timer; /* timer to signal VBLANK end */ emu_timer * scanline0_timer; /* scanline 0 timer */ emu_timer * scanline_timer; /* scanline timer */ UINT64 frame_number; /* the current frame number */ /* screen specific VBLANK callbacks */ vblank_state_changed_func vblank_callback[MAX_VBLANK_CALLBACKS]; /* the array of callbacks */ void * vblank_callback_param[MAX_VBLANK_CALLBACKS]; /* array of parameters */ }; typedef struct _video_global video_global; struct _video_global { /* screenless systems */ emu_timer * screenless_frame_timer; /* timer to signal VBLANK start */ /* throttling calculations */ osd_ticks_t throttle_last_ticks; /* osd_ticks the last call to throttle */ attotime throttle_realtime; /* real time the last call to throttle */ attotime throttle_emutime; /* emulated time the last call to throttle */ UINT32 throttle_history; /* history of frames where we were fast enough */ /* dynamic speed computation */ osd_ticks_t speed_last_realtime; /* real time at the last speed calculation */ attotime speed_last_emutime; /* emulated time at the last speed calculation */ double speed_percent; /* most recent speed percentage */ UINT32 partial_updates_this_frame;/* partial update counter this frame */ /* overall speed computation */ UINT32 overall_real_seconds; /* accumulated real seconds at normal speed */ osd_ticks_t overall_real_ticks; /* accumulated real ticks at normal speed */ attotime overall_emutime; /* accumulated emulated time at normal speed */ UINT32 overall_valid_counter; /* number of consecutive valid time periods */ /* configuration */ UINT8 throttle; /* flag: TRUE if we're currently throttled */ UINT8 fastforward; /* flag: TRUE if we're currently fast-forwarding */ UINT32 seconds_to_run; /* number of seconds to run before quitting */ UINT8 auto_frameskip; /* flag: TRUE if we're automatically frameskipping */ UINT32 speed; /* overall speed (*100) */ /* frameskipping */ UINT8 empty_skip_count; /* number of empty frames we have skipped */ UINT8 frameskip_level; /* current frameskip level */ UINT8 frameskip_counter; /* counter that counts through the frameskip steps */ INT8 frameskip_adjust; UINT8 skipping_this_frame; /* flag: TRUE if we are skipping the current frame */ osd_ticks_t average_oversleep; /* average number of ticks the OSD oversleeps */ /* snapshot stuff */ render_target * snap_target; /* screen shapshot target */ bitmap_t * snap_bitmap; /* screen snapshot bitmap */ UINT8 snap_native; /* are we using native per-screen layouts? */ INT32 snap_width; /* width of snapshots (0 == auto) */ INT32 snap_height; /* height of snapshots (0 == auto) */ /* movie recording */ mame_file * mngfile; /* handle to the open movie file */ avi_file * avifile; /* handle to the open movie file */ attotime movie_frame_period; /* period of a single movie frame */ attotime movie_next_frame_time; /* time of next frame */ UINT32 movie_frame; /* current movie frame number */ }; /*************************************************************************** GLOBAL VARIABLES ***************************************************************************/ /* global state */ static video_global global; /* frameskipping tables */ static const UINT8 skiptable[FRAMESKIP_LEVELS][FRAMESKIP_LEVELS] = { { 0,0,0,0,0,0,0,0,0,0,0,0 }, { 0,0,0,0,0,0,0,0,0,0,0,1 }, { 0,0,0,0,0,1,0,0,0,0,0,1 }, { 0,0,0,1,0,0,0,1,0,0,0,1 }, { 0,0,1,0,0,1,0,0,1,0,0,1 }, { 0,1,0,0,1,0,1,0,0,1,0,1 }, { 0,1,0,1,0,1,0,1,0,1,0,1 }, { 0,1,0,1,1,0,1,0,1,1,0,1 }, { 0,1,1,0,1,1,0,1,1,0,1,1 }, { 0,1,1,1,0,1,1,1,0,1,1,1 }, { 0,1,1,1,1,1,0,1,1,1,1,1 }, { 0,1,1,1,1,1,1,1,1,1,1,1 } }; /*************************************************************************** FUNCTION PROTOTYPES ***************************************************************************/ /* core implementation */ static void video_exit(running_machine *machine); static void init_buffered_spriteram(running_machine *machine); /* graphics decoding */ static void allocate_graphics(running_machine *machine, const gfx_decode_entry *gfxdecodeinfo); static void decode_graphics(running_machine *machine, const gfx_decode_entry *gfxdecodeinfo); static void realloc_screen_bitmaps(const device_config *screen); static STATE_POSTLOAD( video_screen_postload ); /* global rendering */ static TIMER_CALLBACK( vblank_begin_callback ); static TIMER_CALLBACK( vblank_end_callback ); static TIMER_CALLBACK( screenless_update_callback ); static TIMER_CALLBACK( scanline0_callback ); static TIMER_CALLBACK( scanline_update_callback ); static int finish_screen_updates(running_machine *machine); /* throttling/frameskipping/performance */ static void update_throttle(running_machine *machine, attotime emutime); static osd_ticks_t throttle_until_ticks(running_machine *machine, osd_ticks_t target_ticks); static void update_frameskip(running_machine *machine); static void recompute_speed(running_machine *machine, attotime emutime); static void update_refresh_speed(running_machine *machine); /* screen snapshots */ static void create_snapshot_bitmap(const device_config *screen); static file_error mame_fopen_next(running_machine *machine, const char *pathoption, const char *extension, mame_file **file); /* movie recording */ static void video_mng_record_frame(running_machine *machine); static void video_avi_record_frame(running_machine *machine); /* software rendering */ static void rgb888_draw_primitives(const render_primitive *primlist, void *dstdata, UINT32 width, UINT32 height, UINT32 pitch); /*************************************************************************** INLINE FUNCTIONS ***************************************************************************/ /*------------------------------------------------- get_safe_token - makes sure that the passed in device is, in fact, a screen -------------------------------------------------*/ INLINE screen_state *get_safe_token(const device_config *device) { assert(device != NULL); assert(device->token != NULL); assert(device->type == VIDEO_SCREEN); return (screen_state *)device->token; } /*------------------------------------------------- effective_autoframeskip - return the effective autoframeskip value, accounting for fast forward -------------------------------------------------*/ INLINE int effective_autoframeskip(running_machine *machine) { /* if we're fast forwarding or paused, autoframeskip is disabled */ if (global.fastforward || mame_is_paused(machine)) return FALSE; /* otherwise, it's up to the user */ return global.auto_frameskip; } /*------------------------------------------------- effective_frameskip - return the effective frameskip value, accounting for fast forward -------------------------------------------------*/ INLINE int effective_frameskip(void) { /* if we're fast forwarding, use the maximum frameskip */ if (global.fastforward) return FRAMESKIP_LEVELS - 1; /* otherwise, it's up to the user */ return global.frameskip_level; } /*------------------------------------------------- effective_throttle - return the effective throttle value, accounting for fast forward and user interface -------------------------------------------------*/ INLINE int effective_throttle(running_machine *machine) { /* if we're paused, or if the UI is active, we always throttle */ if (mame_is_paused(machine) || ui_is_menu_active()) return TRUE; /* if we're fast forwarding, we don't throttle */ if (global.fastforward) return FALSE; /* otherwise, it's up to the user */ return global.throttle; } /*------------------------------------------------- original_speed_setting - return the original speed setting -------------------------------------------------*/ INLINE int original_speed_setting(void) { return options_get_float(mame_options(), OPTION_SPEED) * 100.0 + 0.5; } /*************************************************************************** CORE IMPLEMENTATION ***************************************************************************/ /*------------------------------------------------- video_init - start up the video system -------------------------------------------------*/ void video_init(running_machine *machine) { const char *filename; const char *viewname; /* validate */ assert(machine != NULL); assert(machine->config != NULL); /* request a callback upon exiting */ add_exit_callback(machine, video_exit); /* reset our global state */ memset(&global, 0, sizeof(global)); global.speed_percent = 1.0; /* extract initial execution state from global configuration settings */ global.speed = original_speed_setting(); update_refresh_speed(machine); global.throttle = options_get_bool(mame_options(), OPTION_THROTTLE); global.auto_frameskip = options_get_bool(mame_options(), OPTION_AUTOFRAMESKIP); global.frameskip_level = options_get_int(mame_options(), OPTION_FRAMESKIP); global.seconds_to_run = options_get_int(mame_options(), OPTION_SECONDS_TO_RUN); /* create spriteram buffers if necessary */ if (machine->config->video_attributes & VIDEO_BUFFERS_SPRITERAM) init_buffered_spriteram(machine); /* convert the gfx ROMs into character sets. This is done BEFORE calling the driver's */ /* palette_init() routine because it might need to check the machine->gfx[] data */ if (machine->config->gfxdecodeinfo != NULL) allocate_graphics(machine, machine->config->gfxdecodeinfo); /* call the PALETTE_INIT function */ if (machine->config->init_palette != NULL) (*machine->config->init_palette)(machine, memory_region(machine, "proms")); /* actually decode the graphics */ if (PREDECODE_GFX && machine->config->gfxdecodeinfo != NULL) decode_graphics(machine, machine->config->gfxdecodeinfo); /* create a render target for snapshots */ viewname = options_get_string(mame_options(), OPTION_SNAPVIEW); global.snap_native = (machine->primary_screen != NULL && (viewname[0] == 0 || strcmp(viewname, "native") == 0)); /* the native target is hard-coded to our internal layout and has all options disabled */ if (global.snap_native) { global.snap_target = render_target_alloc(machine, layout_snap, RENDER_CREATE_SINGLE_FILE | RENDER_CREATE_HIDDEN); assert(global.snap_target != NULL); render_target_set_layer_config(global.snap_target, 0); } /* other targets select the specified view and turn off effects */ else { global.snap_target = render_target_alloc(machine, NULL, RENDER_CREATE_HIDDEN); assert(global.snap_target != NULL); render_target_set_view(global.snap_target, video_get_view_for_target(machine, global.snap_target, viewname, 0, 1)); render_target_set_layer_config(global.snap_target, render_target_get_layer_config(global.snap_target) & ~LAYER_CONFIG_ENABLE_SCREEN_OVERLAY); } /* extract snap resolution if present */ if (sscanf(options_get_string(mame_options(), OPTION_SNAPSIZE), "%dx%d", &global.snap_width, &global.snap_height) != 2) global.snap_width = global.snap_height = 0; /* start recording movie if specified */ filename = options_get_string(mame_options(), OPTION_MNGWRITE); if (filename[0] != 0) video_mng_begin_recording(machine, filename); filename = options_get_string(mame_options(), OPTION_AVIWRITE); if (filename[0] != 0) video_avi_begin_recording(machine, filename); /* if no screens, create a periodic timer to drive updates */ if (machine->primary_screen == NULL) { global.screenless_frame_timer = timer_alloc(machine, screenless_update_callback, NULL); timer_adjust_periodic(global.screenless_frame_timer, DEFAULT_FRAME_PERIOD, 0, DEFAULT_FRAME_PERIOD); } } /*------------------------------------------------- video_exit - close down the video system -------------------------------------------------*/ static void video_exit(running_machine *machine) { int i; /* validate */ assert(machine != NULL); assert(machine->config != NULL); /* stop recording any movie */ video_mng_end_recording(machine); video_avi_end_recording(machine); /* free all the graphics elements */ for (i = 0; i < MAX_GFX_ELEMENTS; i++) gfx_element_free(machine->gfx); /* free the snapshot target */ if (global.snap_target != NULL) render_target_free(global.snap_target); if (global.snap_bitmap != NULL) bitmap_free(global.snap_bitmap); /* print a final result if we have at least 5 seconds' worth of data */ if (global.overall_emutime.seconds >= 5) { osd_ticks_t tps = osd_ticks_per_second(); double final_real_time = (double)global.overall_real_seconds + (double)global.overall_real_ticks / (double)tps; double final_emu_time = attotime_to_double(global.overall_emutime); mame_printf_info("Average speed: %.2f%% (%d seconds)\n", 100 * final_emu_time / final_real_time, attotime_add_attoseconds(global.overall_emutime, ATTOSECONDS_PER_SECOND / 2).seconds); } } /*------------------------------------------------- init_buffered_spriteram - initialize the double-buffered spriteram -------------------------------------------------*/ static void init_buffered_spriteram(running_machine *machine) { assert_always(spriteram_size != 0, "Video buffers spriteram but spriteram_size is 0"); /* allocate memory for the back buffer */ buffered_spriteram = (UINT8 *)auto_malloc(spriteram_size); /* register for saving it */ state_save_register_global_pointer(machine, buffered_spriteram, spriteram_size); /* do the same for the secon back buffer, if present */ if (spriteram_2_size) { /* allocate memory */ buffered_spriteram_2 = (UINT8 *)auto_malloc(spriteram_2_size); /* register for saving it */ state_save_register_global_pointer(machine, buffered_spriteram_2, spriteram_2_size); } /* make 16-bit and 32-bit pointer variants */ buffered_spriteram16 = (UINT16 *)buffered_spriteram; buffered_spriteram32 = (UINT32 *)buffered_spriteram; buffered_spriteram16_2 = (UINT16 *)buffered_spriteram_2; buffered_spriteram32_2 = (UINT32 *)buffered_spriteram_2; } /*************************************************************************** GRAPHICS DECODING ***************************************************************************/ /*------------------------------------------------- allocate_graphics - allocate memory for the graphics -------------------------------------------------*/ static void allocate_graphics(running_machine *machine, const gfx_decode_entry *gfxdecodeinfo) { int curgfx; /* loop over all elements */ for (curgfx = 0; curgfx < MAX_GFX_ELEMENTS && gfxdecodeinfo[curgfx].gfxlayout != NULL; curgfx++) { const gfx_decode_entry *gfxdecode = &gfxdecodeinfo[curgfx]; UINT32 region_length = 8 * memory_region_length(machine, gfxdecode->memory_region); const UINT8 *region_base = memory_region(machine, gfxdecode->memory_region); UINT32 xscale = (gfxdecode->xscale == 0) ? 1 : gfxdecode->xscale; UINT32 yscale = (gfxdecode->yscale == 0) ? 1 : gfxdecode->yscale; UINT32 *extpoffs, extxoffs[MAX_ABS_GFX_SIZE], extyoffs[MAX_ABS_GFX_SIZE]; const gfx_layout *gl = gfxdecode->gfxlayout; int israw = (gl->planeoffset[0] == GFX_RAW); int planes = gl->planes; UINT32 width = gl->width; UINT32 height = gl->height; UINT32 total = gl->total; UINT32 charincrement = gl->charincrement; gfx_layout glcopy; int j; /* make a copy of the layout */ glcopy = *gfxdecode->gfxlayout; /* copy the X and Y offsets into temporary arrays */ memcpy(extxoffs, glcopy.xoffset, sizeof(glcopy.xoffset)); memcpy(extyoffs, glcopy.yoffset, sizeof(glcopy.yoffset)); /* if there are extended offsets, copy them over top */ if (glcopy.extxoffs != NULL) memcpy(extxoffs, glcopy.extxoffs, glcopy.width * sizeof(extxoffs[0])); if (glcopy.extyoffs != NULL) memcpy(extyoffs, glcopy.extyoffs, glcopy.height * sizeof(extyoffs[0])); /* always use the extended offsets here */ glcopy.extxoffs = extxoffs; glcopy.extyoffs = extyoffs; extpoffs = glcopy.planeoffset; /* expand X and Y by the scale factors */ if (xscale > 1) { width *= xscale; for (j = width - 1; j >= 0; j--) extxoffs[j] = extxoffs[j / xscale]; } if (yscale > 1) { height *= yscale; for (j = height - 1; j >= 0; j--) extyoffs[j] = extyoffs[j / yscale]; } /* if the character count is a region fraction, compute the effective total */ if (IS_FRAC(total)) { assert(region_length != 0); total = region_length / charincrement * FRAC_NUM(total) / FRAC_DEN(total); } /* for non-raw graphics, decode the X and Y offsets */ if (!israw) { /* loop over all the planes, converting fractions */ for (j = 0; j < planes; j++) { UINT32 value = extpoffs[j]; if (IS_FRAC(value)) { assert(region_length != 0); extpoffs[j] = FRAC_OFFSET(value) + region_length * FRAC_NUM(value) / FRAC_DEN(value); } } /* loop over all the X/Y offsets, converting fractions */ for (j = 0; j < width; j++) { UINT32 value = extxoffs[j]; if (IS_FRAC(value)) { assert(region_length != 0); extxoffs[j] = FRAC_OFFSET(value) + region_length * FRAC_NUM(value) / FRAC_DEN(value); } } for (j = 0; j < height; j++) { UINT32 value = extyoffs[j]; if (IS_FRAC(value)) { assert(region_length != 0); extyoffs[j] = FRAC_OFFSET(value) + region_length * FRAC_NUM(value) / FRAC_DEN(value); } } } /* otherwise, just use the line modulo */ else { int base = gfxdecode->start; int end = region_length/8; int linemod = gl->yoffset[0]; while (total > 0) { int elementbase = base + (total - 1) * charincrement / 8; int lastpixelbase = elementbase + height * linemod / 8 - 1; if (lastpixelbase < end) break; total--; } } /* update glcopy */ glcopy.width = width; glcopy.height = height; glcopy.total = total; /* allocate the graphics */ machine->gfx[curgfx] = gfx_element_alloc(machine, &glcopy, (region_base != NULL) ? region_base + gfxdecode->start : NULL, gfxdecode->total_color_codes, gfxdecode->color_codes_start); } } /*------------------------------------------------- decode_graphics - decode the graphics -------------------------------------------------*/ static void decode_graphics(running_machine *machine, const gfx_decode_entry *gfxdecodeinfo) { int totalgfx = 0, curgfx = 0; int i; /* count total graphics elements */ for (i = 0; i < MAX_GFX_ELEMENTS; i++) if (machine->gfx != NULL) totalgfx += machine->gfx->total_elements; /* loop over all elements */ for (i = 0; i < MAX_GFX_ELEMENTS; i++) if (machine->gfx != NULL) { /* if we have a valid region, decode it now */ if (gfxdecodeinfo.memory_region != NULL) { gfx_element *gfx = machine->gfx; int j; /* now decode the actual graphics */ for (j = 0; j < gfx->total_elements; j += 1024) { char buffer[200]; int num_to_decode = (j + 1024 < gfx->total_elements) ? 1024 : (gfx->total_elements - j); decodegfx(gfx, j, num_to_decode); curgfx += num_to_decode; /* display some startup text */ sprintf(buffer, "Decoding (%d%%)", curgfx * 100 / totalgfx); ui_set_startup_text(machine, buffer, FALSE); } } /* otherwise, clear the target region */ else memset(machine->gfx->gfxdata, 0, machine->gfx->char_modulo * machine->gfx->total_elements); } } /*************************************************************************** SCREEN MANAGEMENT ***************************************************************************/ /*------------------------------------------------- video_screen_configure - configure the parameters of a screen -------------------------------------------------*/ void video_screen_configure(const device_config *screen, int width, int height, const rectangle *visarea, attoseconds_t frame_period) { screen_state *state = get_safe_token(screen); screen_config *config = (screen_config *)screen->inline_config; /* validate arguments */ assert(width > 0); assert(height > 0); assert(visarea != NULL); assert(visarea->min_x >= 0); assert(visarea->min_y >= 0); assert(config->type == SCREEN_TYPE_VECTOR || visarea->min_x < width); assert(config->type == SCREEN_TYPE_VECTOR || visarea->min_y < height); assert(frame_period > 0); /* fill in the new parameters */ state->width = width; state->height = height; state->visarea = *visarea; /* reallocate bitmap if necessary */ realloc_screen_bitmaps(screen); /* compute timing parameters */ state->frame_period = frame_period; state->scantime = frame_period / height; state->pixeltime = frame_period / (height * width); /* if there has been no VBLANK time specified in the MACHINE_DRIVER, compute it now from the visible area, otherwise just used the supplied value */ if (config->vblank == 0 && !config->oldstyle_vblank_supplied) state->vblank_period = state->scantime * (height - (visarea->max_y + 1 - visarea->min_y)); else state->vblank_period = config->vblank; /* if we are on scanline 0 already, reset the update timer immediately */ /* otherwise, defer until the next scanline 0 */ if (video_screen_get_vpos(screen) == 0) timer_adjust_oneshot(state->scanline0_timer, attotime_zero, 0); else timer_adjust_oneshot(state->scanline0_timer, video_screen_get_time_until_pos(screen, 0, 0), 0); /* start the VBLANK timer */ timer_adjust_oneshot(state->vblank_begin_timer, video_screen_get_time_until_vblank_start(screen), 0); /* adjust speed if necessary */ update_refresh_speed(screen->machine); } /*------------------------------------------------- realloc_screen_bitmaps - reallocate screen bitmaps as necessary -------------------------------------------------*/ static void realloc_screen_bitmaps(const device_config *screen) { screen_state *state = get_safe_token(screen); screen_config *config = (screen_config *)screen->inline_config; if (config->type != SCREEN_TYPE_VECTOR) { int curwidth = 0, curheight = 0; /* extract the current width/height from the bitmap */ if (state->bitmap[0] != NULL) { curwidth = state->bitmap[0]->width; curheight = state->bitmap[0]->height; } /* if we're too small to contain this width/height, reallocate our bitmaps and textures */ if (state->width > curwidth || state->height > curheight) { bitmap_format screen_format = config->format; palette_t *palette; /* free what we have currently */ if (state->texture[0] != NULL) render_texture_free(state->texture[0]); if (state->texture[1] != NULL) render_texture_free(state->texture[1]); if (state->bitmap[0] != NULL) bitmap_free(state->bitmap[0]); if (state->bitmap[1] != NULL) bitmap_free(state->bitmap[1]); /* compute new width/height */ curwidth = MAX(state->width, curwidth); curheight = MAX(state->height, curheight); /* choose the texture format - convert the screen format to a texture format */ switch (screen_format) { case BITMAP_FORMAT_INDEXED16: state->texture_format = TEXFORMAT_PALETTE16; palette = screen->machine->palette; break; case BITMAP_FORMAT_RGB15: state->texture_format = TEXFORMAT_RGB15; palette = NULL; break; case BITMAP_FORMAT_RGB32: state->texture_format = TEXFORMAT_RGB32; palette = NULL; break; default: fatalerror("Invalid bitmap format!"); break; } /* allocate bitmaps */ state->bitmap[0] = bitmap_alloc(curwidth, curheight, screen_format); bitmap_set_palette(state->bitmap[0], screen->machine->palette); state->bitmap[1] = bitmap_alloc(curwidth, curheight, screen_format); bitmap_set_palette(state->bitmap[1], screen->machine->palette); /* allocate textures */ state->texture[0] = render_texture_alloc(NULL, NULL); render_texture_set_bitmap(state->texture[0], state->bitmap[0], &state->visarea, state->texture_format, palette); state->texture[1] = render_texture_alloc(NULL, NULL); render_texture_set_bitmap(state->texture[1], state->bitmap[1], &state->visarea, state->texture_format, palette); } } } /*------------------------------------------------- video_screen_set_visarea - just set the visible area of a screen -------------------------------------------------*/ void video_screen_set_visarea(const device_config *screen, int min_x, int max_x, int min_y, int max_y) { screen_state *state = get_safe_token(screen); rectangle visarea; /* validate arguments */ assert(min_x >= 0); assert(min_y >= 0); assert(min_x < max_x); assert(min_y < max_y); visarea.min_x = min_x; visarea.max_x = max_x; visarea.min_y = min_y; visarea.max_y = max_y; video_screen_configure(screen, state->width, state->height, &visarea, state->frame_period); } /*------------------------------------------------- video_screen_update_partial - perform a partial update from the last scanline up to and including the specified scanline -------------------------------------------------*/ int video_screen_update_partial(const device_config *screen, int scanline) { screen_state *state = get_safe_token(screen); rectangle clip = state->visarea; int result = FALSE; /* validate arguments */ assert(scanline >= 0); LOG_PARTIAL_UPDATES(("Partial: video_screen_update_partial(%s, %d): ", screen->tag, scanline)); /* these two checks only apply if we're allowed to skip frames */ if (!(screen->machine->config->video_attributes & VIDEO_ALWAYS_UPDATE)) { /* if skipping this frame, bail */ if (global.skipping_this_frame) { LOG_PARTIAL_UPDATES(("skipped due to frameskipping\n")); return FALSE; } /* skip if this screen is not visible anywhere */ if (!render_is_live_screen(screen)) { LOG_PARTIAL_UPDATES(("skipped because screen not live\n")); return FALSE; } } /* skip if less than the lowest so far */ if (scanline < state->last_partial_scan) { LOG_PARTIAL_UPDATES(("skipped because less than previous\n")); return FALSE; } /* set the start/end scanlines */ if (state->last_partial_scan > clip.min_y) clip.min_y = state->last_partial_scan; if (scanline < clip.max_y) clip.max_y = scanline; /* render if necessary */ if (clip.min_y <= clip.max_y) { UINT32 flags = UPDATE_HAS_NOT_CHANGED; profiler_mark(PROFILER_VIDEO); LOG_PARTIAL_UPDATES(("updating %d-%d\n", clip.min_y, clip.max_y)); if (screen->machine->config->video_update != NULL) flags = (*screen->machine->config->video_update)(screen, state->bitmap[state->curbitmap], &clip); global.partial_updates_this_frame++; profiler_mark(PROFILER_END); /* if we modified the bitmap, we have to commit */ state->changed |= ~flags & UPDATE_HAS_NOT_CHANGED; result = TRUE; } /* remember where we left off */ state->last_partial_scan = scanline + 1; return result; } /*------------------------------------------------- video_screen_update_now - perform an update from the last beam position up to the current beam position -------------------------------------------------*/ void video_screen_update_now(const device_config *screen) { screen_state *state = get_safe_token(screen); int current_vpos = video_screen_get_vpos(screen); int current_hpos = video_screen_get_hpos(screen); /* since we can currently update only at the scanline level, we are trying to do the right thing by updating including the current scanline, only if the beam is past the halfway point horizontally. If the beam is in the first half of the scanline, we only update up to the previous scanline. This minimizes the number of pixels that might be drawn incorrectly until we support a pixel level granularity */ if (current_hpos < (state->width / 2) && current_vpos > 0) current_vpos = current_vpos - 1; video_screen_update_partial(screen, current_vpos); } /*------------------------------------------------- video_screen_get_vpos - returns the current vertical position of the beam for a given screen -------------------------------------------------*/ int video_screen_get_vpos(const device_config *screen) { screen_state *state = get_safe_token(screen); attoseconds_t delta = attotime_to_attoseconds(attotime_sub(timer_get_time(screen->machine), state->vblank_start_time)); int vpos; /* round to the nearest pixel */ delta += state->pixeltime / 2; /* compute the v position relative to the start of VBLANK */ vpos = delta / state->scantime; /* adjust for the fact that VBLANK starts at the bottom of the visible area */ return (state->visarea.max_y + 1 + vpos) % state->height; } /*------------------------------------------------- video_screen_get_hpos - returns the current horizontal position of the beam for a given screen -------------------------------------------------*/ int video_screen_get_hpos(const device_config *screen) { screen_state *state = get_safe_token(screen); attoseconds_t delta = attotime_to_attoseconds(attotime_sub(timer_get_time(screen->machine), state->vblank_start_time)); int vpos; /* round to the nearest pixel */ delta += state->pixeltime / 2; /* compute the v position relative to the start of VBLANK */ vpos = delta / state->scantime; /* subtract that from the total time */ delta -= vpos * state->scantime; /* return the pixel offset from the start of this scanline */ return delta / state->pixeltime; } /*------------------------------------------------- video_screen_get_vblank - returns the VBLANK state of a given screen -------------------------------------------------*/ int video_screen_get_vblank(const device_config *screen) { screen_state *state = get_safe_token(screen); /* we should never be called with no VBLANK period - indication of a buggy driver */ assert(state->vblank_period != 0); return (attotime_compare(timer_get_time(screen->machine), state->vblank_end_time) < 0); } /*------------------------------------------------- video_screen_get_hblank - returns the HBLANK state of a given screen -------------------------------------------------*/ int video_screen_get_hblank(const device_config *screen) { screen_state *state = get_safe_token(screen); int hpos = video_screen_get_hpos(screen); return (hpos < state->visarea.min_x || hpos > state->visarea.max_x); } /*------------------------------------------------- video_screen_get_width - returns the width of a given screen -------------------------------------------------*/ int video_screen_get_width(const device_config *screen) { screen_state *state = get_safe_token(screen); return state->width; } /*------------------------------------------------- video_screen_get_height - returns the height of a given screen -------------------------------------------------*/ int video_screen_get_height(const device_config *screen) { screen_state *state = get_safe_token(screen); return state->height; } /*------------------------------------------------- video_screen_get_visible_area - returns the visible area of a given screen -------------------------------------------------*/ const rectangle *video_screen_get_visible_area(const device_config *screen) { screen_state *state = get_safe_token(screen); return &state->visarea; } /*------------------------------------------------- video_screen_get_time_until_pos - returns the amount of time remaining until the beam is at the given hpos,vpos -------------------------------------------------*/ attotime video_screen_get_time_until_pos(const device_config *screen, int vpos, int hpos) { screen_state *state = get_safe_token(screen); attoseconds_t curdelta = attotime_to_attoseconds(attotime_sub(timer_get_time(screen->machine), state->vblank_start_time)); attoseconds_t targetdelta; /* validate arguments */ assert(vpos >= 0); assert(hpos >= 0); /* since we measure time relative to VBLANK, compute the scanline offset from VBLANK */ vpos += state->height - (state->visarea.max_y + 1); vpos %= state->height; /* compute the delta for the given X,Y position */ targetdelta = (attoseconds_t)vpos * state->scantime + (attoseconds_t)hpos * state->pixeltime; /* if we're past that time (within 1/2 of a pixel), head to the next frame */ if (targetdelta <= curdelta + state->pixeltime / 2) targetdelta += state->frame_period; while (targetdelta <= curdelta) targetdelta += state->frame_period; /* return the difference */ return attotime_make(0, targetdelta - curdelta); } /*------------------------------------------------- video_screen_get_time_until_vblank_start - returns the amount of time remaining until the next VBLANK period start -------------------------------------------------*/ attotime video_screen_get_time_until_vblank_start(const device_config *screen) { return video_screen_get_time_until_pos(screen, video_screen_get_visible_area(screen)->max_y + 1, 0); } /*------------------------------------------------- video_screen_get_time_until_vblank_end - returns the amount of time remaining until the end of the current VBLANK (if in progress) or the end of the next VBLANK -------------------------------------------------*/ attotime video_screen_get_time_until_vblank_end(const device_config *screen) { attotime ret; screen_state *state = get_safe_token(screen); attotime current_time = timer_get_time(screen->machine); /* we are in the VBLANK region, compute the time until the end of the current VBLANK period */ if (video_screen_get_vblank(screen)) ret = attotime_sub(state->vblank_end_time, current_time); /* otherwise compute the time until the end of the next frame VBLANK period */ else ret = attotime_sub(attotime_add_attoseconds(state->vblank_end_time, state->frame_period), current_time); return ret; } /*------------------------------------------------- video_screen_get_time_until_update - returns the amount of time remaining until the next VBLANK period start -------------------------------------------------*/ attotime video_screen_get_time_until_update(const device_config *screen) { if (screen->machine->config->video_attributes & VIDEO_UPDATE_AFTER_VBLANK) return video_screen_get_time_until_vblank_end(screen); else return video_screen_get_time_until_vblank_start(screen); } /*------------------------------------------------- video_screen_get_scan_period - return the amount of time the beam takes to draw one scanline -------------------------------------------------*/ attotime video_screen_get_scan_period(const device_config *screen) { screen_state *state = get_safe_token(screen); return attotime_make(0, state->scantime); } /*------------------------------------------------- video_screen_get_frame_period - return the amount of time the beam takes to draw one complete frame -------------------------------------------------*/ attotime video_screen_get_frame_period(const device_config *screen) { attotime ret; /* a lot of modules want to the period of the primary screen, so if we are screenless, return something reasonable so that we don't fall over */ if (screen == NULL || !screen->started || video_screen_count(screen->machine->config) == 0) { ret = DEFAULT_FRAME_PERIOD; } else { screen_state *state = get_safe_token(screen); ret = attotime_make(0, state->frame_period); } return ret; } /*------------------------------------------------- video_screen_get_frame_number - return the current frame number since the start of the emulated machine -------------------------------------------------*/ UINT64 video_screen_get_frame_number(const device_config *screen) { screen_state *state = get_safe_token(screen); return state->frame_number; } /*------------------------------------------------- video_screen_register_vblank_callback - registers a VBLANK callback for a specific screen -------------------------------------------------*/ void video_screen_register_vblank_callback(const device_config *screen, vblank_state_changed_func vblank_callback, void *param) { screen_state *state = get_safe_token(screen); int i, found; /* validate arguments */ assert(vblank_callback != NULL); /* check if we already have this callback registered */ found = FALSE; for (i = 0; i < MAX_VBLANK_CALLBACKS; i++) { if (state->vblank_callback == NULL) break; if (state->vblank_callback == vblank_callback) found = TRUE; } /* check that there is room */ assert(i != MAX_VBLANK_CALLBACKS); /* if not found, register and increment count */ if (!found) { state->vblank_callback = vblank_callback; state->vblank_callback_param = param; } } /*************************************************************************** VIDEO SCREEN DEVICE INTERFACE ***************************************************************************/ /*------------------------------------------------- device_start_video_screen - device start callback for a video screen -------------------------------------------------*/ static DEVICE_START( video_screen ) { const device_config *screen = device; screen_state *state = get_safe_token(screen); render_container_user_settings settings; render_container *container; screen_config *config; /* validate some basic stuff */ assert(screen != NULL); assert(screen->static_config == NULL); assert(screen->inline_config != NULL); assert(screen->machine != NULL); assert(screen->machine->config != NULL); /* get and validate that the container for this screen exists */ container = render_container_get_screen(screen); assert(container != NULL); /* get and validate the configuration */ config = (screen_config *)screen->inline_config; assert(config->width > 0); assert(config->height > 0); assert(config->refresh > 0); assert(config->visarea.min_x >= 0); assert(config->visarea.max_x < config->width || config->type == SCREEN_TYPE_VECTOR); assert(config->visarea.max_x > config->visarea.min_x); assert(config->visarea.min_y >= 0); assert(config->visarea.max_y < config->height || config->type == SCREEN_TYPE_VECTOR); assert(config->visarea.max_y > config->visarea.min_y); /* allocate the VBLANK timers */ state->vblank_begin_timer = timer_alloc(screen->machine, vblank_begin_callback, (void *)screen); state->vblank_end_timer = timer_alloc(screen->machine, vblank_end_callback, (void *)screen); /* allocate a timer to reset partial updates */ state->scanline0_timer = timer_alloc(screen->machine, scanline0_callback, (void *)screen); /* configure the default cliparea */ render_container_get_user_settings(container, &settings); if (config->xoffset != 0) settings.xoffset = config->xoffset; if (config->yoffset != 0) settings.yoffset = config->yoffset; if (config->xscale != 0) settings.xscale = config->xscale; if (config->yscale != 0) settings.yscale = config->yscale; render_container_set_user_settings(container, &settings); /* allocate a timer to generate per-scanline updates */ if (screen->machine->config->video_attributes & VIDEO_UPDATE_SCANLINE) state->scanline_timer = timer_alloc(screen->machine, scanline_update_callback, (void *)screen); /* configure the screen with the default parameters */ video_screen_configure(screen, config->width, config->height, &config->visarea, config->refresh); /* reset VBLANK timing */ state->vblank_start_time = attotime_zero; state->vblank_end_time = attotime_make(0, state->vblank_period); /* start the timer to generate per-scanline updates */ if (screen->machine->config->video_attributes & VIDEO_UPDATE_SCANLINE) timer_adjust_oneshot(state->scanline_timer, video_screen_get_time_until_pos(screen, 0, 0), 0); state_save_register_device_item(screen, 0, state->width); state_save_register_device_item(screen, 0, state->height); state_save_register_device_item(screen, 0, state->visarea.min_x); state_save_register_device_item(screen, 0, state->visarea.min_y); state_save_register_device_item(screen, 0, state->visarea.max_x); state_save_register_device_item(screen, 0, state->visarea.max_y); state_save_register_device_item(screen, 0, state->last_partial_scan); state_save_register_device_item(screen, 0, state->frame_period); state_save_register_device_item(screen, 0, state->scantime); state_save_register_device_item(screen, 0, state->pixeltime); state_save_register_device_item(screen, 0, state->vblank_period); state_save_register_device_item(screen, 0, state->vblank_start_time.seconds); state_save_register_device_item(screen, 0, state->vblank_start_time.attoseconds); state_save_register_device_item(screen, 0, state->vblank_end_time.seconds); state_save_register_device_item(screen, 0, state->vblank_end_time.attoseconds); state_save_register_device_item(screen, 0, state->frame_number); state_save_register_postload(device->machine, video_screen_postload, (void *)device); } /*------------------------------------------------- video_screen_postload - after a state load, reconfigure each screen -------------------------------------------------*/ static STATE_POSTLOAD( video_screen_postload ) { const device_config *screen = (const device_config *)param; realloc_screen_bitmaps(screen); global.movie_next_frame_time = timer_get_time(machine); } /*------------------------------------------------- device_stop_video_screen - device stop callback for a video screen -------------------------------------------------*/ static DEVICE_STOP( video_screen ) { const device_config *screen = device; screen_state *state = get_safe_token(screen); if (state->texture[0] != NULL) render_texture_free(state->texture[0]); if (state->texture[1] != NULL) render_texture_free(state->texture[1]); if (state->bitmap[0] != NULL) bitmap_free(state->bitmap[0]); if (state->bitmap[1] != NULL) bitmap_free(state->bitmap[1]); } /*------------------------------------------------- video_screen_set_info - device set info callback -------------------------------------------------*/ static DEVICE_SET_INFO( video_screen ) { switch (state) { /* no parameters to set */ } } /*------------------------------------------------- video_screen_get_info - device get info callback -------------------------------------------------*/ DEVICE_GET_INFO( video_screen ) { switch (state) { /* --- the following bits of info are returned as 64-bit signed integers --- */ case DEVINFO_INT_TOKEN_BYTES: info->i = sizeof(screen_state); break; case DEVINFO_INT_INLINE_CONFIG_BYTES: info->i = sizeof(screen_config); break; case DEVINFO_INT_CLASS: info->i = DEVICE_CLASS_VIDEO; break; /* --- the following bits of info are returned as pointers to data or functions --- */ case DEVINFO_FCT_SET_INFO: info->set_info = DEVICE_SET_INFO_NAME(video_screen); break; case DEVINFO_FCT_START: info->start = DEVICE_START_NAME(video_screen); break; case DEVINFO_FCT_STOP: info->stop = DEVICE_STOP_NAME(video_screen); break; case DEVINFO_FCT_RESET: /* Nothing */ break; /* --- the following bits of info are returned as NULL-terminated strings --- */ case DEVINFO_STR_NAME: strcpy(info->s, "Raster"); break; case DEVINFO_STR_FAMILY: strcpy(info->s, "Video Screen"); break; case DEVINFO_STR_VERSION: strcpy(info->s, "1.0"); break; case DEVINFO_STR_SOURCE_FILE: strcpy(info->s, __FILE__); break; case DEVINFO_STR_CREDITS: strcpy(info->s, "Copyright Nicola Salmoria and the MAME Team"); break; } } /*************************************************************************** GLOBAL RENDERING ***************************************************************************/ /*------------------------------------------------- vblank_begin_callback - call any external callbacks to signal the VBLANK period has begun -------------------------------------------------*/ static TIMER_CALLBACK( vblank_begin_callback ) { int i; device_config *screen = (device_config *)ptr; screen_state *state = get_safe_token(screen); /* reset the starting VBLANK time */ state->vblank_start_time = timer_get_time(machine); state->vblank_end_time = attotime_add_attoseconds(state->vblank_start_time, state->vblank_period); /* call the screen specific callbacks */ for (i = 0; state->vblank_callback != NULL; i++) (*state->vblank_callback)(screen, state->vblank_callback_param, TRUE); /* if this is the primary screen and we need to update now */ if (screen == machine->primary_screen && !(machine->config->video_attributes & VIDEO_UPDATE_AFTER_VBLANK)) video_frame_update(machine, FALSE); /* reset the VBLANK start timer for the next frame */ timer_adjust_oneshot(state->vblank_begin_timer, video_screen_get_time_until_vblank_start(screen), 0); /* if no VBLANK period, call the VBLANK end callback immedietely, otherwise reset the timer */ if (state->vblank_period == 0) vblank_end_callback(machine, screen, 0); else timer_adjust_oneshot(state->vblank_end_timer, video_screen_get_time_until_vblank_end(screen), 0); } /*------------------------------------------------- vblank_end_callback - call any external callbacks to signal the VBLANK period has ended -------------------------------------------------*/ static TIMER_CALLBACK( vblank_end_callback ) { int i; const device_config *screen = (const device_config *)ptr; screen_state *state = get_safe_token(screen); /* call the screen specific callbacks */ for (i = 0; state->vblank_callback != NULL; i++) (*state->vblank_callback)(screen, state->vblank_callback_param, FALSE); /* if this is the primary screen and we need to update now */ if (screen == machine->primary_screen && (machine->config->video_attributes & VIDEO_UPDATE_AFTER_VBLANK)) video_frame_update(machine, FALSE); /* increment the frame number counter */ state->frame_number++; } /*------------------------------------------------- screenless_update_callback - update generator when there are no screens to drive it -------------------------------------------------*/ static TIMER_CALLBACK( screenless_update_callback ) { /* force an update */ video_frame_update(machine, FALSE); } /*------------------------------------------------- scanline0_callback - reset partial updates for a screen -------------------------------------------------*/ static TIMER_CALLBACK( scanline0_callback ) { const device_config *screen = (const device_config *)ptr; screen_state *state = get_safe_token(screen); /* reset partial updates */ state->last_partial_scan = 0; global.partial_updates_this_frame = 0; timer_adjust_oneshot(state->scanline0_timer, video_screen_get_time_until_pos(screen, 0, 0), 0); } /*------------------------------------------------- scanline_update_callback - perform partial updates on each scanline -------------------------------------------------*/ static TIMER_CALLBACK( scanline_update_callback ) { const device_config *screen = (const device_config *)ptr; screen_state *state = get_safe_token(screen); int scanline = param; /* force a partial update to the current scanline */ video_screen_update_partial(screen, scanline); /* compute the next visible scanline */ scanline++; if (scanline > state->visarea.max_y) scanline = state->visarea.min_y; timer_adjust_oneshot(state->scanline_timer, video_screen_get_time_until_pos(screen, scanline, 0), scanline); } /*------------------------------------------------- video_frame_update - handle frameskipping and UI, plus updating the screen during normal operations -------------------------------------------------*/ void video_frame_update(running_machine *machine, int debug) { attotime current_time = timer_get_time(machine); int skipped_it = global.skipping_this_frame; int phase = mame_get_phase(machine); /* validate */ assert(machine != NULL); assert(machine->config != NULL); /* only render sound and video if we're in the running phase */ if (phase == MAME_PHASE_RUNNING && (!mame_is_paused(machine) || options_get_bool(mame_options(), OPTION_UPDATEINPAUSE))) { int anything_changed = finish_screen_updates(machine); /* if none of the screens changed and we haven't skipped too many frames in a row, mark this frame as skipped to prevent throttling; this helps for games that don't update their screen at the monitor refresh rate */ if (!anything_changed && !global.auto_frameskip && global.frameskip_level == 0 && global.empty_skip_count++ < 3) skipped_it = TRUE; else global.empty_skip_count = 0; } /* draw the user interface */ ui_update_and_render(machine); /* if we're throttling, synchronize before rendering */ if (!debug && !skipped_it && effective_throttle(machine)) update_throttle(machine, current_time); /* ask the OSD to update */ profiler_mark(PROFILER_BLIT); osd_update(machine, !debug && skipped_it); profiler_mark(PROFILER_END); /* perform tasks for this frame */ if (!debug) mame_frame_update(machine); /* update frameskipping */ if (!debug) update_frameskip(machine); /* update speed computations */ if (!debug && !skipped_it) recompute_speed(machine, current_time); /* call the end-of-frame callback */ if (phase == MAME_PHASE_RUNNING) { /* reset partial updates if we're paused or if the debugger is active */ if (machine->primary_screen != NULL && (mame_is_paused(machine) || debug || debugger_within_instruction_hook(machine))) { void *param = (void *)machine->primary_screen; scanline0_callback(machine, param, 0); } /* otherwise, call the video EOF callback */ else if (machine->config->video_eof != NULL) { profiler_mark(PROFILER_VIDEO); (*machine->config->video_eof)(machine); profiler_mark(PROFILER_END); } } } /*------------------------------------------------- finish_screen_updates - finish updating all the screens -------------------------------------------------*/ static int finish_screen_updates(running_machine *machine) { const device_config *screen; int anything_changed = FALSE; /* finish updating the screens */ for (screen = video_screen_first(machine->config); screen != NULL; screen = video_screen_next(screen)) video_screen_update_partial(screen, video_screen_get_visible_area(screen)->max_y); /* now add the quads for all the screens */ for (screen = video_screen_first(machine->config); screen != NULL; screen = video_screen_next(screen)) { screen_state *state = get_safe_token(screen); /* only update if live */ if (render_is_live_screen(screen)) { const screen_config *config = (const screen_config *)screen->inline_config; /* only update if empty and not a vector game; otherwise assume the driver did it directly */ if (config->type != SCREEN_TYPE_VECTOR && (machine->config->video_attributes & VIDEO_SELF_RENDER) == 0) { /* if we're not skipping the frame and if the screen actually changed, then update the texture */ if (!global.skipping_this_frame && state->changed) { bitmap_t *bitmap = state->bitmap[state->curbitmap]; rectangle fixedvis = *video_screen_get_visible_area(screen); palette_t *palette = (state->texture_format == TEXFORMAT_PALETTE16) ? machine->palette : NULL; fixedvis.max_x++; fixedvis.max_y++; render_texture_set_bitmap(state->texture[state->curbitmap], bitmap, &fixedvis, state->texture_format, palette); state->curtexture = state->curbitmap; state->curbitmap = 1 - state->curbitmap; } /* create an empty container with a single quad */ render_container_empty(render_container_get_screen(screen)); render_screen_add_quad(screen, 0.0f, 0.0f, 1.0f, 1.0f, MAKE_ARGB(0xff,0xff,0xff,0xff), state->texture[state->curtexture], PRIMFLAG_BLENDMODE(BLENDMODE_NONE) | PRIMFLAG_SCREENTEX(1)); } } /* reset the screen changed flags */ if (state->changed) anything_changed = TRUE; state->changed = FALSE; } /* update our movie recording state */ if (!mame_is_paused(machine)) { video_mng_record_frame(machine); video_avi_record_frame(machine); } /* draw any crosshairs */ for (screen = video_screen_first(machine->config); screen != NULL; screen = video_screen_next(screen)) crosshair_render(screen); return anything_changed; } /*************************************************************************** THROTTLING/FRAMESKIPPING/PERFORMANCE ***************************************************************************/ /*------------------------------------------------- video_skip_this_frame - accessor to determine if this frame is being skipped -------------------------------------------------*/ int video_skip_this_frame(void) { return global.skipping_this_frame; } /*------------------------------------------------- video_get_speed_factor - return the speed factor as an integer * 100 -------------------------------------------------*/ int video_get_speed_factor(void) { return global.speed; } /*------------------------------------------------- video_set_speed_factor - sets the speed factor as an integer * 100 -------------------------------------------------*/ void video_set_speed_factor(int speed) { global.speed = speed; } /*------------------------------------------------- video_get_speed_text - print the text to be displayed in the upper-right corner -------------------------------------------------*/ const char *video_get_speed_text(running_machine *machine) { int paused = mame_is_paused(machine); static char buffer[1024]; char *dest = buffer; /* validate */ assert(machine != NULL); /* if we're paused, just display Paused */ if (paused) dest += sprintf(dest, "paused"); /* if we're fast forwarding, just display Fast-forward */ else if (global.fastforward) dest += sprintf(dest, "fast "); /* if we're auto frameskipping, display that plus the level */ else if (effective_autoframeskip(machine)) dest += sprintf(dest, "auto%2d/%d", effective_frameskip(), MAX_FRAMESKIP); /* otherwise, just display the frameskip plus the level */ else dest += sprintf(dest, "skip %d/%d", effective_frameskip(), MAX_FRAMESKIP); /* append the speed for all cases except paused */ if (!paused) dest += sprintf(dest, "%4d%%", (int)(100 * global.speed_percent + 0.5)); /* display the number of partial updates as well */ if (global.partial_updates_this_frame > 1) dest += sprintf(dest, "\n%d partial updates", global.partial_updates_this_frame); /* return a pointer to the static buffer */ return buffer; } /*------------------------------------------------- video_get_speed_percent - return the current effective speed percentage -------------------------------------------------*/ double video_get_speed_percent(running_machine *machine) { return global.speed_percent; } /*------------------------------------------------- video_get_frameskip - return the current actual frameskip (-1 means autoframeskip) -------------------------------------------------*/ int video_get_frameskip(void) { /* if autoframeskip is on, return -1 */ if (global.auto_frameskip) return -1; /* otherwise, return the direct level */ else return global.frameskip_level; } /*------------------------------------------------- video_set_frameskip - set the current actual frameskip (-1 means autoframeskip) -------------------------------------------------*/ void video_set_frameskip(int frameskip) { /* -1 means autoframeskip */ if (frameskip == -1) { global.auto_frameskip = TRUE; global.frameskip_level = 0; } /* any other level is a direct control */ else if (frameskip >= 0 && frameskip <= MAX_FRAMESKIP) { global.auto_frameskip = FALSE; global.frameskip_level = frameskip; } } /*------------------------------------------------- video_get_throttle - return the current actual throttle -------------------------------------------------*/ int video_get_throttle(void) { return global.throttle; } /*------------------------------------------------- video_set_throttle - set the current actual throttle -------------------------------------------------*/ void video_set_throttle(int throttle) { global.throttle = throttle; } /*------------------------------------------------- video_get_fastforward - return the current fastforward value -------------------------------------------------*/ int video_get_fastforward(void) { return global.fastforward; } /*------------------------------------------------- video_set_fastforward - set the current fastforward value -------------------------------------------------*/ void video_set_fastforward(int _fastforward) { global.fastforward = _fastforward; } /*------------------------------------------------- update_throttle - throttle to the game's natural speed -------------------------------------------------*/ static void update_throttle(running_machine *machine, attotime emutime) { /* Throttling theory: This routine is called periodically with an up-to-date emulated time. The idea is to synchronize real time with emulated time. We do this by "throttling", or waiting for real time to catch up with emulated time. In an ideal world, it will take less real time to emulate and render each frame than the emulated time, so we need to slow things down to get both times in sync. There are many complications to this model: * some games run too slow, so each frame we get further and further behind real time; our only choice here is to not throttle * some games have very uneven frame rates; one frame will take a long time to emulate, and the next frame may be very fast * we run on top of multitasking OSes; sometimes execution time is taken away from us, and this means we may not get enough time to emulate one frame * we may be paused, and emulated time may not be marching forward * emulated time could jump due to resetting the machine or restoring from a saved state */ static const UINT8 popcount[256] = { 0,1,1,2,1,2,2,3, 1,2,2,3,2,3,3,4, 1,2,2,3,2,3,3,4, 2,3,3,4,3,4,4,5, 1,2,2,3,2,3,3,4, 2,3,3,4,3,4,4,5, 2,3,3,4,3,4,4,5, 3,4,4,5,4,5,5,6, 1,2,2,3,2,3,3,4, 2,3,3,4,3,4,4,5, 2,3,3,4,3,4,4,5, 3,4,4,5,4,5,5,6, 2,3,3,4,3,4,4,5, 3,4,4,5,4,5,5,6, 3,4,4,5,4,5,5,6, 4,5,5,6,5,6,6,7, 1,2,2,3,2,3,3,4, 2,3,3,4,3,4,4,5, 2,3,3,4,3,4,4,5, 3,4,4,5,4,5,5,6, 2,3,3,4,3,4,4,5, 3,4,4,5,4,5,5,6, 3,4,4,5,4,5,5,6, 4,5,5,6,5,6,6,7, 2,3,3,4,3,4,4,5, 3,4,4,5,4,5,5,6, 3,4,4,5,4,5,5,6, 4,5,5,6,5,6,6,7, 3,4,4,5,4,5,5,6, 4,5,5,6,5,6,6,7, 4,5,5,6,5,6,6,7, 5,6,6,7,6,7,7,8 }; attoseconds_t real_delta_attoseconds; attoseconds_t emu_delta_attoseconds; attoseconds_t real_is_ahead_attoseconds; attoseconds_t attoseconds_per_tick; osd_ticks_t ticks_per_second; osd_ticks_t target_ticks; osd_ticks_t diff_ticks; /* apply speed factor to emu time */ if (global.speed != 0 && global.speed != 100) { /* multiply emutime by 100, then divide by the global speed factor */ emutime = attotime_div(attotime_mul(emutime, 100), global.speed); } /* compute conversion factors up front */ ticks_per_second = osd_ticks_per_second(); attoseconds_per_tick = ATTOSECONDS_PER_SECOND / ticks_per_second; /* if we're paused, emutime will not advance; instead, we subtract a fixed amount of time (1/60th of a second) from the emulated time that was passed in, and explicitly reset our tracked real and emulated timers to that value ... this means we pretend that the last update was exactly 1/60th of a second ago, and was in sync in both real and emulated time */ if (mame_is_paused(machine)) { global.throttle_emutime = attotime_sub_attoseconds(emutime, ATTOSECONDS_PER_SECOND / PAUSED_REFRESH_RATE); global.throttle_realtime = global.throttle_emutime; } /* attempt to detect anomalies in the emulated time by subtracting the previously reported value from our current value; this should be a small value somewhere between 0 and 1/10th of a second ... anything outside of this range is obviously wrong and requires a resync */ emu_delta_attoseconds = attotime_to_attoseconds(attotime_sub(emutime, global.throttle_emutime)); if (emu_delta_attoseconds < 0 || emu_delta_attoseconds > ATTOSECONDS_PER_SECOND / 10) { if (LOG_THROTTLE) logerror("Resync due to weird emutime delta: %s\n", attotime_string(attotime_make(0, emu_delta_attoseconds), 18)); goto resync; } /* now determine the current real time in OSD-specified ticks; we have to be careful here because counters can wrap, so we only use the difference between the last read value and the current value in our computations */ diff_ticks = osd_ticks() - global.throttle_last_ticks; global.throttle_last_ticks += diff_ticks; /* if it has been more than a full second of real time since the last call to this function, we just need to resynchronize */ if (diff_ticks >= ticks_per_second) { if (LOG_THROTTLE) logerror("Resync due to real time advancing by more than 1 second\n"); goto resync; } /* convert this value into attoseconds for easier comparison */ real_delta_attoseconds = diff_ticks * attoseconds_per_tick; /* now update our real and emulated timers with the current values */ global.throttle_emutime = emutime; global.throttle_realtime = attotime_add_attoseconds(global.throttle_realtime, real_delta_attoseconds); /* keep a history of whether or not emulated time beat real time over the last few updates; this can be used for future heuristics */ global.throttle_history = (global.throttle_history << 1) | (emu_delta_attoseconds > real_delta_attoseconds); /* determine how far ahead real time is versus emulated time; note that we use the accumulated times for this instead of the deltas for the current update because we want to track time over a longer duration than a single update */ real_is_ahead_attoseconds = attotime_to_attoseconds(attotime_sub(global.throttle_emutime, global.throttle_realtime)); /* if we're more than 1/10th of a second out, or if we are behind at all and emulation is taking longer than the real frame, we just need to resync */ if (real_is_ahead_attoseconds < -ATTOSECONDS_PER_SECOND / 10 || (real_is_ahead_attoseconds < 0 && popcount[global.throttle_history & 0xff] < 6)) { if (LOG_THROTTLE) logerror("Resync due to being behind: %s (history=%08X)\n", attotime_string(attotime_make(0, -real_is_ahead_attoseconds), 18), global.throttle_history); goto resync; } /* if we're behind, it's time to just get out */ if (real_is_ahead_attoseconds < 0) return; /* compute the target real time, in ticks, where we want to be */ target_ticks = global.throttle_last_ticks + real_is_ahead_attoseconds / attoseconds_per_tick; /* throttle until we read the target, and update real time to match the final time */ diff_ticks = throttle_until_ticks(machine, target_ticks) - global.throttle_last_ticks; global.throttle_last_ticks += diff_ticks; global.throttle_realtime = attotime_add_attoseconds(global.throttle_realtime, diff_ticks * attoseconds_per_tick); return; resync: /* reset realtime and emutime to the same value */ global.throttle_realtime = global.throttle_emutime = emutime; } /*------------------------------------------------- throttle_until_ticks - spin until the specified target time, calling the OSD code to sleep if possible -------------------------------------------------*/ static osd_ticks_t throttle_until_ticks(running_machine *machine, osd_ticks_t target_ticks) { osd_ticks_t minimum_sleep = osd_ticks_per_second() / 1000; osd_ticks_t current_ticks = osd_ticks(); osd_ticks_t new_ticks; int allowed_to_sleep = FALSE; /* we're allowed to sleep via the OSD code only if we're configured to do so and we're not frameskipping due to autoframeskip, or if we're paused */ if (options_get_bool(mame_options(), OPTION_SLEEP) && (!effective_autoframeskip(machine) || effective_frameskip() == 0)) allowed_to_sleep = TRUE; if (mame_is_paused(machine)) allowed_to_sleep = TRUE; /* loop until we reach our target */ profiler_mark(PROFILER_IDLE); while (current_ticks < target_ticks) { osd_ticks_t delta; int slept = FALSE; /* compute how much time to sleep for, taking into account the average oversleep */ delta = (target_ticks - current_ticks) * 1000 / (1000 + global.average_oversleep); /* see if we can sleep */ if (allowed_to_sleep && delta >= minimum_sleep) { osd_sleep(delta); slept = TRUE; } /* read the new value */ new_ticks = osd_ticks(); /* keep some metrics on the sleeping patterns of the OSD layer */ if (slept) { osd_ticks_t actual_ticks = new_ticks - current_ticks; /* if we overslept, keep an average of the amount */ if (actual_ticks > delta) { osd_ticks_t oversleep_milliticks = 1000 * (actual_ticks - delta) / delta; /* take 90% of the previous average plus 10% of the new value */ global.average_oversleep = (global.average_oversleep * 99 + oversleep_milliticks) / 100; if (LOG_THROTTLE) logerror("Slept for %d ticks, got %d ticks, avgover = %d\n", (int)delta, (int)actual_ticks, (int)global.average_oversleep); } } current_ticks = new_ticks; } profiler_mark(PROFILER_END); return current_ticks; } /*------------------------------------------------- update_frameskip - update frameskipping counters and periodically update autoframeskip -------------------------------------------------*/ static void update_frameskip(running_machine *machine) { /* if we're throttling and autoframeskip is on, adjust */ if (effective_throttle(machine) && effective_autoframeskip(machine) && global.frameskip_counter == 0) { double speed = global.speed * 0.01; /* if we're too fast, attempt to increase the frameskip */ if (global.speed_percent >= 0.995 * speed) { /* but only after 3 consecutive frames where we are too fast */ if (++global.frameskip_adjust >= 3) { global.frameskip_adjust = 0; if (global.frameskip_level > 0) global.frameskip_level--; } } /* if we're too slow, attempt to increase the frameskip */ else { /* if below 80% speed, be more aggressive */ if (global.speed_percent < 0.80 * speed) global.frameskip_adjust -= (0.90 * speed - global.speed_percent) / 0.05; /* if we're close, only force it up to frameskip 8 */ else if (global.frameskip_level < 8) global.frameskip_adjust--; /* perform the adjustment */ while (global.frameskip_adjust <= -2) { global.frameskip_adjust += 2; if (global.frameskip_level < MAX_FRAMESKIP) global.frameskip_level++; } } } /* increment the frameskip counter and determine if we will skip the next frame */ global.frameskip_counter = (global.frameskip_counter + 1) % FRAMESKIP_LEVELS; global.skipping_this_frame = skiptable[effective_frameskip()][global.frameskip_counter]; } /*------------------------------------------------- update_refresh_speed - update the global.speed based on the maximum refresh rate supported -------------------------------------------------*/ static void update_refresh_speed(running_machine *machine) { /* only do this if the refreshspeed option is used */ if (options_get_bool(mame_options(), OPTION_REFRESHSPEED)) { float minrefresh = render_get_max_update_rate(); if (minrefresh != 0) { attoseconds_t min_frame_period = ATTOSECONDS_PER_SECOND; UINT32 original_speed = original_speed_setting(); const device_config *screen; UINT32 target_speed; /* find the screen with the shortest frame period (max refresh rate) */ /* note that we first check the token since this can get called before all screens are created */ for (screen = video_screen_first(machine->config); screen != NULL; screen = video_screen_next(screen)) if (screen->token != NULL) { screen_state *state = get_safe_token(screen); if (state->frame_period != 0) min_frame_period = MIN(min_frame_period, state->frame_period); } /* compute a target speed as an integral percentage */ /* note that we lop 0.25Hz off of the minrefresh when doing the computation to allow for the fact that most refresh rates are not accurate to 10 digits... */ target_speed = floor((minrefresh - 0.25f) * 100.0 / ATTOSECONDS_TO_HZ(min_frame_period)); target_speed = MIN(target_speed, original_speed); /* if we changed, log that verbosely */ if (target_speed != global.speed) { mame_printf_verbose("Adjusting target speed to %d%% (hw=%.2fHz, game=%.2fHz, adjusted=%.2fHz)\n", target_speed, minrefresh, ATTOSECONDS_TO_HZ(min_frame_period), ATTOSECONDS_TO_HZ(min_frame_period * 100 / target_speed)); global.speed = target_speed; } } } } /*------------------------------------------------- recompute_speed - recompute the current overall speed; we assume this is called only if we did not skip a frame -------------------------------------------------*/ static void recompute_speed(running_machine *machine, attotime emutime) { attoseconds_t delta_emutime; /* if we don't have a starting time yet, or if we're paused, reset our starting point */ if (global.speed_last_realtime == 0 || mame_is_paused(machine)) { global.speed_last_realtime = osd_ticks(); global.speed_last_emutime = emutime; } /* if it has been more than the update interval, update the time */ delta_emutime = attotime_to_attoseconds(attotime_sub(emutime, global.speed_last_emutime)); if (delta_emutime > SUBSECONDS_PER_SPEED_UPDATE) { osd_ticks_t realtime = osd_ticks(); osd_ticks_t delta_realtime = realtime - global.speed_last_realtime; osd_ticks_t tps = osd_ticks_per_second(); /* convert from ticks to attoseconds */ global.speed_percent = (double)delta_emutime * (double)tps / ((double)delta_realtime * (double)ATTOSECONDS_PER_SECOND); #ifdef SOUND_RESYNC machine->speed_percent = global.speed_percent; #endif /* SOUND_RESYNC */ /* remember the last times */ global.speed_last_realtime = realtime; global.speed_last_emutime = emutime; /* if we're throttled, this time period counts for overall speed; otherwise, we reset the counter */ if (!global.fastforward) global.overall_valid_counter++; else global.overall_valid_counter = 0; /* if we've had at least 4 consecutive valid periods, accumulate stats */ if (global.overall_valid_counter >= 4) { global.overall_real_ticks += delta_realtime; while (global.overall_real_ticks >= tps) { global.overall_real_ticks -= tps; global.overall_real_seconds++; } global.overall_emutime = attotime_add_attoseconds(global.overall_emutime, delta_emutime); } } /* if we're past the "time-to-execute" requested, signal an exit */ if (global.seconds_to_run != 0 && emutime.seconds >= global.seconds_to_run) { if (machine->primary_screen != NULL) { astring *fname = astring_assemble_2(astring_alloc(), machine->basename, PATH_SEPARATOR "final.png"); file_error filerr; mame_file *file; /* create a final screenshot */ filerr = mame_fopen(SEARCHPATH_SCREENSHOT, astring_c(fname), OPEN_FLAG_WRITE | OPEN_FLAG_CREATE | OPEN_FLAG_CREATE_PATHS, &file); if (filerr == FILERR_NONE) { video_screen_save_snapshot(machine, machine->primary_screen, file); mame_fclose(file); } astring_free(fname); } /* schedule our demise */ mame_schedule_exit(machine); } } /*************************************************************************** SCREEN SNAPSHOTS ***************************************************************************/ /*------------------------------------------------- video_screen_save_snapshot - save a snapshot to the given file handle -------------------------------------------------*/ void video_screen_save_snapshot(running_machine *machine, const device_config *screen, mame_file *fp) { png_info pnginfo = { 0 }; const rgb_t *palette; png_error error; char text[256]; /* validate */ assert(!global.snap_native || screen != NULL); assert(fp != NULL); /* create the bitmap to pass in */ create_snapshot_bitmap(screen); /* add two text entries describing the image */ sprintf(text, APPNAME " %s", build_version); png_add_text(&pnginfo, "Software", text); sprintf(text, "%s %s", machine->gamedrv->manufacturer, machine->gamedrv->description); png_add_text(&pnginfo, "System", text); /* now do the actual work */ palette = (machine->palette != NULL) ? palette_entry_list_adjusted(machine->palette) : NULL; error = png_write_bitmap(mame_core_file(fp), &pnginfo, global.snap_bitmap, machine->config->total_colors, palette); /* free any data allocated */ png_free(&pnginfo); } /*------------------------------------------------- video_save_active_screen_snapshots - save a snapshot of all active screens -------------------------------------------------*/ void video_save_active_screen_snapshots(running_machine *machine) { mame_file *fp; const device_config *screen; /* validate */ assert(machine != NULL); assert(machine->config != NULL); /* if we're native, then write one snapshot per visible screen */ if (global.snap_native) { /* write one snapshot per visible screen */ for (screen = video_screen_first(machine->config); screen != NULL; screen = video_screen_next(screen)) if (render_is_live_screen(screen)) { file_error filerr = mame_fopen_next(machine, SEARCHPATH_SCREENSHOT, "png", &fp); if (filerr == FILERR_NONE) { video_screen_save_snapshot(machine, screen, fp); mame_fclose(fp); } } } /* otherwise, just write a single snapshot */ else { file_error filerr = mame_fopen_next(machine, SEARCHPATH_SCREENSHOT, "png", &fp); if (filerr == FILERR_NONE) { video_screen_save_snapshot(machine, NULL, fp); mame_fclose(fp); } } } /*------------------------------------------------- creare_snapshot_bitmap - creates a bitmap containing the screenshot for the given screen -------------------------------------------------*/ static void create_snapshot_bitmap(const device_config *screen) { const render_primitive_list *primlist; INT32 width, height; int view_index; /* select the appropriate view in our dummy target */ if (global.snap_native && screen != NULL) { view_index = device_list_index(screen->machine->config->devicelist, VIDEO_SCREEN, screen->tag); assert(view_index != -1); render_target_set_view(global.snap_target, view_index); } /* get the minimum width/height and set it on the target */ width = global.snap_width; height = global.snap_height; if (width == 0 || height == 0) render_target_get_minimum_size(global.snap_target, &width, &height); render_target_set_bounds(global.snap_target, width, height, 0); /* if we don't have a bitmap, or if it's not the right size, allocate a new one */ if (global.snap_bitmap == NULL || width != global.snap_bitmap->width || height != global.snap_bitmap->height) { if (global.snap_bitmap != NULL) bitmap_free(global.snap_bitmap); global.snap_bitmap = bitmap_alloc(width, height, BITMAP_FORMAT_RGB32); assert(global.snap_bitmap != NULL); } /* render the screen there */ primlist = render_target_get_primitives(global.snap_target); osd_lock_acquire(primlist->lock); rgb888_draw_primitives(primlist->head, global.snap_bitmap->base, width, height, global.snap_bitmap->rowpixels); osd_lock_release(primlist->lock); } /*------------------------------------------------- mame_fopen_next - open the next non-existing file of type filetype according to our numbering scheme -------------------------------------------------*/ static file_error mame_fopen_next(running_machine *machine, const char *pathoption, const char *extension, mame_file **file) { const char *snapname = options_get_string(mame_options(), OPTION_SNAPNAME); astring *snapstr = astring_alloc(); astring *fname = astring_alloc(); file_error filerr; int index; /* handle defaults */ if (snapname == NULL || snapname[0] == 0) snapname = "%g/%i"; astring_cpyc(snapstr, snapname); /* strip any extension in the provided name and add our own */ index = astring_rchr(snapstr, 0, '.'); if (index != -1) astring_substr(snapstr, 0, index); astring_catc(snapstr, "."); astring_catc(snapstr, extension); /* substitute path and gamename up front */ astring_replacec(snapstr, 0, "/", PATH_SEPARATOR); astring_replacec(snapstr, 0, "%g", machine->basename); /* determine if the template has an index; if not, we always use the same name */ if (astring_findc(snapstr, 0, "%i") == -1) astring_cpy(fname, snapstr); /* otherwise, we scan for the next available filename */ else { int seq; /* try until we succeed */ for (seq = 0; ; seq++) { char seqtext[10]; /* make text for the sequence number */ sprintf(seqtext, "%04d", seq); /* build up the filename */ astring_cpy(fname, snapstr); astring_replacec(fname, 0, "%i", seqtext); /* try to open the file; stop when we fail */ filerr = mame_fopen(pathoption, astring_c(fname), OPEN_FLAG_READ, file); if (filerr != FILERR_NONE) break; mame_fclose(*file); } } /* create the final file */ filerr = mame_fopen(pathoption, astring_c(fname), OPEN_FLAG_WRITE | OPEN_FLAG_CREATE | OPEN_FLAG_CREATE_PATHS, file); /* free the name and get out */ astring_free(fname); astring_free(snapstr); return filerr; } /*************************************************************************** MNG MOVIE RECORDING ***************************************************************************/ /*------------------------------------------------- video_mng_is_movie_active - return true if a MNG movie is currently being recorded -------------------------------------------------*/ int video_mng_is_movie_active(running_machine *machine) { return (global.mngfile != NULL); } /*------------------------------------------------- video_mng_begin_recording - begin recording of a MNG movie -------------------------------------------------*/ void video_mng_begin_recording(running_machine *machine, const char *name) { screen_state *state = NULL; file_error filerr; png_error pngerr; int rate; /* close any existing movie file */ if (global.mngfile != NULL) video_mng_end_recording(machine); /* look up the primary screen */ if (machine->primary_screen != NULL) state = get_safe_token(machine->primary_screen); /* create a snapshot bitmap so we know what the target size is */ create_snapshot_bitmap(NULL); /* create a new movie file and start recording */ if (name != NULL) filerr = mame_fopen(SEARCHPATH_MOVIE, name, OPEN_FLAG_WRITE | OPEN_FLAG_CREATE | OPEN_FLAG_CREATE_PATHS, &global.mngfile); else filerr = mame_fopen_next(machine, SEARCHPATH_MOVIE, "mng", &global.mngfile); /* start the capture */ rate = (state != NULL) ? ATTOSECONDS_TO_HZ(state->frame_period) : DEFAULT_FRAME_RATE; pngerr = mng_capture_start(mame_core_file(global.mngfile), global.snap_bitmap, rate); if (pngerr != PNGERR_NONE) { video_mng_end_recording(machine); return; } /* compute the frame time */ global.movie_next_frame_time = timer_get_time(machine); global.movie_frame_period = ATTOTIME_IN_HZ(rate); global.movie_frame = 0; } /*------------------------------------------------- video_mng_end_recording - stop recording of a MNG movie -------------------------------------------------*/ void video_mng_end_recording(running_machine *machine) { /* close the file if it exists */ if (global.mngfile != NULL) { mng_capture_stop(mame_core_file(global.mngfile)); mame_fclose(global.mngfile); global.mngfile = NULL; global.movie_frame = 0; } } /*------------------------------------------------- video_mng_record_frame - record a frame of a movie -------------------------------------------------*/ static void video_mng_record_frame(running_machine *machine) { /* only record if we have a file */ if (global.mngfile != NULL) { attotime curtime = timer_get_time(machine); png_info pnginfo = { 0 }; png_error error; profiler_mark(PROFILER_MOVIE_REC); /* create the bitmap */ create_snapshot_bitmap(NULL); /* loop until we hit the right time */ while (attotime_compare(global.movie_next_frame_time, curtime) <= 0) { const rgb_t *palette; /* set up the text fields in the movie info */ if (global.movie_frame == 0) { char text[256]; sprintf(text, APPNAME " %s", build_version); png_add_text(&pnginfo, "Software", text); sprintf(text, "%s %s", machine->gamedrv->manufacturer, machine->gamedrv->description); png_add_text(&pnginfo, "System", text); } /* write the next frame */ palette = (machine->palette != NULL) ? palette_entry_list_adjusted(machine->palette) : NULL; error = mng_capture_frame(mame_core_file(global.mngfile), &pnginfo, global.snap_bitmap, machine->config->total_colors, palette); png_free(&pnginfo); if (error != PNGERR_NONE) { video_mng_end_recording(machine); break; } /* advance time */ global.movie_next_frame_time = attotime_add(global.movie_next_frame_time, global.movie_frame_period); global.movie_frame++; } profiler_mark(PROFILER_END); } } /*************************************************************************** AVI MOVIE RECORDING ***************************************************************************/ /*------------------------------------------------- video_avi_begin_recording - begin recording of an AVI movie -------------------------------------------------*/ void video_avi_begin_recording(running_machine *machine, const char *name) { screen_state *state = NULL; avi_movie_info info; mame_file *tempfile; file_error filerr; avi_error avierr; /* close any existing movie file */ if (global.avifile != NULL) video_avi_end_recording(machine); /* look up the primary screen */ if (machine->primary_screen != NULL) state = get_safe_token(machine->primary_screen); /* create a snapshot bitmap so we know what the target size is */ create_snapshot_bitmap(NULL); /* build up information about this new movie */ info.video_format = 0; info.video_timescale = 1000 * ((state != NULL) ? ATTOSECONDS_TO_HZ(state->frame_period) : DEFAULT_FRAME_RATE); info.video_sampletime = 1000; info.video_numsamples = 0; info.video_width = global.snap_bitmap->width; info.video_height = global.snap_bitmap->height; info.video_depth = 24; info.audio_format = 0; info.audio_timescale = machine->sample_rate; info.audio_sampletime = 1; info.audio_numsamples = 0; info.audio_channels = 2; info.audio_samplebits = 16; info.audio_samplerate = machine->sample_rate; /* create a new temporary movie file */ if (name != NULL) filerr = mame_fopen(SEARCHPATH_MOVIE, name, OPEN_FLAG_WRITE | OPEN_FLAG_CREATE | OPEN_FLAG_CREATE_PATHS, &tempfile); else filerr = mame_fopen_next(machine, SEARCHPATH_MOVIE, "avi", &tempfile); /* reset our tracking */ global.movie_frame = 0; global.movie_next_frame_time = timer_get_time(machine); global.movie_frame_period = attotime_div(ATTOTIME_IN_SEC(1000), info.video_timescale); /* if we succeeded, make a copy of the name and create the real file over top */ if (filerr == FILERR_NONE) { astring *fullname = astring_dupc(mame_file_full_name(tempfile)); mame_fclose(tempfile); /* create the file and free the string */ avierr = avi_create(astring_c(fullname), &info, &global.avifile); astring_free(fullname); } } /*------------------------------------------------- video_avi_end_recording - stop recording of a avi movie -------------------------------------------------*/ void video_avi_end_recording(running_machine *machine) { /* close the file if it exists */ if (global.avifile != NULL) { avi_close(global.avifile); global.avifile = NULL; global.movie_frame = 0; } } /*------------------------------------------------- video_avi_record_frame - record a frame of a movie -------------------------------------------------*/ static void video_avi_record_frame(running_machine *machine) { /* only record if we have a file */ if (global.avifile != NULL) { attotime curtime = timer_get_time(machine); avi_error avierr; profiler_mark(PROFILER_MOVIE_REC); /* create the bitmap */ create_snapshot_bitmap(NULL); /* loop until we hit the right time */ while (attotime_compare(global.movie_next_frame_time, curtime) <= 0) { /* write the next frame */ avierr = avi_append_video_frame_rgb32(global.avifile, global.snap_bitmap); if (avierr != AVIERR_NONE) { video_avi_end_recording(machine); break; } /* advance time */ global.movie_next_frame_time = attotime_add(global.movie_next_frame_time, global.movie_frame_period); global.movie_frame++; } profiler_mark(PROFILER_END); } } /*------------------------------------------------- video_avi_add_sound - add sound to an AVI recording -------------------------------------------------*/ void video_avi_add_sound(running_machine *machine, const INT16 *sound, int numsamples) { /* only record if we have a file */ if (global.avifile != NULL) { avi_error avierr; profiler_mark(PROFILER_MOVIE_REC); /* write the next frame */ avierr = avi_append_sound_samples(global.avifile, 0, sound + 0, numsamples, 1); if (avierr == AVIERR_NONE) avierr = avi_append_sound_samples(global.avifile, 1, sound + 1, numsamples, 1); if (avierr != AVIERR_NONE) video_avi_end_recording(machine); profiler_mark(PROFILER_END); } } /*************************************************************************** CONFIGURATION HELPERS ***************************************************************************/ /*------------------------------------------------- video_get_view_for_target - select a view for a given target -------------------------------------------------*/ int video_get_view_for_target(running_machine *machine, render_target *target, const char *viewname, int targetindex, int numtargets) { int viewindex = -1; /* auto view just selects the nth view */ if (strcmp(viewname, "auto") != 0) { /* scan for a matching view name */ for (viewindex = 0; ; viewindex++) { const char *name = render_target_get_view_name(target, viewindex); /* stop scanning when we hit NULL */ if (name == NULL) { viewindex = -1; break; } if (mame_strnicmp(name, viewname, strlen(viewname)) == 0) break; } } /* if we don't have a match, default to the nth view */ if (viewindex == -1) { int scrcount = video_screen_count(machine->config); /* if we have enough targets to be one per screen, assign in order */ if (numtargets >= scrcount) { /* find the first view with this screen and this screen only */ for (viewindex = 0; ; viewindex++) { UINT32 viewscreens = render_target_get_view_screens(target, viewindex); if (viewscreens == (1 << targetindex)) break; if (viewscreens == 0) { viewindex = -1; break; } } } /* otherwise, find the first view that has all the screens */ if (viewindex == -1) { for (viewindex = 0; ; viewindex++) { UINT32 viewscreens = render_target_get_view_screens(target, viewindex); if (viewscreens == (1 << scrcount) - 1) break; if (viewscreens == 0) break; } } } /* make sure it's a valid view */ if (render_target_get_view_name(target, viewindex) == NULL) viewindex = 0; return viewindex; } /*************************************************************************** DEBUGGING HELPERS ***************************************************************************/ /*------------------------------------------------- video_assert_out_of_range_pixels - assert if any pixels in the given bitmap contain an invalid palette index -------------------------------------------------*/ void video_assert_out_of_range_pixels(running_machine *machine, bitmap_t *bitmap) { #ifdef MAME_DEBUG int maxindex = palette_get_max_index(machine->palette); int x, y; /* this only applies to indexed16 bitmaps */ if (bitmap->format != BITMAP_FORMAT_INDEXED16) return; /* iterate over rows */ for (y = 0; y < bitmap->height; y++) { UINT16 *rowbase = BITMAP_ADDR16(bitmap, y, 0); for (x = 0; x < bitmap->width; x++) assert(rowbase[x] < maxindex); } #endif } /*************************************************************************** SOFTWARE RENDERING ***************************************************************************/ #define FUNC_PREFIX(x) rgb888_##x #define PIXEL_TYPE UINT32 #define SRCSHIFT_R 0 #define SRCSHIFT_G 0 #define SRCSHIFT_B 0 #define DSTSHIFT_R 16 #define DSTSHIFT_G 8 #define DSTSHIFT_B 0 #define BILINEAR_FILTER 1 #include "rendersw.c"
  9. Bom pessoal, eu preciso desenvolver um algoritmo que determine o inicio de um evento em data, e hora, o final do evento, e tire a media, ou seja, a duração do evento, porém eu preciso fazer isso sem biblioteca, alguem poderia me dar uma luz, não tenho que definir o ano, o evento ele pode começar no ano 1500 e terminar em 2019 por exemplo.
  10. Preciso de ajuda... por favor! Tenho que fazer um programa que gere uma senha de 3 dígitos numéricos (0 a 9) de forma aleatória. O algoritmo deve tentar “quebrar” essa senha. O tempo de “quebra” da senha deve ser contabilizado. Agradecida desde já.
  11. Olá galera estou precisando de uma ajuda, tenho um projeto de estoque para mero aprendizado, e cheguei em uma parte que não sei por onde começar a pesquisar, nem sei se é possível, mas sabendo que da pra fazer ou por onde começar a ver algo sobre, já ta de bom tamanho. Já consegui fazer a pesquisa por nome abrindo o arquivo que o usuário desejar, mas gostaria de algo mas pratico, algo como uma lista. 1)Quero que leia todos os arquivos txt contidos na pasta //não o nome, todo o seu conteúdo; 2)Imprima na tela todo o conteúdo de todos os arquivos; 3)Os nomes desses arquivos não podem ser colocados diretamente no código Não postei o código porque nem comecei essa parte em questão, estou perdido hehe, se caso alguém precisar eu posto, muito obrigado desde já.
  12. Preciso de algum site que tenha exercícios,listas,projetos para realizar em C++. Já conheço e fiz vários do URI ONLINE JUDGE
  13. Bom dia, Estou fazendo um projeto que estou usando uma RFID do arduino, porém preciso de um temporizador para ser usado junto com a RFID. Alguém pode me ajudar
  14. Fala galera beleza? Bom comecei na facul Estrutura de dados e o professor usar C++, como eu utilizava o C estou com algumas dificuldades, como nesse algoritmo #include <iostream> #include <stdlib.h> using namespace std; void Bubble(int v[],int n) { int i,j,guarda,trocou,k=n; while(1){ trocou = 0; cout <<"\nRepete: "<<n-k+1<<": "; for(i=0; i<k-1; i++){ if(v[i] > v[i+1]) { trocou =1; guarda = v[i]; v[i] = v[i+1]; v[i+1] = guarda; } // FIM IF } // FIM FOR for(i=0; i<k; i++) cout<<" "<<v[i]; k--; if(!(trocou)) return ; } // FIM WHILE }// FIM BUBBLE int main() { int i, n; cout<<"Digite quantos valores tem o vetor: "; cin >> n; int v[n]; for(i=0; i<n; i++) { cout<<"Entre com o valor para V["<< i <<"]: "; cin>> v[i]; } // FIM FOR cout<<"Vetor desordenado: "; for(i=0; i<n; i++) cout<<" "<<v[i]; cout<<"\n"; Bubble(v,n); cout <<"\nVetor ordenado: \n"; for(i=0; i<n; i++) cout<<" "<<v[i]; cout<<endl; } // FIM MAIN Quando eu executo ele da um Erro "ISO C++ forbids variable length array 'v' [-Wvla]", eu não sei o motivo, se alguem souber pode me ajudar ?
  15. Bem pessoal é o seguinte, estou com um grande dúvida na hora de escrever um código para o esp32 (ide do arduino). É o seguinte meu programa vai ler sensores que depois vai fazer comparações entre eles e depois vai ativar dispositivos externos até chegar a condição desejada. Então eu pensei em dividir o meu código em partes, criar funções, para facilitar até porque serão vários sensores. Até ai tudo bem. O meu problema é como eu crio, eu até sei fazer algumas mas são bem básicas, e como chamá-las no loop. Por exemplo: Temos um sensor de umidade. #define sensorPin 10 void setup(){ } void loop (){ valorAnalogico = analogRead(sensorPIN) if(valorAnalogico>300 && valorAnalogico < 600){ FAZER ISSO } if(valorAnalogico>900){ FAZER ISSO } } Isso séria um código para um sensor. Veja que eu não criei uma função eu coloquei tudo no loop. Caso eu quisesse colocar isso numa função como eu faria? O meu projeto tem uns 2 sensores de umidade e vários outros, por exemplo o de temperatura, caso eu queria comparar os valores os dois sensores de umidade para acionar tal coisa como eu faria? Lembrando que eu quero separar os sensores por funções. Se eu quisesse comparar os valores dos dois sensores eu teria que criar duas funções, tipo int sensorUmid e int sensorUmid2, e depois fazer a comparação no loop? Agradeço desde já
  16. ola to com problema na hora de chamar o form2 . no form1 tá o código #include "stdafx.h" #include "form2.h" e quando o botão for clicado this->Hide(); form2^ f2 = gcnew form2(); f2->ShowDialog(); quando coloco botão(ou qualquer coisa) no form2 não compila quando o form2 tá vazio funciona normal to usando o visual c++ 2010(antigo porque meu pc é velho).
  17. Estou tentando testar o tempo de execução de um código, porém estou sempre obtendo valores incorretos, o primeiro teste vai ser sempre o que tem o pior tempo. E na maioria das vezes o segundo teste é sempre 0. #include <iostream> #include <math.h> #include <intrin.h> #include <chrono> using namespace std; #define MAX_LOOP 100000 #define NUM 10000.f auto sse_sqrt( float n ) { __m128 reg = _mm_load_ss( &n ); return _mm_mul_ss( reg, _mm_rsqrt_ss( reg ) ).m128_f32[ 0 ]; } auto stl_sqrt_timer() { auto start = std::chrono::high_resolution_clock::now(); for ( auto i = 0; i < MAX_LOOP; i++ ) { auto v = std::sqrt( NUM ); } auto end = std::chrono::high_resolution_clock::now(); return ( end - start ).count(); } auto sse_sqrt_timer() { auto start = std::chrono::high_resolution_clock::now(); for ( auto i = 0; i < MAX_LOOP; i++ ) { auto v = sse_sqrt( NUM ); } auto end = std::chrono::high_resolution_clock::now(); return ( end - start ).count(); } int main() { cout << "stl_sqrt: " << stl_sqrt_timer() << "\n"; cout << "sse_sqrt: " << sse_sqrt_timer() << "\n"; cin.ignore(); return 0; } Primeira execução: sse_sqrt: 12461 stl_sqrt: 0 Segunda execução: sse_sqrt: 2643 stl_sqrt: 378 Invertendo a ordem de testes: Primeira execução: stl_sqrt: 23032 sse_sqrt: 378 Segunda execução: stl_sqrt: 2265 sse_sqrt: 0 Estou compilando em Release x86, com otimização /Ox
  18. Programa discord Galera,eu achei um jogo legal chamado dayz armageddon,foi feito de uma versao retirada de 2014,o meu problema é que toda vez que eu inicio o launcher ele mostra no discord normal (jogando dayz armageddon) o problema que as vezes quando eu fecho o dayz ele ainda mostra que ainda to jogando,ou quando vou abrir alguma outra coisa ele fala que ainda to jogando dayz armageddon,a unica coisa que descobri é esse programa,quando eu abro ele,ele fala que to jogando o dayz,quando fecho ele,ele volta ao normal,alquem poderia me ajudar a solucionar o problema ?
  19. Olá galera, preciso fazer um programa que no menu tenha as seguintes opções: 1. cadastrar pessoa (codigo, nome, cpf, idade) 2. Listar pessoas 3. encerrar programa Sou meio leigo, então não sei bem o que estou fazendo de errado, mas já fiz o seguinte: #include <iostream> #include <string.h> struct Pessoa { int codigoPessoa; char cpf[15]; // NNN.NNN.NNN-NN char nome[30]; int idade; }; Pessoa p1, listaPessoas[5]; // 2 variaveis do tipo Pessoa FILE * arq; int i; int cadastraPessoa() { Pessoa p1, listaPessoas[5]; // 2 variaveis do tipo Pessoa FILE * arq; int i; // DADOS DA PESSOA p1.codigoPessoa = 123; strcpy(p1.cpf,"123.456.789-00"); strcpy(p1.nome,"Marcos"); p1.idade = 20; arq = fopen("pessoa.dat","wb"); // write binario if (arq == NULL) { cout << "Erro na criacao do arquivo!"; return 0; } // gravacao binaria fwrite(&p1, sizeof(struct Pessoa), 1, arq); cout << "Gravando arquivo...." << endl << endl; fclose(arq); // ACRESCENTAR PESSOAS arq = fopen("pessoa.dat","ab"); // write binario if (arq == NULL) { cout << "Erro na criacao do arquivo!"; return 0; } fwrite(&p1, sizeof(struct Pessoa), 1, arq); cout << "Gravando arquivo...." << endl << endl; fwrite(&p1, sizeof(struct Pessoa), 1, arq); cout << "Gravando arquivo...." << endl << endl; fwrite(&p1, sizeof(struct Pessoa), 1, arq); cout << "Gravando arquivo...." << endl << endl; fclose(arq); return 0; } int listaPessoas() { arq = fopen("pessoa.dat","rb"); // read binario if (arq == NULL) { cout << "Erro na abertura do arquivo!"; return 0; } fread(&listaPessoas[0], sizeof(struct Pessoa), 4, arq); cout << "Lendo arquivo...." << endl << endl; for(i=0;i<4;i++) { cout << "Codigo: " << listaPessoas.codigoPessoa << endl; cout << "CPF: " << listaPessoas.cpf << endl; cout << "Nome: " << listaPessoas.nome << endl; cout << "Idade: " << listaPessoas.idade << endl << endl; } fclose(arq); return 0; } int main() { int opc; // opcao escolhida no menu do { cout << "1. Cadastrar pessoa" << endl; cout << "2. Listar pessoa cadartrada" << endl; cout << "3. Encerrar o programa" << endl; cout << "escolha opcao: "; cin >> opc; swtich (opc) { case 1: cadastraPessoa(); break; case 2: listaPessoas(); break; case 3: cout << "Encerrando o programa..."; break; } } return 0; } caso queiram a especificação mais detalhada, segue o anexo: MATA37_TP3_2018_1.pdf
  20. Preciso de uma dica, bom tenho uma questão na qual eu vou ler uma quantidade de frases indefinidas(preferencialmente char ) na qual o limite de caracteres é de (100) e apos isso eu mostre as mesmas frases invertidas e uma opção de remover a ultima frase digitada . #include <iostream> #include <cstring> #include <limits> #define t 100 using namespace std; char nome[t]; int x,tamanho,cont=0; void inserir(){ char limite; cout << endl; limite = cin.get(); cout <<" Digite sua frase "<< endl; while (limite != '\n') { cin.get(nome,t); nome[cont] = limite; cont++; } } void mostrar(){ for (x=strlen(nome); x >-1; x--) { cout << nome[x]<< endl; } } int main() { int menu; while(true) { cout << "1-Insira sua frase "<< endl<< "2-Remover da fila "<< endl << "3-Mostrar"<< endl << "4-Sair"<< endl; cin >> menu; system("clear"); if(menu==1) { inserir(); } /* if(menu==2) { remover(); }*/ if(menu==3) { mostrar(); } if(menu==4) { return 616; } } } Cheguei em algo assim mas não funciona então não da para seguir essa logica .
  21. Olá! Estou começando a aprender a programar em C++, mas não sei por onde começar perante ao meu objetivo, então... Estou com a intenção de criar um jogo baseado em Tibia e Ultima Online, point click, 2 dimensões e ângulo 45º (visão de cima), mas não sei quais programas utilizar para este meu projeto, quero um jogo com Client e servidor separados, bases de dados em MySQL (sem ser phpmyadmin, mas se for necessário sem problema). Já estudei muito criação de ots de Tibia, caso conheçam, quero um sistema parecido (mas não uma modificação!) Não estou interessado em programas de programação em C++, claramente conheço e uso vários, conversores, já estou acostumado, mas, se for necessário um específico para o programa que me mandarem, não hesitem! Como eu disse, estou num nível iniciante de programação, então também seria grato se tiverem tutoriais e artigos (grátis, se possível ) para passar também! Obrigado quem puder me ajudar agradeço, caso precisem de mais informações responderei o mais rápido possível! P.S.: Uma base de jogo seria ótimo, para me familiarizar mais com o sistema, e se possível passem links para os programas (que funcionaram parar vocês!) P.S.2: Se eu estiver cometendo alguma infração das regras do fórum por favor sejam pacientes, esse é meu primeiro tópico
  22. Pessoal, tudo bem? alguém poderia por gentileza me indicar o erro deste algoritmo que verifica se o número é primo ou não? O erro é que a função só retorna true! bool primo(int n) { for(int i = 2; i < int(sqrt(n)); i++) if(n % i == 0) return false; return true; } int main() { std::cout << primo(5); }
  23. Pessoal tudo bem? Alguém sabe me informar se o codeblocks funciona para C++? fiz um código aqui mas não está compilando...
  24. C++ Exibir na tela a/b

    Como faço para exibir um resultado em formato de fração: a/b? cout << a << "/" << b; cout << 5 << "/" << 2; Isso está correto?
  25. Galera estou aprendente pelo o livro "treinamento em linguagem c++" e não estou entendendo esse código no livro estou copiando do mesmo jeito mas esta tando esses erros, alguém pode me explicar o que esta errado ? estou usando o Visual Studio. deste de já agradeço. class Data { public: int dia, mes, ano; private: int Bissexto( ) //fuçao inline { return (ano % 4 == 0 && ano % 100 || ano % 400 == 0); } void InitData(int d, int m, int a) //inicializaçao de dados { void PrintData(); void PrintSigno(); void PrintBissexto(); }; void Data::InitData(int d, int m, int a) //o nome qualificado não e permitido a em declaração de membro << esta tanto esse erro nesse lugares { int dmes[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 }; ano = a > 0 ? a : 1;//valida ano dmes[2] = dmes[2] + Bissexto(); mes = m >= 1 && m <= 12 ? m : 1; //valida mes dia = d >= 1 && d <= dmes[mes] ? d : 1;//valida dia } void Data::PrintData() //o nome qualificado não e permitido a em declaração de membro { char nome[13][10] = { "zero","janeiro","fevereiro","março","abriu", "maio","junho","julho","agosto","setembro", "outubro","novembro","dezembro" }; cout << dia << "de" << nome[mes] << "de" << ano << endl; } void Data::PrintSigno() //o nome qualificado não e permitido a em declaração de membro { char nsigno[14][12] = { "zero","capricornio","aquario","peixes","aries", "touro","gemeos","leao","virgem", "libra","escorpiao","sagitario","capricornio" }; int sig[] = { 0,20,19,20,20,20,20,21,22,22,22,21,21 }; if (dia < sig[mes]) cout << "Signo: " << nsigno[mes] << endl; else cout << "signo: " << nsigno[mes + 1] << endl; } Data::PrintBissexto() //o nome qualificado não e permitido a em declaração de membro { if (Bissexto()) cout << "ano e bissexto." << endl; else cout << "ano nao e bissexto." << endl; } }; int main() { Data x, y, z; x.InitData(14, 6, 1992); // aqui esta falando que a função esta inacessível << em todos elas y.InitData(12, 1, 1976); z.InitData(30, 7, 1978); x.PrintData(); x.PrintData(); x.PrintBissexto(); y.PrintData(); y.PrintData(); y.PrintBissexto(); z.PrintData(); z.PrintData(); z.PrintBissexto(); }

Sobre o Clube do Hardware

No ar desde 1996, o Clube do Hardware é uma das maiores, mais antigas e mais respeitadas publicações sobre tecnologia do Brasil. Leia mais

Direitos autorais

Não permitimos a cópia ou reprodução do conteúdo do nosso site, fórum, newsletters e redes sociais, mesmo citando-se a fonte. Leia mais

×