afq | Дата: Воскресенье, 11 Ноября 2018, 07:37 | Сообщение # 1 |
Разработчик
Сейчас нет на сайте
| видите вон там сверху часть гриба вверх ногами, вот об этом я и пишу. Как они появляются не пойму. Я приведу примеры кода, может кто глянет и что нибудь напишет. Кстати, спрайт, который загружается background вообще не отображается, почему незнаю.
Game.cpp
Код #include "Game.hpp" #include "Sprite.hpp"
Game::Game ( ) { SDL_Init ( SDL_INIT_TIMER | SDL_INIT_VIDEO | SDL_INIT_EVENTS ); SDL_DisplayMode mode; SDL_GetCurrentDisplayMode ( 0, &mode ); screen_width = mode.w; screen_height = mode.h;
window = SDL_CreateWindow ( "slots", 0, 0, screen_width, screen_height, SDL_WINDOW_OPENGL | SDL_WINDOW_FULLSCREEN );
SDL_GLContext glc;
SDL_GL_SetAttribute ( SDL_GL_CONTEXT_MAJOR_VERSION, 4 ); SDL_GL_SetAttribute ( SDL_GL_CONTEXT_MAJOR_VERSION, 0 ); glc = SDL_GL_CreateContext ( window ); SDL_GL_MakeCurrent ( window, glc );
glViewport ( 0, 0, screen_width, screen_height ); glMatrixMode ( GL_PROJECTION ); glLoadIdentity ( );
GLfloat aspect; aspect = (GLfloat) screen_width / (GLfloat) screen_height; glOrtho ( 0.0f, 1.0, 0.0 /*screen_height*/, 1.0 /*0.0f*/, 0.0f, 1.0f ); // glMatrixMode ( GL_TEXTURE ); glMatrixMode ( GL_MODELVIEW ); glLoadIdentity ( ); glEnable ( GL_BLEND ); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); } int thread ( void *data ) { SDL_Event event; while ( SDL_WaitEvent ( &event ) ) { } } #define MAX_SPRITES 24 #define BOTTOM 21 #define SPEED 0.09
char lines[3][5]; int offsets[] = { 0, 262156, 8818724, 9080880 }; void place ( Sprite *s, int end, float y, float x ) { for ( int i = 0; i < ( MAX_SPRITES - end ); i++, y += 0.2 ) { int ii = random () % 4; s[i].load ( offsets [ ii ] ); s[i].set_pos ( x, y ); s[i].number = ii; } } int st = 0; int new_game = 0;
void run ( Sprite *s ) { for ( int i = 0; i < MAX_SPRITES; i++ ) { s[i].set_pos ( s[i].get_x(), s[i].get_y() - SPEED ); if ( s[BOTTOM].get_y () <= 0.15 ) { s[i + 0].set_pos ( s[i + 0].get_x(), 0.15 ); s[i + 1].set_pos ( s[i + 1].get_x(), 0.15 + 0.2 ); s[i + 2].set_pos ( s[i + 2].get_x(), 0.15 + 0.2 + 0.2 ); st = 0; new_game = 1; break; } } } void copy ( Sprite *s ) { for ( int i = 0; i < 3; i++ ) { s[i].free_data ( ); } s[0].load ( s[MAX_SPRITES - 3].offset ); s[0].set_pos ( s[MAX_SPRITES - 3].get_x(), s[MAX_SPRITES - 3].get_y() ); s[1].load ( s[MAX_SPRITES - 2].offset ); s[1].set_pos ( s[MAX_SPRITES -2].get_x(), s[MAX_SPRITES - 2].get_y() ); s[2].load ( s[MAX_SPRITES - 1].offset ); s[2].set_pos ( s[MAX_SPRITES - 1].get_x(), s[MAX_SPRITES - 1].get_y() );
for ( int i = 3; i < MAX_SPRITES; i++ ) { s[i].free_data (); } }
void check_win ( Sprite *s1, Sprite *s2, Sprite *s3, Sprite *s4, Sprite *s5 ) { lines[0][0] = s1[0].number; lines[1][0] = s1[1].number; lines[2][0] = s1[2].number; lines[0][1] = s2[0].number; lines[1][1] = s2[1].number; lines[2][1] = s2[2].number; lines[0][2] = s3[0].number; lines[1][2] = s3[1].number; lines[2][2] = s3[2].number; lines[0][3] = s4[0].number; lines[1][3] = s4[1].number; lines[2][3] = s4[2].number; lines[0][4] = s5[0].number; lines[1][4] = s5[1].number; lines[2][4] = s5[2].number;
int ns1 = 0; int win = 0; for ( int i = 0; i < 5; i++ ) { ns1 = lines[0][i]; } }
void Game::loop ( ) { SDL_CreateThread ( thread, "thread", NULL );
Sprite *s1 = new Sprite [MAX_SPRITES]; #if 1 Sprite *s2 = new Sprite [MAX_SPRITES]; Sprite *s3 = new Sprite [MAX_SPRITES]; Sprite *s4 = new Sprite [MAX_SPRITES]; Sprite *s5 = new Sprite [MAX_SPRITES]; #endif #if 1 Sprite *background = new Sprite ( ); background->load ( 524312 ); background->set_pos ( 0.0, 0.0 ); background->set_size ( 1.0, 1.0 ); #endif
for ( int i = 0; i < MAX_SPRITES; i++ ) { s1[i].set_size ( 0.15, 0.2 ); s2[i].set_size ( 0.15, 0.2 ); s3[i].set_size ( 0.15, 0.2 ); s4[i].set_size ( 0.15, 0.2 ); s5[i].set_size ( 0.15, 0.2 ); } srandom ( time ( 0 ) );
place ( &s1[0], 0, 0.15, 0.15 ); place ( &s2[0], 0, 0.15, 0.15 + 0.15 ); place ( &s3[0], 0, 0.15, 0.15 + 0.15 + 0.15 ); place ( &s4[0], 0, 0.15, 0.15 + 0.15 + 0.15 + 0.15 ); place ( &s5[0], 0, 0.15, 0.15 + 0.15 + 0.15 + 0.15 + 0.15 );
while ( 1 ) { const Uint8 *state = SDL_GetKeyboardState ( NULL ); if ( state [ SDL_SCANCODE_Q ] ) { SDL_Quit ( ); exit ( EXIT_SUCCESS ); } if ( state [ SDL_SCANCODE_SPACE ] ) { if ( st == 1 ) { } else { st = 1; new_game = 0; } } if ( st >= 1 ) { run ( s1 ); #if 1 run ( s2 ); run ( s3 ); run ( s4 ); run ( s5 ); #endif } if ( new_game ) { copy ( s1 ); #if 1 copy ( s2 ); copy ( s3 ); copy ( s4 ); copy ( s5 ); #endif place ( &s1[3], 3, 0.15 + 0.2 + 0.2 + 0.2, 0.15 ); place ( &s2[3], 3, 0.15 + 0.2 + 0.2 + 0.2, 0.15 + 0.15 ); place ( &s3[3], 3, 0.15 + 0.2 + 0.2 + 0.2, 0.15 + 0.15 + 0.15 ); place ( &s4[3], 3, 0.15 + 0.2 + 0.2 + 0.2, 0.15 + 0.15 + 0.15 + 0.15 ); place ( &s5[3], 3, 0.15 + 0.2 + 0.2 + 0.2, 0.15 + 0.15 + 0.15 + 0.15 + 0.15 ); // check_win ( s1, s2, s3, s4, s5 ); new_game = 0; continue; } glClearColor ( 1, 1, 1, 1 ); glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); for ( int i = 0; i < MAX_SPRITES; i++ ) { s1[i].render ( ); s2[i].render ( ); s3[i].render ( ); s4[i].render ( ); s5[i].render ( ); } background->render(); SDL_GL_SwapWindow ( window ); SDL_Delay ( 0.01 ); } }
Sprite.cpp
Код #include "Sprite.hpp"
Sprite::Sprite ( long pos ) { //init_texture ( ); }
Sprite::Sprite ( ) { }
void Sprite::free_data ( ) { if ( texture ) delete ( texture ); if ( vertices ) delete ( vertices ); if ( pixels ) delete ( pixels ); } void Sprite::load ( long pos ) { offset = pos; std::FILE *fd = std::fopen ( "data", "r" ); if ( !fd ) { std::perror ( "sprite load" ); exit ( EXIT_FAILURE ); }
int ret; ret = std::fseek ( fd, pos, SEEK_SET ); if ( ret == -1 ) { perror ( "fseek\n" ); }
ret = std::fread ( &width, sizeof ( unsigned int ), 1, fd ); if ( ret == -1 ) { perror ( "width\n" ); } ret = std::fread ( &height, sizeof ( unsigned int ), 1, fd ); if ( ret == -1 ) { perror ( "heigth\n" ); } ret = std::fread ( &max_pixels, sizeof ( unsigned int ), 1, fd ); if ( ret == -1 ) { perror ( "max_pixels\n" ); }
pixels = new unsigned char [ max_pixels ]; if ( !pixels ) { printf (" !@!\n" ); }
ret = std::fread ( &pixels[0] , sizeof ( unsigned char ), max_pixels, fd ); if ( ret == -1 ) { perror ( "pixels\n" ); } std::fclose ( fd );
glGenTextures ( 1, &tex ); glBindTexture ( GL_TEXTURE_2D, tex ); #if 1 glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER ); glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER ); glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_BORDER ); #endif // gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels );
glTexImage2D ( GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels ); glEnable ( GL_TEXTURE_2D );
max_draw = height * width; vertices = new float [ 12 ]; vertices[0] = 0.0f; vertices[1] = 0.0f; vertices[2] = 0.0f; vertices[3] = fheight; vertices[4] = fwidth; vertices[5] = 0.0f;
vertices[6] = fwidth; vertices[7] = 0.0f; vertices[8] = fwidth; vertices[9] = fheight; vertices[10] = 0.0f; vertices[11] = fheight;
texture = new float [ 12 ]; texture[0] = 0.0f; texture[1] = 1.0f; texture[2] = 0.0f; texture[3] = 0.0f; texture[4] = 1.0f; texture[5] = 1.0f;
texture[6] = 1.0f; texture[7] = 1.0f; texture[8] = 1.0f; texture[9] = 0.0f; texture[10] = 0.0f; texture[11] = 0.0f; } void Sprite::render ( ) { #if 1 if ( y > 1.0 ) return; if ( y < -0.3 ) return; #endif
glBindTexture ( GL_TEXTURE_2D, tex ); glPushMatrix ( );
#if 0 glFrontFace ( GL_CCW ); glEnable ( GL_CULL_FACE ); glCullFace ( GL_BACK ); #endif
glTranslatef ( this->x, this->y, 0 );
glEnableClientState ( GL_VERTEX_ARRAY ); glEnableClientState ( GL_TEXTURE_COORD_ARRAY );
glVertexPointer ( 2, GL_FLOAT, 0, vertices ); glTexCoordPointer ( 2, GL_FLOAT, 0, texture ); glDrawArrays ( GL_TRIANGLES, 0, 12 );
glDisableClientState ( GL_VERTEX_ARRAY ); glDisableClientState ( GL_TEXTURE_COORD_ARRAY );
// glDisable ( GL_CULL_FACE ); // glTranslatef ( 0, 0, -10 );
glPopMatrix ( );
glFlush ( ); }
float Sprite::get_width ( ) { return width; }
float Sprite::get_height ( ) { return height; }
float Sprite::get_x ( ) { return x; }
float Sprite::get_y ( ) { return y; }
void Sprite::set_pos ( float x, float y ) { this->x = x; this->y = y; }
void Sprite::slice ( int width, int height ) { } void Sprite::set_size ( float width, float height ) { this->fwidth = width; this->fheight = height; }
int Sprite::get_max_frames ( ) { return max_frames; }
|
|
| |