Vaisseaux animator[dev c++]

Description

C'est juste un début d'application où vous devez tirer sur 2 carrés qui se déplacent tout seul.
Le seul vrai intérêt est le code source.
Dev C++

Source / Exemple :


// Includes

#include <windows.h>
#include <gl/gl.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
// Function Declarations

LRESULT CALLBACK 
WndProc( HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam );
VOID EnableOpenGL( HWND hWnd, HDC * hDC, HGLRC * hRC );
VOID DisableOpenGL( HWND hWnd, HDC hDC, HGLRC hRC );
float carre1X = 0.25f; float carre1Y = 0.25f;
float carre2X = -0.25f; float carre2Y = -0.25f;
float carre3X = 0.25f;  float carre3Y = -0.25f;
int nombre; int nombre2; int nombre3;
int choix, choix2, choix3;
int hasard = 60; int hasard2 = 60;
int n = 0;
int i = 0;
int a = 0;
float tir1X[100000]; float tir1Y[100000];
float tir2X[100000]; float tir2Y[100000];
float tir3X[100000]; float tir3Y[100000];
float couleur1 = 0.0f;
float couleur21 = 1.0f; float couleur22 = 1.0f; float couleur23 = 1.0f;
float couleur31 = 1.0f; float couleur32 = 1.0f; float couleur33 = 1.0f;
int indicateur = 0;
float score = 10.0f;

void shoot(int tir)
{
if (tir == 1)
{
glBegin(GL_POLYGON);
glColor3f(0.0f, 1.0f, 0.0f);   glVertex2f(tir1X[n], tir1Y[n]);
glColor3f(0.0f, 1.0f, 0.0f);   glVertex2f(tir1X[n] + 0.02f, tir1Y[n]);
glColor3f(0.0f, 1.0f, 0.0f);   glVertex2f(tir1X[n] + 0.02f, tir1Y[n] - 0.05f);
glColor3f(0.0f, 1.0f, 0.0f);   glVertex2f(tir1X[n], tir1Y[n] - 0.05f);

glEnd();
tir1Y[n] = tir1Y[n] - 0.01f;
if (tir1X[n] >= carre2X)
{
if (tir1X[n] <= carre2X + 0.15f)
{
if (tir1Y[n] - 0.05f >= carre2Y + 0.14f)
{
if (tir1Y[n] - 0.05f <= carre2Y + 0.15f)
{
couleur21 = couleur21 - 0.25f;
couleur22 = couleur21 - 0.25f;
couleur23 = couleur21 - 0.25f;
}
}
}
}

if (tir1X[n] >= carre3X)
{
if (tir1X[n] <= carre3X + 0.15f)
{
if (tir1Y[n] - 0.05f >= carre3Y + 0.14f)
{
if (tir1Y[n] - 0.05f <= carre3Y + 0.15f)
{
couleur31 = couleur31 - 0.25f;
couleur32 = couleur32 - 0.25f;
couleur33 = couleur33 - 0.25f;
}
}
}
}

}
if (tir == 2)
{
glBegin(GL_POLYGON);
glColor3f(0.0f, 1.0f, 1.0f);   glVertex2f(tir2X[i], tir2Y[i]);
glColor3f(0.0f, 1.0f, 1.0f);   glVertex2f(tir2X[i] + 0.02f, tir2Y[i]);
glColor3f(0.0f, 1.0f, 1.0f);   glVertex2f(tir2X[i] + 0.02f, tir2Y[i] + 0.05f);
glColor3f(0.0f, 1.0f, 1.0f);   glVertex2f(tir2X[i], tir2Y[i] + 0.05f);

glEnd();
tir2Y[i] = tir2Y[i] + 0.01f;
if (tir2X[i] >= carre1X)
{
if (tir2X[i] <= carre1X + 0.15f)
{
if (tir2Y[i] + 0.05f >= carre1Y)
{
if (tir2Y[i] + 0.05f <= carre1Y + 0.1f)
{
couleur1 = 1.0f;
if (indicateur <= 0)
{
score = score - 1.0f;
indicateur = 10;
}
}
}
}
}

}

if (tir == 3)
{
glBegin(GL_POLYGON);
glColor3f(0.0f, 0.5f, 1.0f);   glVertex2f(tir3X[a], tir3Y[a]);
glColor3f(0.0f, 0.5f, 1.0f);   glVertex2f(tir3X[a] + 0.02f, tir3Y[a]);
glColor3f(0.0f, 0.5f, 1.0f);   glVertex2f(tir3X[a] + 0.02f, tir3Y[a] + 0.05f);
glColor3f(0.0f, 0.5f, 1.0f);   glVertex2f(tir3X[a], tir3Y[a] + 0.05f);

glEnd();
tir3Y[a] = tir3Y[a] + 0.01f;
if (tir3X[a] >= carre1X)
{
if (tir3X[a] <= carre1X + 0.15f)
{
if (tir3Y[a] + 0.05f >= carre1Y)
{
if (tir3Y[a] + 0.05f <= carre1Y + 0.1f)
{
couleur1 = 1.0f;
if (indicateur <= 0)
{
score = score - 1.0f;
indicateur = 10;
}
}
}
}
}

}

}

// WinMain

int WINAPI 
WinMain( HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int iCmdShow )
{
srand( (unsigned)time( NULL ) ); 
WNDCLASS wc;
HWND hWnd;
HDC hDC;
HGLRC hRC;
MSG msg;
BOOL bQuit = FALSE;
float theta = 0.0f;
// register window class
wc.style = CS_OWNDC;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon( NULL, IDI_APPLICATION );
wc.hCursor = LoadCursor( NULL, IDC_ARROW );
wc.hbrBackground = (HBRUSH)GetStockObject( BLACK_BRUSH );
wc.lpszMenuName = NULL;
wc.lpszClassName = "GLSample";
RegisterClass( &wc );

// create main window
hWnd = CreateWindow( 
"GLSample", "OpenGL Sample", 
WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE,
0, 0, 700, 700,
NULL, NULL, hInstance, NULL );

// enable OpenGL for the window
EnableOpenGL( hWnd, &hDC, &hRC );

// program main loop
while ( !bQuit ) {

// check for messages
if ( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) ) {

// handle or dispatch messages
if ( msg.message == WM_QUIT ) {
bQuit = TRUE;
} else {
TranslateMessage( &msg );
DispatchMessage( &msg );
}

} else {

// OpenGL animation code goes here
glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
glClear( GL_COLOR_BUFFER_BIT );
glBegin(GL_POLYGON);
glColor3f(1.0f, couleur1, 0.0f);   glVertex2f(carre1X, carre1Y);
glColor3f(1.0f, couleur1, 0.0f);   glVertex2f(carre1X + 0.15f, carre1Y);
glColor3f(1.0f, couleur1, 0.0f);   glVertex2f(carre1X + 0.15f, carre1Y + 0.15f);
glColor3f(1.0f, couleur1, 0.0f);  glVertex2f(carre1X, carre1Y + 0.15f);
glEnd();

if (choix == 1)
{
shoot(1);
}

nombre2 = rand() % hasard;
if (hasard != 60)
{
hasard = hasard - 1;
}
if (nombre2 == 53)
{
hasard = hasard + 40;
choix2 = 1;
i = i + 1;
tir2X[i] = carre2X + 0.075f;
tir2Y[i] = carre2Y + 0.15f;
}

if (choix2 == 1)
{
shoot(2);
}

nombre3 = rand() % hasard2;
if (hasard2 != 60)
{
hasard2 = hasard2 - 1;
}
if (nombre3 == 53)
{
hasard2 = hasard2 + 40;
choix3 = 1;
a = a + 1;
tir3X[a] = carre3X + 0.075f;
tir2Y[a] = carre3Y + 0.15f;
}

if (choix3 == 1)
{
shoot(3);
}

glBegin(GL_POLYGON);
nombre = rand() % 14;
if (nombre == 0)
{
if (carre2Y <= 0.0f)
{
carre2Y = carre2Y + 0.05f;
}
}

if (nombre == 1)
{
if (carre2Y >= -0.9f)
{
carre2Y = carre2Y - 0.05f;
}
}

if (nombre == 2)
{
if (carre2X <= 0.9f)
{
carre2X = carre2X + 0.05f;
}
}

if (nombre == 3)
{
if (carre2X >= -0.9f)
{
carre2X = carre2X - 0.05f;
}
}

if (nombre == 4)
{
if (carre2X <= 0.9f)
{
carre2X = carre2X + 0.05f;
}
}

if (nombre == 5)
{
if (carre2X >= -0.9f)
{
carre2X = carre2X - 0.05f;
}
}

if (nombre == 6)
{
if (carre3X >= -0.9f)
{
carre3X = carre3X - 0.05f;
}
}

if (nombre == 7)
{
if (carre3X >= -0.9f)
{
carre3X = carre3X - 0.05f;
}
}

if (nombre == 8)
{
if (carre3X <= 0.9f)
{
carre3X = carre3X + 0.05f;
}
}

if (nombre == 9)
{
if (carre3X <= 0.9f)
{
carre3X = carre3X + 0.05f;
}
}

if (nombre == 10)
{
if (carre3Y >= -0.9f)
{
carre3Y = carre3Y - 0.05f;
}
}

if (nombre == 11)
{
if (carre3Y <= 0.0f)
{
carre3Y = carre3Y + 0.05f;
}
}

glColor3f(couleur21, couleur22, couleur23);   glVertex2f(carre2X, carre2Y);
glColor3f(couleur21, couleur22, couleur23);   glVertex2f(carre2X + 0.15f, carre2Y);
glColor3f(couleur21, couleur22, couleur23);   glVertex2f(carre2X + 0.15f, carre2Y + 0.15f);
glColor3f(couleur21, couleur22, couleur23);  glVertex2f(carre2X, carre2Y + 0.15f);
glEnd();

glBegin(GL_POLYGON);
glColor3f(couleur31, couleur32, couleur33);   glVertex2f(carre3X, carre3Y);
glColor3f(couleur31, couleur32, couleur33);   glVertex2f(carre3X + 0.15f, carre3Y);
glColor3f(couleur31, couleur32, couleur33);   glVertex2f(carre3X + 0.15f, carre3Y + 0.15f);
glColor3f(couleur31, couleur32, couleur33);  glVertex2f(carre3X, carre3Y + 0.15f);
glEnd();

glBegin(GL_POLYGON);
glColor3f(0.5f, 0.5f, 1.0f);   glVertex2f(0.50f, 0.80f);
glColor3f(1.0f, 0.5f, 0.5f);   glVertex2f(0.50f + (score / 100) * 5, 0.80f);
glColor3f(0.5f, 1.0f, 0.5f);   glVertex2f(0.50f + (score / 100) * 5, 0.90f);
glColor3f(1.0f, 0.1f, 0.1f);   glVertex2f(0.50f, 0.90f);
glEnd();

indicateur = indicateur - 1;
SwapBuffers( hDC );
}

}

// shutdown OpenGL
DisableOpenGL( hWnd, hDC, hRC );

// destroy the window explicitly
DestroyWindow( hWnd );

return msg.wParam;

}

// Window Procedure

LRESULT CALLBACK 
WndProc( HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam )
{

switch ( message ) {

case WM_CREATE:
return 0;

case WM_CLOSE:
PostQuitMessage( 0 );
return 0;

case WM_DESTROY:
return 0;

case WM_KEYDOWN:
switch ( wParam ) {

case VK_ESCAPE:
PostQuitMessage( 0 );
return 0;

case VK_UP:
if (carre1Y <= 0.9f)
{
carre1Y = carre1Y + 0.05f;
}
return DefWindowProc( hWnd, 
message, wParam, lParam );
break;

case VK_DOWN:
if (carre1Y >= 0.1f)
{
carre1Y = carre1Y - 0.05f;
}
return DefWindowProc( hWnd,
message, wParam, lParam );
break;

case VK_LEFT:
if (carre1X >= -1.0f)
{
carre1X = carre1X - 0.05f;
}
return DefWindowProc( hWnd, 
message, wParam, lParam );
break;

case VK_RIGHT:
if (carre1X <= 0.9f)
{
carre1X = carre1X + 0.05f;
}
return DefWindowProc( hWnd,
message, wParam, lParam );
break;

case VK_SPACE:
n = n + 1;
choix = 1;
tir1X[n] = carre1X + 0.075f;
tir1Y[n] = carre1Y;
return DefWindowProc( hWnd,
message, wParam, lParam );
break;
}
return 0;

default:
return DefWindowProc( hWnd, 
message, wParam, lParam );

}

}

// Enable OpenGL

VOID EnableOpenGL( HWND hWnd, HDC * hDC, HGLRC * hRC )
{
PIXELFORMATDESCRIPTOR pfd;
int iFormat;

// get the device context (DC)

  • hDC = GetDC( hWnd );
// set the pixel format for the DC ZeroMemory( &pfd, sizeof( pfd ) ); pfd.nSize = sizeof( pfd ); pfd.nVersion = 1; pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER; pfd.iPixelType = PFD_TYPE_RGBA; pfd.cColorBits = 24; pfd.cDepthBits = 16; pfd.iLayerType = PFD_MAIN_PLANE; iFormat = ChoosePixelFormat( *hDC, &pfd ); SetPixelFormat( *hDC, iFormat, &pfd ); // create and enable the render context (RC)
  • hRC = wglCreateContext( *hDC );
wglMakeCurrent( *hDC, *hRC ); } // Disable OpenGL VOID DisableOpenGL( HWND hWnd, HDC hDC, HGLRC hRC ) { wglMakeCurrent( NULL, NULL ); wglDeleteContext( hRC ); ReleaseDC( hWnd, hDC ); }

Conclusion :


sous la suggestion de Jo (commentaire plus bas), je précise le compilateur : Dev C++.
Pour compiler la source, il faut créer un project .dev et mettre la source dans un fichier .cpp

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.