init commit

This commit is contained in:
2024-11-12 17:41:10 +01:00
parent 1e4f1f955b
commit 20bc9108d3
146 changed files with 24465 additions and 0 deletions
Executable
BIN
View File
Binary file not shown.
+28
View File
@@ -0,0 +1,28 @@
#ifndef __SDLCOMMON_H
#define __SDLCOMMON_H
#include <SDL.h>
#include <SDL_image.h>
#ifndef PI
#define PI 3.141592
#endif
#ifndef NULLCOL
#define NULLCOL 0x00FF00FF
#define NULLCOLA 0xFF
#define NULLCOLR 0xFF
#define NULLCOLG 0x00
#define NULLCOLB 0xFF
#endif
void setpixel(SDL_Surface*, int, int, Uint8, Uint8, Uint8);
int getpixel(SDL_Surface*, int, int);
void DrawLine(SDL_Surface*, int , int , int , int , char , char, char);
void rotatesurface(SDL_Surface*, SDL_Surface*, int);
void interpole(SDL_Surface* , SDL_Surface* , Uint32 );
bool inCarArea(SDL_Surface* , int , int );
#endif
+11
View File
@@ -0,0 +1,11 @@
/bg1.bmp/1.1/Wed Apr 13 15:04:02 2005//
/bg1.dia/1.1/Wed Apr 13 15:04:03 2005//
/bg2.bmp/1.1/Wed Apr 13 15:04:03 2005//
/bg2.dia/1.1/Wed Apr 13 15:04:02 2005//
/bg3.bmp/1.1/Wed Apr 13 15:04:03 2005//
/bg3.dia/1.1/Wed Apr 13 15:04:03 2005//
/bg4.bmp/1.1/Wed Apr 13 15:04:03 2005//
/bg4.dia/1.1/Wed Apr 13 15:04:03 2005//
/bg5.bmp/1.2/Wed Apr 13 15:04:03 2005//
/bg5.dia/1.2/Wed Apr 13 15:04:03 2005//
D
+1
View File
@@ -0,0 +1 @@
ia2005/InterfaceCpp/Back
+1
View File
@@ -0,0 +1 @@
:pserver:feeling@jfroche.be:/opt/cvsroot
Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 MiB

Binary file not shown.
Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 MiB

Binary file not shown.
Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 MiB

Binary file not shown.
Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 MiB

Binary file not shown.
Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 MiB

Binary file not shown.
+1
View File
@@ -0,0 +1 @@
D
+1
View File
@@ -0,0 +1 @@
ia2005/InterfaceCpp/CVS
+1
View File
@@ -0,0 +1 @@
:pserver:feeling@jfroche.be:/opt/cvsroot
+15
View File
@@ -0,0 +1,15 @@
D/CVS////
D/Cars////
D/Back////
/Makefile/1.4/Wed Apr 13 15:04:03 2005//
/SFont.c/1.2/Wed Apr 13 15:04:03 2005//
/SFont.h/1.1/Wed Apr 13 15:04:03 2005//
/fenetre.h/1.4/Wed Apr 13 15:04:03 2005//
/font.png/1.1/Wed Apr 13 15:04:03 2005//
/main.cpp/1.3/Wed Apr 13 15:04:03 2005//
/sdlcommon.cpp/1.5/Thu Apr 14 17:40:11 2005//
/sdlcommon.h/1.4/Thu Apr 14 17:38:15 2005//
/car.cpp/1.20/Thu Apr 14 17:36:20 2005//
/car.h/1.11/Thu Apr 14 17:34:22 2005//
/shmdata.h/1.8/Thu Apr 14 16:59:58 2005//
/fenetre.cpp/1.22/Thu Apr 14 18:45:30 2005//
+1
View File
@@ -0,0 +1 @@
ia2005/InterfaceCpp
+1
View File
@@ -0,0 +1 @@
:pserver:feeling@jfroche.be:/opt/cvsroot
+5
View File
@@ -0,0 +1,5 @@
/voiture1.bmp/1.1/Wed Apr 13 15:04:03 2005//
/voiture2.bmp/1.1/Wed Apr 13 15:04:03 2005//
/voiture3.bmp/1.1/Wed Apr 13 15:04:03 2005//
/voiture4.bmp/1.1/Wed Apr 13 15:04:03 2005//
D
+1
View File
@@ -0,0 +1 @@
ia2005/InterfaceCpp/Cars
+1
View File
@@ -0,0 +1 @@
:pserver:feeling@jfroche.be:/opt/cvsroot
Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 188 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.5 KiB

+40
View File
@@ -0,0 +1,40 @@
#$Id: Makefile,v 1.4 2005/04/10 11:46:45 feeling Exp $'
CFLAGS= -Wno-deprecated -std=c++14
CPP= g++
GCC= gcc
INCLUDES= -L/usr/lib -I/usr/include/SDL
LIBS= -L/usr/lib -lSDL -lpthread -lSDL_image
all : interface
interface : fenetre.o main.o car.o sdlcommon.o SFont.o
$(CPP) fenetre.o main.o car.o sdlcommon.o SFont.o -o iface -L/usr/lib -lSDL -lpthread -lSDL_image -Wno-deprecated -std=c++14
fenetre.o : fenetre.cpp
$(CPP) -c fenetre.cpp -L/usr/lib -I/usr/include/SDL -Wno-deprecated -std=c++14
main.o : main.cpp
$(CPP) -c main.cpp -L/usr/lib -I/usr/include/SDL -Wno-deprecated -std=c++14
car.o : car.cpp
$(CPP) -c car.cpp -L/usr/lib -I/usr/include/SDL -Wno-deprecated -std=c++14
sdlcommon.o : sdlcommon.cpp
$(CPP) -c sdlcommon.cpp -L/usr/lib -I/usr/include/SDL -Wno-deprecated -std=c++14
SFont.o : SFont.c
$(GCC) -c SFont.c -L/usr/lib -I/usr/include/SDL
# g++ -c fenetre.cpp -L/usr/lib -I/usr/include/SDL -Wno-deprecated -std=c++14
# g++ -c main.cpp -L/usr/lib -I/usr/include/SDL -Wno-deprecated -std=c++14
# g++ -c car.cpp -L/usr/lib -I/usr/include/SDL -Wno-deprecated -std=c++14
# g++ -c sdlcommon.cpp -L/usr/lib -I/usr/include/SDL -Wno-deprecated -std=c++14
# gcc -c SFont.c -L/usr/lib -I/usr/include/SDL
# g++ fenetre.o main.o car.o sdlcommon.o SFont.o -o iface -L/usr/lib -lSDL -lpthread -lSDL_image -Wno-deprecated -std=c++14
# g++ -c fenetre.cpp -L/usr/lib -I/usr/include/SDL -Wno-deprecated
# g++ -c main.cpp -L/usr/lib -I/usr/include/SDL -Wno-deprecated
# g++ -c car.cpp -L/usr/lib -I/usr/include/SDL -Wno-deprecated
# g++ -c sdlcommon.cpp -L/usr/lib -I/usr/include/SDL -Wno-deprecated
# gcc -c SFont.c -L/usr/lib -I/usr/include/SDL
# g++ fenetre.o main.o car.o sdlcommon.o SFont.o -o iface -L/usr/lib -lSDL -lpthread -lSDL_image -Wno-deprecated
mrproper:
rm -rf *.o
rm -rf iface
clean:
rm -rf *.o
+32
View File
@@ -0,0 +1,32 @@
#$Id: Makefile,v 1.4 2005/04/10 11:46:45 feeling Exp $'
CFLAGS= -Wall -g -Wno-deprecated -std=c++14
CPP= g++
GCC= gcc
INCLUDES= -I/usr/include/SDL
LIBS= -L/usr/lib -lSDL -lpthread -lSDL_image
all : interface
interface : fenetre.o main.o car.o sdlcommon.o SFont.o
$(CPP) $(CFLAGS) $(LIBS) -o iface fenetre.o main.o car.o sdlcommon.o SFont.o
fenetre.o : fenetre.cpp
$(CPP) $(CFLAGS) $(INCLUDES) -c fenetre.cpp
main.o : main.cpp
$(CPP) $(CFLAGS) $(INCLUDES) -c main.cpp
car.o : car.cpp
$(CPP) $(CFLAGS) $(INCLUDES) -c car.cpp
sdlcommon.o : sdlcommon.cpp
$(CPP) $(CFLAGS) $(INCLUDES) -c sdlcommon.cpp
SFont.o : SFont.c
$(CPP) $(CFLAGS) $(INCLUDES) -c SFont.c
# g++ -c fenetre.cpp -L/usr/lib -I/usr/include/SDL -Wno-deprecated
# g++ -c main.cpp -L/usr/lib -I/usr/include/SDL -Wno-deprecated
# g++ -c car.cpp -L/usr/lib -I/usr/include/SDL -Wno-deprecated
# g++ -c sdlcommon.cpp -L/usr/lib -I/usr/include/SDL -Wno-deprecated
# gcc -c SFont.c -L/usr/lib -I/usr/include/SDL
# g++ fenetre.o main.o car.o sdlcommon.o SFont.o -o iface -L/usr/lib -lSDL -lpthread -lSDL_image -Wno-deprecated
mrproper:
rm -rf *.o
rm -rf iface
clean:
rm -rf *.o
+189
View File
@@ -0,0 +1,189 @@
/* SFont: a simple font-library that uses special .pngs as fonts
Copyright (C) 2003 Karl Bartel
License: GPL or LGPL (at your choice)
WWW: http://www.linux-games.com/sfont/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Karl Bartel
Cecilienstr. 14
12307 Berlin
GERMANY
karlb@gmx.net
*/
#include <SDL.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "SFont.h"
static Uint32 GetPixel(SDL_Surface *Surface, Sint32 X, Sint32 Y)
{
Uint8 *bits;
Uint32 Bpp;
assert(X>=0);
assert(X<Surface->w);
Bpp = Surface->format->BytesPerPixel;
bits = ((Uint8 *)Surface->pixels)+Y*Surface->pitch+X*Bpp;
switch(Bpp) {
case 1:
return *((Uint8 *)Surface->pixels + Y * Surface->pitch + X);
break;
case 2:
return *((Uint16 *)Surface->pixels + Y * Surface->pitch/2 + X);
break;
case 3: {
Uint8 r, g, b;
r = *((bits)+Surface->format->Rshift/8);
g = *((bits)+Surface->format->Gshift/8);
b = *((bits)+Surface->format->Bshift/8);
return SDL_MapRGB(Surface->format, r, g, b);
}
break;
case 4:
return *((Uint32 *)Surface->pixels + Y * Surface->pitch/4 + X);
break;
}
return 0;
}
SFont_Font* SFont_InitFont(SDL_Surface* Surface)
{
int x = 0, i = 0;
Uint32 pixel;
SFont_Font* Font;
Uint32 pink;
if (Surface == NULL)
return NULL;
Font = (SFont_Font *) malloc(sizeof(SFont_Font));
Font->Surface = Surface;
SDL_LockSurface(Surface);
pink = SDL_MapRGB(Surface->format, 255, 0, 255);
while (x < Surface->w) {
if (GetPixel(Surface, x, 0) == pink) {
Font->CharPos[i++]=x;
while((x < Surface->w) && (GetPixel(Surface, x, 0)== pink))
x++;
Font->CharPos[i++]=x;
}
x++;
}
Font->MaxPos = x-1;
pixel = GetPixel(Surface, 0, Surface->h-1);
SDL_UnlockSurface(Surface);
SDL_SetColorKey(Surface, SDL_SRCCOLORKEY, pixel);
return Font;
}
void SFont_FreeFont(SFont_Font* FontInfo)
{
SDL_FreeSurface(FontInfo->Surface);
free(FontInfo);
}
void SFont_Write(SDL_Surface *Surface, const SFont_Font *Font,
int x, int y, const char *text)
{
const char* c;
int charoffset;
SDL_Rect srcrect, dstrect;
if(text == NULL)
return;
/* these values won't change in the loop */
srcrect.y = 1;
dstrect.y = y;
srcrect.h = dstrect.h = Font->Surface->h - 1;
for(c = text; *c != '\0' && x <= Surface->w ; c++) {
charoffset = ((int) (*c - 33)) * 2 + 1;
/* skip spaces and nonprintable characters */
if (*c == ' ' || charoffset < 0 || charoffset > Font->MaxPos) {
x += Font->CharPos[2]-Font->CharPos[1];
continue;
}
srcrect.w = dstrect.w =
(Font->CharPos[charoffset+2] + Font->CharPos[charoffset+1])/2 -
(Font->CharPos[charoffset] + Font->CharPos[charoffset-1])/2;
srcrect.x = (Font->CharPos[charoffset]+Font->CharPos[charoffset-1])/2;
dstrect.x = (Sint16)(x - (float)(Font->CharPos[charoffset] - Font->CharPos[charoffset-1])/2);
SDL_BlitSurface(Font->Surface, &srcrect, Surface, &dstrect);
x += Font->CharPos[charoffset+1] - Font->CharPos[charoffset];
}
}
int SFont_TextWidth(const SFont_Font *Font, const char *text)
{
const char* c;
int charoffset=0;
int width = 0;
if(text == NULL)
return 0;
for(c = text; *c != '\0'; c++) {
charoffset = ((int) *c - 33) * 2 + 1;
/* skip spaces and nonprintable characters */
if (*c == ' ' || charoffset < 0 || charoffset > Font->MaxPos) {
width += Font->CharPos[2]-Font->CharPos[1];
continue;
}
width += Font->CharPos[charoffset+1] - Font->CharPos[charoffset];
}
return width;
}
int SFont_TextHeight(const SFont_Font* Font)
{
return Font->Surface->h - 1;
}
void SFont_WriteCenter(SDL_Surface *Surface, const SFont_Font *Font,
int y, const char *text)
{
SFont_Write(Surface, Font, Surface->w/2 - SFont_TextWidth(Font, text)/2,
y, text);
}
void SFont_Write_V(SDL_Surface *Surface, const SFont_Font *Font, int x, int y,
const char *text)
{
int i,ph;
char bf[4];
ph=SFont_TextHeight(Font);
for(i=0;i<((int)strlen(text));i++)
{
sprintf(bf,"%c",text[i]);
SFont_Write(Surface,Font, x-(SFont_TextWidth(Font,bf)/2) , y+(i*ph) , bf);
}
}
+85
View File
@@ -0,0 +1,85 @@
/* SFont: a simple font-library that uses special bitmaps as fonts
Copyright (C) 2003 Karl Bartel
License: GPL or LGPL (at your choice)
WWW: http://www.linux-games.com/sfont/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Karl Bartel
Cecilienstr. 14
12307 Berlin
GERMANY
karlb@gmx.net
*/
/************************************************************************
* SFONT - SDL Font Library by Karl Bartel <karlb@gmx.net> *
* *
* All functions are explained below. There are two versions of each *
* funtction. The first is the normal one, the function with the *
* 2 at the end can be used when you want to handle more than one font *
* in your program. *
* *
************************************************************************/
#ifndef SFONT_H
#define SFONT_H
#include <SDL.h>
#ifdef __cplusplus
extern "C" {
#endif
/*Delcare one variable of this type for each font you are using.
To load the fonts, load the font image into YourFont->Surface
and call InitFont( YourFont );
*/
typedef struct {
SDL_Surface *Surface;
int CharPos[512];
int MaxPos;
} SFont_Font;
/* Initializes the font
Font: this contains the suface with the font.
The font must be loaded before using this function.
*/
SFont_Font* SFont_InitFont (SDL_Surface *Font);
void SFont_FreeFont(SFont_Font* Font);
/* Blits a string to a surface
Destination: the suface you want to blit to
text: a string containing the text you want to blit.
*/
void SFont_Write(SDL_Surface *Surface, const SFont_Font *Font, int x, int y,
const char *text);
void SFont_Write_V(SDL_Surface *Surface, const SFont_Font *Font, int x, int y,
const char *text);
/* Returns the width of "text" in pixels */
int SFont_TextWidth(const SFont_Font* Font, const char *text);
/* Returns the height of "text" in pixels (which is always equal to Font->Surface->h) */
int SFont_TextHeight(const SFont_Font* Font);
/* Blits a string to Surface with centered x position */
void SFont_WriteCenter(SDL_Surface *Surface, const SFont_Font* Font, int y,
const char *text);
#ifdef __cplusplus
}
#endif
#endif /* SFONT_H */
Binary file not shown.
+313
View File
@@ -0,0 +1,313 @@
#include <iostream>
#include <unistd.h>
#include <SDL.h>
#include <math.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include "sdlcommon.h"
#include "car.h"
#include "shmdata.h"
using namespace std;
Car::Car()
{
initShm();
reset();
showbar=1;
angstep=5;
for(int i=0;i<CAMVECT;i++) camera[i]=0;
carimage=0;
for(int i=0;i<TRAINEESIZE;i++) { trainee[i][0]=0; trainee[i][1]=0; }
traineepos=0;
traineesize=1;
}
void Car::loadImage(char *imagefile)
{
SDL_Surface *voitsurf;
Uint32 cur=SDL_GetTicks();
voitsurf=SDL_LoadBMP(imagefile);
printf("Precaching Surfaces ..... \n");
for(int i=0;i<360;i++)
{
Surface_car[i]=SDL_CreateRGBSurface(SDL_HWSURFACE, voitsurf->w, voitsurf->h, 32, 0,0,0,0);
if(SDL_SetColorKey(Surface_car[i], SDL_SRCCOLORKEY | SDL_RLEACCEL, SDL_MapRGB(Surface_car[i]->format, 255,0,255)) == -1)
fprintf(stderr, "Warning: colorkey will not be used, reason: %s\n", SDL_GetError());
rotatesurface(voitsurf,Surface_car[i],i);
}
carimage=1;
printf("Done ... (%d ms)\n",SDL_GetTicks()-cur);
}
void Car::updateCar(SDL_Surface* screen)
{
int i;
for(i=0;(SData->read!=0) && i<200;i++) usleep(5); // on attends 1 seconde
if(i==200)
{
cout << "AutoPilot Timed Out ...";
exit(0);
}
if(SData->Res[0]) decAngle();
if(SData->Res[1]) incAngle();
SData->Res[0]=0;
SData->Res[1]=0;
SData->pos_x+=getSpeed()*(cos(SData->angle*PI/180));
SData->pos_y+=getSpeed()*(sin(SData->angle*PI/180));
showTrainee(screen);
showCamera(screen); /* obligé pour avoir les pixels :-) */
// CAMERASIZE // CAMERASTEP
if(carimage) /* on blit la voiture */
{
SDL_Rect r;
r.w=Surface_car[SData->angle]->w;
r.h=Surface_car[SData->angle]->h;
r.x=(int)SData->pos_x-(r.w/2);
r.y=(int)SData->pos_y-(r.h/2);
SDL_BlitSurface( Surface_car[SData->angle], NULL, screen, &r);
}
else for(int i=0;i<20;i++) /* affichage de la voiture simplifiée (ligne rouge) */
{
setpixel(screen,(int)(SData->pos_x-(cos(SData->angle*PI/180)*i)),(int)( SData->pos_y-(sin(SData->angle*PI/180)*i)),255,0,0);
}
for(int i=0;i<CAMVECT;i++)
{
// cout << camera[i];
SData->V[i]=camera[i];
}
SData->read=1; /* on a lu :D */
//cout << endl;
}
void Car::showTrainee(SDL_Surface* screen)
{
int k;
trainee[traineepos][0]=(int)SData->pos_x;
trainee[traineepos][1]=(int)SData->pos_y;
traineepos=(traineepos+1)%traineesize;
k=traineepos;
k=(k+1)%traineesize;
for(int i=0; /*i < traineesize &&*/ k!=traineepos;i++) /* affichage de la trainée */
{
//setpixel(screen,trainee[k][0],trainee[k][1],0,(int)(i*(256.0/traineesize))%256,255);
int x0,y0,x1,y1;
x0=trainee[(k+traineesize-1)%traineesize][0];
y0=trainee[(k+traineesize-1)%traineesize][1];
x1=trainee[k][0];
y1=trainee[k][1];
if(x0==0) x0=x1;
if(y0==0) y0=y1;
if(x1==0) x1=x0;
if(y1==0) y1=y0;
DrawLine(screen,x0,y0,x1,y1,0,0,255);
k=(k+1)%traineesize;
}
}
void Car::showCamera(SDL_Surface* screen)
{
double x,y;
int k,ang;
x=SData->pos_x + ( cos(SData->angle*PI/180) * CAMERADISTANCE );
y=SData->pos_y + ( sin(SData->angle*PI/180) * CAMERADISTANCE );
ang=(SData->angle+90)%360;
x-=CAMERASIZE/2*cos(ang*PI/180);
y-=CAMERASIZE/2*sin(ang*PI/180);
k=0;
for(int i=0;i<CAMERASIZE;i++)
{
x+=cos(ang*PI/180);
y+=sin(ang*PI/180);
if(!(i%CAMERASTEP))
{
camera[k]=1;
if(inCarArea(screen,(int)x,(int)y)) //if(x>0 && x<screen->w && y>0 && y<screen->h)
camera[k]=(((getpixel(screen,(int)x,(int)y))%2)+1)%2;
else camera[k]=0;
if(camera[k] && showbar)
{
setpixel(screen, (int)x, (int)y,255,0,0);
//setpixel(screen, (int)x, (int)y-1,255,0,0);
//setpixel(screen, (int)x-1, (int)y,255,0,0);
//setpixel(screen, (int)x-1, (int)y-1,255,0,0);
}
else if(showbar)
{
setpixel(screen, (int)x, (int)y,0,255,0);
//setpixel(screen, (int)x, (int)y-1,0,255,0);
//setpixel(screen, (int)x-1, (int)y,0,255,0);
//setpixel(screen, (int)x-1, (int)y-1,0,255,0);
}
k++;
}
else
{
if(showbar) setpixel(screen, (int)x, (int)y,0,0,255);
}
}
}
void Car::incAngle()
{
SData->angle=(SData->angle+angstep)%360;
}
void Car::decAngle()
{
SData->angle=(SData->angle+360-angstep)%360;
}
int Car::incAngleStep(int i)
{
angstep+=i;
return angstep;
}
void Car::incSpeed()
{
if(SData->Res[2]==0)
{
SData->Res[0]=0;
SData->Res[1]=0;
SData->Res[2]=1;
if(SData->speed==0) SData->speed=1;
}
SData->speed++;
}
void Car::decSpeed()
{
SData->speed--;
}
bool Car::APToggle()
{
SData->APon=(SData->APon+1)%2;
return SData->APon;
}
bool Car::APState() const
{
return SData->APon;
}
bool Car::neurToggle()
{
SData->Neuron=(SData->Neuron+1)%2;
return SData->Neuron;
}
bool Car::neurState() const
{
return SData->Neuron;
}
void Car::initShm()
{
if ((shmid = shmget(SHMKEY, sizeof(struct shmdata), 0666)) < 0)
{
perror("shmget");
exit(1);
}
if (( SData = (struct shmdata *)shmat(shmid, NULL, 0)) == (struct shmdata *) -1)
{
perror("shmat");
exit(1);
}
}
int Car::getSpeed() const
{
if(SData->Res[2])
return SData->speed;
return 0;
}
int Car::getAngle() const
{
return SData->angle;
}
bool Car::showBar(int i)
{
showbar=(showbar+i)%2;
return showbar;
}
bool* Car::getVect()
{
return SData->Res;
}
bool* Car::getView()
{
return SData->V;
}
void Car::reset()
{
SData->pos_x=200;
SData->pos_y=200;
SData->speed=1;
SData->angle=0;
SData->Res[2]=1;
}
void Car::turn180()
{
SData->angle=(SData->angle+180)%360;
}
int Car::incTraineeSize(int s)
{
int i,k;
if(s==0) return traineesize;
if(traineesize==1 && s<0) return traineesize;
if(traineesize+s > TRAINEESIZE) s=TRAINEESIZE-traineesize-1;
if(traineesize+s < 0) s=1-TRAINEESIZE;
int tmp[traineesize+s][2];
k=traineepos;
if(s<0) k=(traineesize+traineepos-s)%traineesize;
for(i=0;i<(traineesize+s);i++)
{
if(i>traineesize)
{
tmp[i][0]=0;
tmp[i][1]=0;
}
else
{
tmp[i][0]=trainee[k][0];
tmp[i][1]=trainee[k][1];
}
k=(k+1)%traineesize;
}
for(i=0;i<traineesize+s;i++)
{
trainee[i][0]=tmp[i][0];
trainee[i][1]=tmp[i][1];
}
traineepos=traineesize-1;
// if(traineesize<1) { traineesize=1; traineepos=0; }
if(s<0)traineepos=0;
traineesize+=s;
return traineesize;
}
void Car::setDelay(int del)
{
SData->delay=del;
}
void Car::harakiri()
{
cout << " EXIT INTERFACE ...\n";
SData->read=666;
usleep(100);
SData->read=666;
usleep(100);
SData->read=666;
}
void Car::saveState()
{
SData->action=SAVE_STATE;
}
void Car::loadState()
{
SData->action=RELOAD_STATE;
}
void Car::showNetwork(bool a)
{
SData->viewNetwork=a;
}
bool Car::showNetworkState()
{
return SData->viewNetwork;
}
+312
View File
@@ -0,0 +1,312 @@
#include <iostream>
#include <SDL.h>
#include <math.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include "sdlcommon.h"
#include "car.h"
#include "shmdata.h"
using namespace std;
Car::Car()
{
initShm();
reset();
showbar=1;
angstep=5;
for(int i=0;i<CAMVECT;i++) camera[i]=0;
carimage=0;
for(int i=0;i<TRAINEESIZE;i++) { trainee[i][0]=0; trainee[i][1]=0; }
traineepos=0;
traineesize=1;
}
void Car::loadImage(char *imagefile)
{
SDL_Surface *voitsurf;
Uint32 cur=SDL_GetTicks();
voitsurf=SDL_LoadBMP(imagefile);
printf("Precaching Surfaces ..... \n");
for(int i=0;i<360;i++)
{
Surface_car[i]=SDL_CreateRGBSurface(SDL_HWSURFACE, voitsurf->w, voitsurf->h, 32, 0,0,0,0);
if(SDL_SetColorKey(Surface_car[i], SDL_SRCCOLORKEY | SDL_RLEACCEL, SDL_MapRGB(Surface_car[i]->format, 255,0,255)) == -1)
fprintf(stderr, "Warning: colorkey will not be used, reason: %s\n", SDL_GetError());
rotatesurface(voitsurf,Surface_car[i],i);
}
carimage=1;
printf("Done ... (%d ms)\n",SDL_GetTicks()-cur);
}
void Car::updateCar(SDL_Surface* screen)
{
int i;
for(i=0;(SData->read!=0) && i<200;i++) usleep(5); // on attends 1 seconde
if(i==200)
{
cout << "AutoPilot Timed Out ...";
exit(0);
}
if(SData->Res[0]) decAngle();
if(SData->Res[1]) incAngle();
SData->Res[0]=0;
SData->Res[1]=0;
SData->pos_x+=getSpeed()*(cos(SData->angle*PI/180));
SData->pos_y+=getSpeed()*(sin(SData->angle*PI/180));
showTrainee(screen);
showCamera(screen); /* obligé pour avoir les pixels :-) */
// CAMERASIZE // CAMERASTEP
if(carimage) /* on blit la voiture */
{
SDL_Rect r;
r.w=Surface_car[SData->angle]->w;
r.h=Surface_car[SData->angle]->h;
r.x=(int)SData->pos_x-(r.w/2);
r.y=(int)SData->pos_y-(r.h/2);
SDL_BlitSurface( Surface_car[SData->angle], NULL, screen, &r);
}
else for(int i=0;i<20;i++) /* affichage de la voiture simplifiée (ligne rouge) */
{
setpixel(screen,(int)(SData->pos_x-(cos(SData->angle*PI/180)*i)),(int)( SData->pos_y-(sin(SData->angle*PI/180)*i)),255,0,0);
}
for(int i=0;i<CAMVECT;i++)
{
// cout << camera[i];
SData->V[i]=camera[i];
}
SData->read=1; /* on a lu :D */
//cout << endl;
}
void Car::showTrainee(SDL_Surface* screen)
{
int k;
trainee[traineepos][0]=(int)SData->pos_x;
trainee[traineepos][1]=(int)SData->pos_y;
traineepos=(traineepos+1)%traineesize;
k=traineepos;
k=(k+1)%traineesize;
for(int i=0; /*i < traineesize &&*/ k!=traineepos;i++) /* affichage de la trainée */
{
//setpixel(screen,trainee[k][0],trainee[k][1],0,(int)(i*(256.0/traineesize))%256,255);
int x0,y0,x1,y1;
x0=trainee[(k+traineesize-1)%traineesize][0];
y0=trainee[(k+traineesize-1)%traineesize][1];
x1=trainee[k][0];
y1=trainee[k][1];
if(x0==0) x0=x1;
if(y0==0) y0=y1;
if(x1==0) x1=x0;
if(y1==0) y1=y0;
DrawLine(screen,x0,y0,x1,y1,0,0,255);
k=(k+1)%traineesize;
}
}
void Car::showCamera(SDL_Surface* screen)
{
double x,y;
int k,ang;
x=SData->pos_x + ( cos(SData->angle*PI/180) * CAMERADISTANCE );
y=SData->pos_y + ( sin(SData->angle*PI/180) * CAMERADISTANCE );
ang=(SData->angle+90)%360;
x-=CAMERASIZE/2*cos(ang*PI/180);
y-=CAMERASIZE/2*sin(ang*PI/180);
k=0;
for(int i=0;i<CAMERASIZE;i++)
{
x+=cos(ang*PI/180);
y+=sin(ang*PI/180);
if(!(i%CAMERASTEP))
{
camera[k]=1;
if(inCarArea(screen,(int)x,(int)y)) //if(x>0 && x<screen->w && y>0 && y<screen->h)
camera[k]=(((getpixel(screen,(int)x,(int)y))%2)+1)%2;
else camera[k]=0;
if(camera[k] && showbar)
{
setpixel(screen, (int)x, (int)y,255,0,0);
//setpixel(screen, (int)x, (int)y-1,255,0,0);
//setpixel(screen, (int)x-1, (int)y,255,0,0);
//setpixel(screen, (int)x-1, (int)y-1,255,0,0);
}
else if(showbar)
{
setpixel(screen, (int)x, (int)y,0,255,0);
//setpixel(screen, (int)x, (int)y-1,0,255,0);
//setpixel(screen, (int)x-1, (int)y,0,255,0);
//setpixel(screen, (int)x-1, (int)y-1,0,255,0);
}
k++;
}
else
{
if(showbar) setpixel(screen, (int)x, (int)y,0,0,255);
}
}
}
void Car::incAngle()
{
SData->angle=(SData->angle+angstep)%360;
}
void Car::decAngle()
{
SData->angle=(SData->angle+360-angstep)%360;
}
int Car::incAngleStep(int i)
{
angstep+=i;
return angstep;
}
void Car::incSpeed()
{
if(SData->Res[2]==0)
{
SData->Res[0]=0;
SData->Res[1]=0;
SData->Res[2]=1;
if(SData->speed==0) SData->speed=1;
}
SData->speed++;
}
void Car::decSpeed()
{
SData->speed--;
}
bool Car::APToggle()
{
SData->APon=(SData->APon+1)%2;
return SData->APon;
}
bool Car::APState() const
{
return SData->APon;
}
bool Car::neurToggle()
{
SData->Neuron=(SData->Neuron+1)%2;
return SData->Neuron;
}
bool Car::neurState() const
{
return SData->Neuron;
}
void Car::initShm()
{
if ((shmid = shmget(SHMKEY, sizeof(struct shmdata), 0666)) < 0)
{
perror("shmget");
exit(1);
}
if (( SData = (struct shmdata *)shmat(shmid, NULL, 0)) == (struct shmdata *) -1)
{
perror("shmat");
exit(1);
}
}
int Car::getSpeed() const
{
if(SData->Res[2])
return SData->speed;
return 0;
}
int Car::getAngle() const
{
return SData->angle;
}
bool Car::showBar(int i)
{
showbar=(showbar+i)%2;
return showbar;
}
bool* Car::getVect()
{
return SData->Res;
}
bool* Car::getView()
{
return SData->V;
}
void Car::reset()
{
SData->pos_x=200;
SData->pos_y=200;
SData->speed=1;
SData->angle=0;
SData->Res[2]=1;
}
void Car::turn180()
{
SData->angle=(SData->angle+180)%360;
}
int Car::incTraineeSize(int s)
{
int i,k;
if(s==0) return traineesize;
if(traineesize==1 && s<0) return traineesize;
if(traineesize+s > TRAINEESIZE) s=TRAINEESIZE-traineesize-1;
if(traineesize+s < 0) s=1-TRAINEESIZE;
int tmp[traineesize+s][2];
k=traineepos;
if(s<0) k=(traineesize+traineepos-s)%traineesize;
for(i=0;i<(traineesize+s);i++)
{
if(i>traineesize)
{
tmp[i][0]=0;
tmp[i][1]=0;
}
else
{
tmp[i][0]=trainee[k][0];
tmp[i][1]=trainee[k][1];
}
k=(k+1)%traineesize;
}
for(i=0;i<traineesize+s;i++)
{
trainee[i][0]=tmp[i][0];
trainee[i][1]=tmp[i][1];
}
traineepos=traineesize-1;
// if(traineesize<1) { traineesize=1; traineepos=0; }
if(s<0)traineepos=0;
traineesize+=s;
return traineesize;
}
void Car::setDelay(int del)
{
SData->delay=del;
}
void Car::harakiri()
{
cout << " EXIT INTERFACE ...\n";
SData->read=666;
usleep(100);
SData->read=666;
usleep(100);
SData->read=666;
}
void Car::saveState()
{
SData->action=SAVE_STATE;
}
void Car::loadState()
{
SData->action=RELOAD_STATE;
}
void Car::showNetwork(bool a)
{
SData->viewNetwork=a;
}
bool Car::showNetworkState()
{
return SData->viewNetwork;
}
+57
View File
@@ -0,0 +1,57 @@
#ifndef __CAR_H
#define __CAR_H
#define PI 3.141592
#define ANGLESTEP 5
#define TRAINEESIZE 8192
#include "shmdata.h"
class Car
{
private :
SDL_Surface *Surface_car[360];
SDL_Rect rect;
bool carimage;
bool showbar;
bool camera[CAMVECT];
struct shmdata *SData;
int shmid;
int angstep;
int trainee[TRAINEESIZE][2];
int traineepos;
int traineesize;
public :
Car();
void updateCar(SDL_Surface* screen);
void loadImage(char *imagefile);
void initShm();
int incTraineeSize(int);
void incAngle();
void decAngle();
void incSpeed();
void decSpeed();
int incAngleStep(int);
bool APToggle();
bool APState() const;
bool neurToggle();
bool neurState() const;
int getSpeed() const;
int getAngle() const;
bool showBar(int i);
bool* getVect();
bool* getView();
void reset();
void turn180();
void showCamera(SDL_Surface* screen);
void showTrainee(SDL_Surface* screen);
void loadState();
void saveState();
void harakiri();
void setDelay(int del);
void showNetwork(bool a);
bool showNetworkState();
};
#endif
+57
View File
@@ -0,0 +1,57 @@
#ifndef __CAR_H
#define __CAR_H
#define PI 3.141592
#define ANGLESTEP 5
#define TRAINEESIZE 8192
#include "shmdata.h"
class Car
{
private :
SDL_Surface *Surface_car[360];
SDL_Rect rect;
bool carimage;
bool showbar;
bool camera[CAMVECT];
struct shmdata *SData;
int shmid;
int angstep;
int trainee[TRAINEESIZE][2];
int traineepos;
int traineesize;
public :
Car();
void updateCar(SDL_Surface* screen);
void loadImage(char *imagefile);
void initShm();
int incTraineeSize(int);
void incAngle();
void decAngle();
void incSpeed();
void decSpeed();
int incAngleStep(int);
bool APToggle();
bool APState() const;
bool neurToggle();
bool neurState() const;
int getSpeed() const;
int getAngle() const;
bool showBar(int i);
bool* getVect();
bool* getView();
void reset();
void turn180();
void showCamera(SDL_Surface* screen);
void showTrainee(SDL_Surface* screen);
void loadState();
void saveState();
void harakiri();
void setDelay(int del);
bool showNetwork(bool a);
bool showNetworkState();
};
#endif
Binary file not shown.
+376
View File
@@ -0,0 +1,376 @@
#include <SDL.h>
#include <SDL_image.h>
#include <time.h>
#include <iostream>
#include "sdlcommon.h"
#include "fenetre.h"
#include "shmdata.h"
using namespace std;
Fenetre::Fenetre ()
{
obstacle=0;
SHOWcamera=1;
SHOWpilot=1;
fps=0;
font=SFont_InitFont(IMG_Load(FONT_PATH));
if( SDL_Init(SDL_INIT_VIDEO) < 0 )
{
fprintf(stderr, "Could not initialize SDL: %s\n", SDL_GetError());
return ;
}
atexit(SDL_Quit);
screen = SDL_SetVideoMode(800,600, 32, SDL_HWSURFACE);
/* SDL_HWSURFACE / SDL_SWSURFACE */
/*|SDL_ANYFORMAT*/
if( screen==NULL )
{
fprintf(stderr, "Could not create surface: %s\n", SDL_GetError());
return ;
}
return ;
Voiture.loadImage("Cars/voiture4.bmp");
circuit=1;
}
Fenetre::Fenetre (int largeur = 800, int hauteur = 600, int nb_couleurs = 16, char *titre = "Fenetre SDL", char *background=NULL)
{
obstacle=0;
SHOWcamera=1;
SHOWpilot=1;
fps=0;
font=SFont_InitFont(IMG_Load(FONT_PATH));
if (SDL_Init (SDL_INIT_VIDEO) < 0)
{
cerr << "Erreur dans l'initialisation de la librairie" << endl;
}
screen = SDL_SetVideoMode (largeur, hauteur, nb_couleurs, SDL_HWSURFACE | SDL_DOUBLEBUF);
//SDL_CreateRGBSurface(SDL_HWSURFACE, largeur,hauteur,32,0,0,0,0);
if (screen == NULL)
{
cerr << "Erreur dans l'initialisation des modes vidéos:" <<
SDL_GetError ();
SDL_Quit ();
}
SDL_WM_SetCaption (titre, NULL);
fond = SDL_LoadBMP (background);
SDL_BlitSurface (fond, NULL, screen, NULL);
SDL_Flip (screen);
HardBG=SDL_CreateRGBSurface(SDL_HWSURFACE, largeur, hauteur, 32, 0,0,0,0);
SDL_BlitSurface( fond, NULL, HardBG, NULL);
Voiture.loadImage("Cars/voiture4.bmp");
circuit=1;
}
void Fenetre::loadBG(char *background)
{
fond = SDL_LoadBMP (background);
SDL_BlitSurface (fond, NULL, /*screen*/HardBG, NULL);
SDL_BlitSurface( fond, NULL, HardBG, NULL);
SDL_Flip (screen);
}
Fenetre::~Fenetre ()
{
/*
if(fond) SDL_FreeSurface (fond);
if(screen) SDL_FreeSurface (screen);
*/
SDL_Quit ();
}
void Fenetre::showView(int x,int y)
{
bool* v=Voiture.getView();
SDL_Rect r;
r.x=x; r.y=y-1; r.h=1; r.w=4*CAMVECT;
SDL_FillRect(screen,&r, SDL_MapRGB(screen->format, 255,0,0));
r.x=x; r.y=y+4; r.h=1; r.w=4*CAMVECT;
SDL_FillRect(screen,&r, SDL_MapRGB(screen->format, 255,0,0));
for(int i=0;i<CAMVECT;i++)
{
if(v[i])
{
r.x=x+(4*i);
r.y=y;
r.w=4;
r.h=4;
SDL_FillRect(screen,&r, SDL_MapRGB(screen->format, 0,0,0));
}
}
}
void Fenetre::showObstacle()
{
SDL_Rect r;
r.x=400;
r.y=20;
r.w=40;
r.h=screen->h-40;
SDL_FillRect(screen,&r, SDL_MapRGB(screen->format, 255,0,0));
}
void Fenetre::showPilot(int x,int y)
{
if(Voiture.getVect()[0])
for(int i=0;i<16;i++)
{
for(int j=y-i;j<y+i;j++)
putPixel(x+i,j,0,0,0);
}
if(Voiture.getVect()[1])
for(int i=0;i<16;i++)
{
for(int j=y-i;j<y+i;j++)
putPixel(x+64-i,j,0,0,0);
}
if(Voiture.getVect()[2]==1)
for(int i=0;i<16;i++)
{
for(int j=x-i+32;j<x+i+32;j++)
putPixel(j,y+i-32,0,0,0);
}
if(Voiture.getVect()[2]==0)
for(int i=0;i<16;i++)
{
for(int j=x-i+32;j<x+i+32;j++)
putPixel(j,y-i+32,0,0,0);
}
}
void Fenetre::drawText()
{
char *bf=(char*)malloc(256);
sprintf(bf,": %d",Voiture.APState());
SFont_Write(screen,font,10,10,"AutoPilot (a)");
SFont_Write(screen,font,120,10,bf);
sprintf(bf,": %d",Voiture.neurState());
SFont_Write(screen,font,10,25,"NeuronPilot (n)");
SFont_Write(screen,font,120,25,bf);
sprintf(bf,": %d",Voiture.getSpeed());
SFont_Write(screen,font,10,40,"Speed");
SFont_Write(screen,font,120,40,bf);
sprintf(bf,": %d",Voiture.getAngle());
SFont_Write(screen,font,10,55,"Angle");
SFont_Write(screen,font,120,55,bf);
sprintf(bf,": %d",fps);
SFont_Write(screen,font,10,70,"Framerate");
SFont_Write(screen,font,120,70,bf);
sprintf(bf,": %d",Voiture.incTraineeSize(0));
SFont_Write(screen,font,10,85,"trainee(F8,F9)");
SFont_Write(screen,font,120,85,bf);
sprintf(bf,": %d",Voiture.showBar(0));
SFont_Write(screen,font,200,10,"Showbar (b)");
SFont_Write(screen,font,300,10,bf);
sprintf(bf,": %d",SHOWcamera);
SFont_Write(screen,font,200,25,"Showcam (c)");
SFont_Write(screen,font,300,25,bf);
sprintf(bf,": %d",SHOWpilot);
SFont_Write(screen,font,200,40,"Showpilot (p)");
SFont_Write(screen,font,300,40,bf);
sprintf(bf,": %d",obstacle);
SFont_Write(screen,font,200,55,"Obstacle (o)");
SFont_Write(screen,font,300,55,bf);
sprintf(bf,": %d",Voiture.incAngleStep(0));
SFont_Write(screen,font,200,70,"AStep (pgU-D)");
SFont_Write(screen,font,300,70,bf);
sprintf(bf,": %d",Voiture.showNetworkState());
SFont_Write(screen,font,200,85,"NeuronIface(i)");
SFont_Write(screen,font,300,85,bf);
DrawLine(screen,0,99,319,99,0,0,128);
}
void Fenetre::redraw()
{
#ifdef __DEBUG_TIME
Uint32 deltatime;
#endif
if(sec!=time(NULL)) { sec=time(NULL); fps=frame; frame=0; }
frame++;
#ifdef __DEBUG_TIME
deltatime=SDL_GetTicks();
#endif
/* SDL_BlitSurface( fond, NULL, screen, NULL); Software Mode */
SDL_BlitSurface( HardBG, NULL, screen , NULL); /* Hardware Mode */
#ifdef __DEBUG_TIME
printf("Background Blit : %d ms, ",SDL_GetTicks()-deltatime);
deltatime=SDL_GetTicks();
#endif
drawText();
if(SHOWpilot) showPilot(440,50);
if(SHOWcamera) showView(350,10);
if(obstacle) showObstacle();
Voiture.updateCar(screen);
#ifdef __DEBUG_TIME
printf("Car Update : %d ms, ",SDL_GetTicks()-deltatime);
deltatime=SDL_GetTicks();
#endif
SDL_Flip (screen);
#ifdef __DEBUG_TIME
printf("Screen Refresh : %d ms\n",SDL_GetTicks()-deltatime);
#endif
}
void Fenetre::putPixel(int x, int y, char r, char g, char b)
{
setpixel(screen,x,y,r,g,b);
}
void Fenetre::handle()
{
int exitkey = 0;
bool shownet= 0;
int waitdel = 20;
SDL_Event event;
while (!exitkey)
{
SDL_Delay (waitdel);
redraw();
while (SDL_PollEvent (&event))
{
switch (event.type)
{
case SDL_QUIT:
Voiture.harakiri();
exitkey = 1;
break;
case SDL_KEYDOWN:
switch (event.key.keysym.sym)
{
case SDLK_ESCAPE:
Voiture.harakiri();
exitkey = 1;
break;
case SDLK_LEFT:
Voiture.decAngle();
break;
case SDLK_RIGHT:
Voiture.incAngle();
break;
case SDLK_DOWN:
Voiture.decSpeed();
break;
case SDLK_UP:
Voiture.incSpeed();
break;
case SDLK_a:
Voiture.APToggle();
break;
case SDLK_b:
Voiture.showBar(1);
break;
case SDLK_c:
SHOWcamera=(SHOWcamera+1)%2;
break;
case SDLK_i:
shownet=(shownet+1)%2;
Voiture.showNetwork(shownet);
break;
case SDLK_n:
Voiture.neurToggle();
break;
case SDLK_o:
obstacle=(obstacle+1)%2;
break;
case SDLK_p:
SHOWpilot=(SHOWpilot+1)%2;
break;
case SDLK_r:
Voiture.reset();
break;
case SDLK_t:
Voiture.turn180();
break;
case SDLK_w:
Voiture.saveState();
//SData->action=SAVE_STATE;
break;
case SDLK_x:
Voiture.loadState();
//SData->action=RELOAD_STATE;
break;
case SDLK_F1:
printf("Chargement Circuit 1\n");
circuit=1;
loadBG("Back/bg1.bmp");
break;
case SDLK_F2:
printf("Chargement Circuit 2\n");
circuit=2;
loadBG("Back/bg2.bmp");
break;
case SDLK_F3:
printf("Chargement Circuit 3\n");
circuit=3;
loadBG("Back/bg3.bmp");
break;
case SDLK_F4:
printf("Chargement Circuit 4\n");
circuit=4;
loadBG("Back/bg4.bmp");
break;
case SDLK_F5:
printf("Chargement Circuit 5\n");
circuit=5;
loadBG("Back/bg5.bmp");
break;
case SDLK_F6:
printf("Chargement Circuit 6\n");
circuit=6;
loadBG("Back/bg6.bmp");
break;
case SDLK_F7:
Voiture.incTraineeSize(-256);
break;
case SDLK_F8:
Voiture.incTraineeSize(256);
break;
case SDLK_F9:
printf("Chargement Voiture 1\n");
Voiture.loadImage("Cars/voiture1.bmp");
break;
case SDLK_F10:
printf("Chargement Voiture 2\n");
Voiture.loadImage("Cars/voiture2.bmp");
break;
case SDLK_F11:
printf("Chargement Voiture 3\n");
Voiture.loadImage("Cars/voiture3.bmp");
break;
case SDLK_F12:
printf("Chargement Voiture 4\n");
Voiture.loadImage("Cars/voiture4.bmp");
break;
case SDLK_PAGEUP:
Voiture.incAngleStep(1);
break;
case SDLK_PAGEDOWN:
Voiture.incAngleStep(-1);
break;
case SDLK_HOME:
waitdel+=5;
break;
case SDLK_END:
waitdel-=5;
if(waitdel<0) waitdel=0;
break;
default :
break;
}
break;
}
break;
}
}
}
+376
View File
@@ -0,0 +1,376 @@
#include <SDL.h>
#include <SDL_image.h>
#include <time.h>
#include <iostream>
#include "sdlcommon.h"
#include "fenetre.h"
#include "shmdata.h"
using namespace std;
Fenetre::Fenetre ()
{
obstacle=0;
SHOWcamera=1;
SHOWpilot=1;
fps=0;
font=SFont_InitFont(IMG_Load(FONT_PATH));
if( SDL_Init(SDL_INIT_VIDEO) < 0 )
{
fprintf(stderr, "Could not initialize SDL: %s\n", SDL_GetError());
return ;
}
atexit(SDL_Quit);
screen = SDL_SetVideoMode(800,600, 32, SDL_HWSURFACE);
/* SDL_HWSURFACE / SDL_SWSURFACE */
/*|SDL_ANYFORMAT*/
if( screen==NULL )
{
fprintf(stderr, "Could not create surface: %s\n", SDL_GetError());
return ;
}
return ;
Voiture.loadImage("Cars/voiture4.bmp");
circuit=1;
}
Fenetre::Fenetre (int largeur = 800, int hauteur = 600, int nb_couleurs = 16, char *titre = "Fenetre SDL", char *background=NULL)
{
obstacle=0;
SHOWcamera=1;
SHOWpilot=1;
fps=0;
font=SFont_InitFont(IMG_Load(FONT_PATH));
if (SDL_Init (SDL_INIT_VIDEO) < 0)
{
cerr << "Erreur dans l'initialisation de la librairie" << endl;
}
screen = SDL_SetVideoMode (largeur, hauteur, nb_couleurs, SDL_HWSURFACE | SDL_DOUBLEBUF);
//SDL_CreateRGBSurface(SDL_HWSURFACE, largeur,hauteur,32,0,0,0,0);
if (screen == NULL)
{
cerr << "Erreur dans l'initialisation des modes vidéos:" <<
SDL_GetError ();
SDL_Quit ();
}
SDL_WM_SetCaption (titre, NULL);
fond = SDL_LoadBMP (background);
SDL_BlitSurface (fond, NULL, screen, NULL);
SDL_Flip (screen);
HardBG=SDL_CreateRGBSurface(SDL_HWSURFACE, largeur, hauteur, 32, 0,0,0,0);
SDL_BlitSurface( fond, NULL, HardBG, NULL);
Voiture.loadImage("Cars/voiture4.bmp");
circuit=1;
}
void Fenetre::loadBG(char *background)
{
fond = SDL_LoadBMP (background);
SDL_BlitSurface (fond, NULL, /*screen*/HardBG, NULL);
SDL_BlitSurface( fond, NULL, HardBG, NULL);
SDL_Flip (screen);
}
Fenetre::~Fenetre ()
{
/*
if(fond) SDL_FreeSurface (fond);
if(screen) SDL_FreeSurface (screen);
*/
SDL_Quit ();
}
void Fenetre::showView(int x,int y)
{
bool* v=Voiture.getView();
SDL_Rect r;
r.x=x; r.y=y-1; r.h=1; r.w=4*CAMVECT;
SDL_FillRect(screen,&r, SDL_MapRGB(screen->format, 255,0,0));
r.x=x; r.y=y+4; r.h=1; r.w=4*CAMVECT;
SDL_FillRect(screen,&r, SDL_MapRGB(screen->format, 255,0,0));
for(int i=0;i<CAMVECT;i++)
{
if(v[i])
{
r.x=x+(4*i);
r.y=y;
r.w=4;
r.h=4;
SDL_FillRect(screen,&r, SDL_MapRGB(screen->format, 0,0,0));
}
}
}
void Fenetre::showObstacle()
{
SDL_Rect r;
r.x=400;
r.y=20;
r.w=40;
r.h=screen->h-40;
SDL_FillRect(screen,&r, SDL_MapRGB(screen->format, 255,0,0));
}
void Fenetre::showPilot(int x,int y)
{
if(Voiture.getVect()[0])
for(int i=0;i<16;i++)
{
for(int j=y-i;j<y+i;j++)
putPixel(x+i,j,0,0,0);
}
if(Voiture.getVect()[1])
for(int i=0;i<16;i++)
{
for(int j=y-i;j<y+i;j++)
putPixel(x+64-i,j,0,0,0);
}
if(Voiture.getVect()[2]==1)
for(int i=0;i<16;i++)
{
for(int j=x-i+32;j<x+i+32;j++)
putPixel(j,y+i-32,0,0,0);
}
if(Voiture.getVect()[2]==0)
for(int i=0;i<16;i++)
{
for(int j=x-i+32;j<x+i+32;j++)
putPixel(j,y-i+32,0,0,0);
}
}
void Fenetre::drawText()
{
char *bf=(char*)malloc(256);
sprintf(bf,": %d",Voiture.APState());
SFont_Write(screen,font,10,10,"AutoPilot (a)");
SFont_Write(screen,font,120,10,bf);
sprintf(bf,": %d",Voiture.neurState());
SFont_Write(screen,font,10,25,"NeuronPilot (n)");
SFont_Write(screen,font,120,25,bf);
sprintf(bf,": %d",Voiture.getSpeed());
SFont_Write(screen,font,10,40,"Speed");
SFont_Write(screen,font,120,40,bf);
sprintf(bf,": %d",Voiture.getAngle());
SFont_Write(screen,font,10,55,"Angle");
SFont_Write(screen,font,120,55,bf);
sprintf(bf,": %d",fps);
SFont_Write(screen,font,10,70,"Framerate");
SFont_Write(screen,font,120,70,bf);
sprintf(bf,": %d",Voiture.incTraineeSize(0));
SFont_Write(screen,font,10,85,"trainee(F8,F9)");
SFont_Write(screen,font,120,85,bf);
sprintf(bf,": %d",Voiture.showBar(0));
SFont_Write(screen,font,200,10,"Showbar (b)");
SFont_Write(screen,font,300,10,bf);
sprintf(bf,": %d",SHOWcamera);
SFont_Write(screen,font,200,25,"Showcam (c)");
SFont_Write(screen,font,300,25,bf);
sprintf(bf,": %d",SHOWpilot);
SFont_Write(screen,font,200,40,"Showpilot (p)");
SFont_Write(screen,font,300,40,bf);
sprintf(bf,": %d",obstacle);
SFont_Write(screen,font,200,55,"Obstacle (o)");
SFont_Write(screen,font,300,55,bf);
sprintf(bf,": %d",Voiture.incAngleStep(0));
SFont_Write(screen,font,200,70,"AStep (pgU-D)");
SFont_Write(screen,font,300,70,bf);
sprintf(bf,": %d",Voiture.showNetworkState());
SFont_Write(screen,font,200,85,"NeuronIface(i)");
SFont_Write(screen,font,300,85,bf);
DrawLine(screen,0,99,319,99,0,0,128);
}
void Fenetre::redraw()
{
#ifdef __DEBUG_TIME
Uint32 deltatime;
#endif
if(sec!=time(NULL)) { sec=time(NULL); fps=frame; frame=0; }
frame++;
#ifdef __DEBUG_TIME
deltatime=SDL_GetTicks();
#endif
/* SDL_BlitSurface( fond, NULL, screen, NULL); Software Mode */
SDL_BlitSurface( HardBG, NULL, screen , NULL); /* Hardware Mode */
#ifdef __DEBUG_TIME
printf("Background Blit : %d ms, ",SDL_GetTicks()-deltatime);
deltatime=SDL_GetTicks();
#endif
drawText();
if(SHOWpilot) showPilot(440,50);
if(SHOWcamera) showView(350,10);
if(obstacle) showObstacle();
Voiture.updateCar(screen);
#ifdef __DEBUG_TIME
printf("Car Update : %d ms, ",SDL_GetTicks()-deltatime);
deltatime=SDL_GetTicks();
#endif
SDL_Flip (screen);
#ifdef __DEBUG_TIME
printf("Screen Refresh : %d ms\n",SDL_GetTicks()-deltatime);
#endif
}
void Fenetre::putPixel(int x, int y, char r, char g, char b)
{
setpixel(screen,x,y,r,g,b);
}
void Fenetre::handle()
{
int exitkey = 0;
bool shownet= 0;
int waitdel = 20;
SDL_Event event;
while (!exitkey)
{
SDL_Delay (waitdel);
redraw();
while (SDL_PollEvent (&event))
{
switch (event.type)
{
case SDL_QUIT:
exitkey = 1;
printf ("Quit!\n");
break;
case SDL_KEYDOWN:
switch (event.key.keysym.sym)
{
case SDLK_ESCAPE:
Voiture.harakiri();
exitkey = 1;
break;
case SDLK_LEFT:
Voiture.decAngle();
break;
case SDLK_RIGHT:
Voiture.incAngle();
break;
case SDLK_DOWN:
Voiture.decSpeed();
break;
case SDLK_UP:
Voiture.incSpeed();
break;
case SDLK_a:
Voiture.APToggle();
break;
case SDLK_b:
Voiture.showBar(1);
break;
case SDLK_c:
SHOWcamera=(SHOWcamera+1)%2;
break;
case SDLK_i:
shownet=(shownet+1)%2;
Voiture.showNetwork(shownet);
break;
case SDLK_n:
Voiture.neurToggle();
break;
case SDLK_o:
obstacle=(obstacle+1)%2;
break;
case SDLK_p:
SHOWpilot=(SHOWpilot+1)%2;
break;
case SDLK_r:
Voiture.reset();
break;
case SDLK_t:
Voiture.turn180();
break;
case SDLK_w:
Voiture.saveState();
//SData->action=SAVE_STATE;
break;
case SDLK_x:
Voiture.loadState();
//SData->action=RELOAD_STATE;
break;
case SDLK_F1:
printf("Chargement Circuit 1\n");
circuit=1;
loadBG("Back/bg1.bmp");
break;
case SDLK_F2:
printf("Chargement Circuit 2\n");
circuit=2;
loadBG("Back/bg2.bmp");
break;
case SDLK_F3:
printf("Chargement Circuit 3\n");
circuit=3;
loadBG("Back/bg3.bmp");
break;
case SDLK_F4:
printf("Chargement Circuit 4\n");
circuit=4;
loadBG("Back/bg4.bmp");
break;
case SDLK_F5:
printf("Chargement Circuit 5\n");
circuit=5;
loadBG("Back/bg5.bmp");
break;
case SDLK_F6:
printf("Chargement Circuit 6\n");
circuit=6;
loadBG("Back/bg6.bmp");
break;
case SDLK_F7:
Voiture.incTraineeSize(-256);
break;
case SDLK_F8:
Voiture.incTraineeSize(256);
break;
case SDLK_F9:
printf("Chargement Voiture 1\n");
Voiture.loadImage("Cars/voiture1.bmp");
break;
case SDLK_F10:
printf("Chargement Voiture 2\n");
Voiture.loadImage("Cars/voiture2.bmp");
break;
case SDLK_F11:
printf("Chargement Voiture 3\n");
Voiture.loadImage("Cars/voiture3.bmp");
break;
case SDLK_F12:
printf("Chargement Voiture 4\n");
Voiture.loadImage("Cars/voiture4.bmp");
break;
case SDLK_PAGEUP:
Voiture.incAngleStep(1);
break;
case SDLK_PAGEDOWN:
Voiture.incAngleStep(-1);
break;
case SDLK_HOME:
waitdel+=5;
break;
case SDLK_END:
waitdel-=5;
if(waitdel<0) waitdel=0;
break;
default :
break;
}
break;
}
break;
}
}
}
+34
View File
@@ -0,0 +1,34 @@
#ifndef __FENETRE_H
#define __FENETRE_H
#include "car.h"
#include "SFont.h"
#define FONT_PATH "font.png"
class Fenetre
{
private:
SDL_Surface* screen;
SDL_Surface* fond;
SDL_Surface* HardBG;
SFont_Font* font;
Car Voiture;
int frame,sec,fps,circuit;
bool SHOWpilot,SHOWcamera,obstacle;
public:
Fenetre();
Fenetre (int , int , int , char *, char *);
~Fenetre();
void loadBG(char *);
void redraw();
void handle();
void putPixel(int , int , char, char, char);
void showView(int ,int);
void showPilot(int,int);
void showObstacle();
void drawText();
};
#endif
+34
View File
@@ -0,0 +1,34 @@
#ifndef __FENETRE_H
#define __FENETRE_H
#include "car.h"
#include "SFont.h"
#define FONT_PATH "font.png"
class Fenetre
{
private:
SDL_Surface* screen;
SDL_Surface* fond;
SDL_Surface* HardBG;
SFont_Font* font;
car Voiture;
int frame,sec,fps,circuit;
bool SHOWpilot,SHOWcamera,obstacle;
public:
Fenetre();
Fenetre (int , int , int , char *, char *);
~Fenetre();
void loadBG(char *);
void redraw();
void handle();
void putPixel(int , int , char, char, char);
void showView(int ,int);
void showPilot(int,int);
void showObstacle();
void drawText();
};
#endif
Binary file not shown.
Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

BIN
View File
Binary file not shown.
+17
View File
@@ -0,0 +1,17 @@
#include <iostream>
#include <SDL.h>
#include "fenetre.h"
using namespace std;
int main()
{
Fenetre f(800,600,32,"Auto Pilot","Back/bg1.bmp");
f.redraw();
f.handle();
return 1;
}
Binary file not shown.
+238
View File
@@ -0,0 +1,238 @@
#include <stdio.h>
#include <stdlib.h>
#include <SDL.h>
#include <math.h>
#include "sdlcommon.h"
bool inCarArea(SDL_Surface *screen, int x, int y)
{
if(x<0 || y<0 || x>screen->w || y>screen->h) return 0;
if(x<320 && y<100) return 0;
//DrawLine(screen,0,100,320,100,0,0,128);
return 1;
}
void setpixel(SDL_Surface *screen, int x, int y, Uint8 r, Uint8 g, Uint8 b)
{
Uint8 *ubuff8;
Uint16 *ubuff16;
Uint32 *ubuff32;
Uint32 color;
char c1, c2, c3;
if(x >= screen->w || x < 0 || y >= screen->h || y <0 ) return;
if(SDL_MUSTLOCK(screen))
{
if(SDL_LockSurface(screen) < 0)
return;
}
color = SDL_MapRGB( screen->format, r, g, b );
switch(screen->format->BytesPerPixel)
{
case 1:
ubuff8 = (Uint8*) screen->pixels;
ubuff8 += (y * screen->pitch) + x;
*ubuff8 = (Uint8) color;
break;
case 2:
ubuff8 = (Uint8*) screen->pixels;
ubuff8 += (y * screen->pitch) + (x*2);
ubuff16 = (Uint16*) ubuff8;
*ubuff16 = (Uint16) color;
break;
case 3:
ubuff8 = (Uint8*) screen->pixels;
ubuff8 += (y * screen->pitch) + (x*3);
if(SDL_BYTEORDER == SDL_LIL_ENDIAN)
{
c1 = (color & 0xFF0000) >> 16;
c2 = (color & 0x00FF00) >> 8;
c3 = (color & 0x0000FF);
}
else {
c3 = (color & 0xFF0000) >> 16;
c2 = (color & 0x00FF00) >> 8;
c1 = (color & 0x0000FF);
}
ubuff8[0] = c3;
ubuff8[1] = c2;
ubuff8[2] = c1;
break;
case 4:
ubuff8 = (Uint8*) screen->pixels;
ubuff8 += (y*screen->pitch) + (x*4);
ubuff32 = (Uint32*)ubuff8;
*ubuff32 = color;
break;
default:
fprintf(stderr, "Error: Unknown bitdepth!\n");
}
if(SDL_MUSTLOCK(screen))
{
SDL_UnlockSurface(screen);
}
}
int getpixel(SDL_Surface *screen, int x, int y)
{
int bpp = screen->format->BytesPerPixel;
Uint8 *p = (Uint8 *)screen->pixels + y * screen->pitch + x * bpp;
switch(bpp)
{
case 1:
return *p;
case 2:
return *(Uint16 *)p;
case 3:
if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
return p[0] << 16 | p[1] << 8 | p[2];
else
return p[0] | p[1] << 8 | p[2] << 16;
case 4:
return *(Uint32 *)p;
default:
return 0;
}
}
void interpole(SDL_Surface* src, SDL_Surface* dst, Uint32 nulcol)
{
/* Interpolation */
Uint32 ipol;
for(int i=1;i< (src->w-1) ;i++)
for(int j=1;j< (src->h-1);j++)
{
if((ipol=getpixel(src,i,j))==nulcol)
{
Uint32 ir=0,ig=0,ib=0;
int tot=0;
if((ipol=getpixel(src,i-1,j-1))!=NULLCOL){
ir+=(ipol>>16)%256; ig+=(ipol>>8)%256; ib+=(ipol)%256;
tot++;
}
if((ipol=getpixel(src,i-1,j-1))!=NULLCOL){
ir+=(ipol>>16)%256; ig+=(ipol>>8)%256; ib+=(ipol)%256;
tot++;
}
if((ipol=getpixel(src,i-1,j-1))!=NULLCOL){
ir+=(ipol>>16)%256; ig+=(ipol>>8)%256; ib+=(ipol)%256;
tot++;
}
if((ipol=getpixel(src,i-1,j-1))!=NULLCOL){
ir+=(ipol>>16)%256; ig+=(ipol>>8)%256; ib+=(ipol)%256;
tot++;
}
if((ipol=getpixel(src,i-1,j-1))!=NULLCOL){
ir+=(ipol>>16)%256; ig+=(ipol>>8)%256; ib+=(ipol)%256;
tot++;
}
if((ipol=getpixel(src,i-1,j-1))!=NULLCOL){
ir+=(ipol>>16)%256; ig+=(ipol>>8)%256; ib+=(ipol)%256;
tot++;
}
if((ipol=getpixel(src,i-1,j-1))!=NULLCOL){
ir+=(ipol>>16)%256; ig+=(ipol>>8)%256; ib+=(ipol)%256;
tot++;
}
if((ipol=getpixel(src,i-1,j-1))!=NULLCOL){
ir+=(ipol>>16)%256; ig+=(ipol>>8)%256; ib+=(ipol)%256;
tot++;
}
if((ipol=getpixel(src,i,j-2))!=NULLCOL){
ir+=(ipol>>16)%256; ig+=(ipol>>8)%256; ib+=(ipol)%256;
tot++;
}
if((ipol=getpixel(src,i,j+1))!=NULLCOL){
ir+=(ipol>>16)%256; ig+=(ipol>>8)%256; ib+=(ipol)%256;
tot++;
}
if((ipol=getpixel(src,i-2,j))!=NULLCOL){
ir+=(ipol>>16)%256; ig+=(ipol>>8)%256; ib+=(ipol)%256;
tot++;
}
if((ipol=getpixel(src,i+2,j))!=NULLCOL){
ir+=(ipol>>16)%256; ig+=(ipol>>8)%256; ib+=(ipol)%256;
tot++;
}
if(tot>3)
{
ir/=tot;
ig/=tot;
ib/=tot;
//printf("interpol pixel %d,%d, with %d,%d,%d (%d)\n",i,j,ir,ig,ib,tot);
setpixel(dst,i,j,(Uint8)ir,(Uint8)ig,(Uint8)ib);
//setpixel(dst,i,j,255,0,0);
}
// else pixel à interpolé entouré de rien ...
}
else {
setpixel(dst,i,j,(Uint8)(ipol>>16),(Uint8)(ipol>>8),(Uint8)(ipol));
//setpixel(dst,i,j,0,255,0);
}
}
}
void DrawLine(SDL_Surface* screen, int x1, int y1, int x2, int y2, char r, char g, char b)
{
double dx,dy,x,y;
int step,i;
if(abs(x2-x1)>abs(y2-y1)) step=abs(x2-x1);
else step=abs(y2-y1);
dx=(x2-x1);
dy=(y2-y1);
dx/=step;
dy/=step;
x=x1;
y=y1;
for(i=0;i<step;i++)
{
x=x+dx;
y=y+dy;
setpixel(screen,(int)x,(int)y,r,g,b);
}
}
void rotatesurface(SDL_Surface *src, SDL_Surface *dst, int anginc)
{
Uint32 pix,x,y;
double angle,ba,ca,norm;
Uint8 r,g,b;
SDL_Surface *tmp;
SDL_Rect rec;
rec.h=src->h;
rec.w=src->w;
rec.x=0;
rec.y=0;
tmp=SDL_CreateRGBSurface(SDL_HWSURFACE, src->w, src->h, 32, 0,0,0,0);
SDL_FillRect(tmp,&rec, SDL_MapRGB(tmp->format,NULLCOLR,NULLCOLG,NULLCOLB/**/));
SDL_FillRect(dst,&rec, SDL_MapRGB(tmp->format,NULLCOLR,NULLCOLG,NULLCOLB/**/));
for(int i=0;i<src->w;i++)
for(int j=0;j<src->h;j++)
{
pix=getpixel(src,i,j);
r=pix>>16; // couleur R
g=pix>>8; // couleur G
b=pix; // couleur B
ba=(src->w/2)-i; // base
ca=(src->h/2)-j; // cote adjascent
norm=sqrt(ba*ba+ca*ca); // norme
angle=atan(ca/ba); // angle
if(i>(src->w)/2) angle+=(PI);
angle+=(anginc*PI/180); // angle tourné
x=(int)round((src->w/2)-(norm*cos(angle)));
y=(int)round((src->h/2)-(norm*sin(angle)));
setpixel(tmp, x,y, r,g,b);
}
pix=getpixel(src,(src->w/2),(src->h/2));
setpixel(tmp, (src->w/2),(src->h/2), pix>>16,pix>>8,pix);
//SDL_BlitSurface(tmp , NULL, dst, NULL);
interpole(tmp,dst, NULLCOL);
}
+28
View File
@@ -0,0 +1,28 @@
#ifndef __SDLCOMMON_H
#define __SDLCOMMON_H
#include <SDL.h>
#include <SDL_image.h>
#ifndef PI
#define PI 3.141592
#endif
#ifndef NULLCOL
#define NULLCOL 0x00FF00FF
#define NULLCOLA 0xFF
#define NULLCOLR 0xFF
#define NULLCOLG 0x00
#define NULLCOLB 0xFF
#endif
void setpixel(SDL_Surface*, int, int, Uint8, Uint8, Uint8);
int getpixel(SDL_Surface*, int, int);
void DrawLine(SDL_Surface*, int , int , int , int , char , char, char);
void rotatesurface(SDL_Surface*, SDL_Surface*, int);
void interpole(SDL_Surface* , SDL_Surface* , Uint32 );
bool inCarArea(SDL_Surface* , int , int );
#endif
+26
View File
@@ -0,0 +1,26 @@
#ifndef __SDLCOMMON_H
#define __SDLCOMMON_H
#ifndef PI
#define PI 3.141592
#endif
#ifndef NULLCOL
#define NULLCOL 0x00FF00FF
#define NULLCOLA 0xFF
#define NULLCOLR 0xFF
#define NULLCOLG 0x00
#define NULLCOLB 0xFF
#endif
void setpixel(SDL_Surface*, int, int, Uint8, Uint8, Uint8);
int getpixel(SDL_Surface*, int, int);
void DrawLine(SDL_Surface*, int , int , int , int , char , char, char);
void rotatesurface(SDL_Surface*, SDL_Surface*, int);
void interpole(SDL_Surface* , SDL_Surface* , Uint32 );
bool inCarArea(SDL_Surface* , int , int );
#endif
Binary file not shown.
+34
View File
@@ -0,0 +1,34 @@
#ifndef __SHMDATA_H
#define __SHMDATA_H
#define SHMKEY 5678
#define CAMERASIZE 128
#define CAMERASTEP 2
#define CAMERADISTANCE 50
#define CAMVECT (CAMERASIZE/CAMERASTEP)
#define NOMBRE_NEURONES_INTERMEDIAIRES 8
#define TAILLERESULTAT 3
#define SAVE_STATE 1
#define RELOAD_STATE 2
#define STATE_FILE "State.network"
struct shmdata
{
bool V[CAMVECT]; /* vecteur camera */
bool Res[3]; /* 0 gauche 1 droite 2 avancer 3 stop*/
int read; /* 1 = info a ete traitée */
int angle; /* angle de la voiture 0->359 */
int speed; /* vitesse */
int APon; /* pilote automatique (mode apprentissage) (0/1) */
int Neuron; /* activer le reseau de neurone (0/1) */
double pos_x; /* position x de la voiture */
double pos_y; /* position y de la voiture */
int action; /* action a executer (genre sauvegarde) */
int delay; /* delai entre les frames */
bool viewNetwork; /* afficher le reseau */
};
#endif
+33
View File
@@ -0,0 +1,33 @@
#ifndef __SHMDATA_H
#define __SHMDATA_H
#define SHMKEY 5678
#define CAMERASIZE 128
#define CAMERASTEP 2
#define CAMERADISTANCE 50
#define CAMVECT (CAMERASIZE/CAMERASTEP)
#define NOMBRE_NEURONES_INTERMEDIAIRES 8
#define TAILLERESULTAT 3
#define SAVE_STATE 1
#define RELOAD_STATE 2
#define STATE_FILE "State.network"
struct shmdata
{
bool V[CAMVECT]; /* vecteur camera */
bool Res[3]; /* 0 gauche 1 droite 2 avancer 3 stop*/
int read; /* 1 = info a ete traitée */
int angle; /* angle de la voiture 0->359 */
int speed; /* vitesse */
int APon; /* pilote automatique (mode apprentissage) (0/1) */
int Neuron; /* activer le reseau de neurone (0/1) */
double pos_x; /* position x de la voiture */
double pos_y; /* position y de la voiture */
int action; /* action a executer (genre sauvegarde) */
int delay; /* delai entre les frames */
};
#endif
+31
View File
@@ -0,0 +1,31 @@
CFLAGS= -g -fpermissive
CPP= g++
GCC= gcc
MAKE = make
#INCLUDES= -I ./Include
all : backprops interface autopilot
@mv server/AutoPilot ./
backprops :
@$(MAKE) -C backprop
interface :
@$(MAKE) -C InterfaceCpp
autopilot :
@$(MAKE) -C server
clean : cleanbackprop cleaninterface cleanautopilot
cleanbackprop :
@$(MAKE) -C backprop clean
cleaninterface :
@$(MAKE) -C InterfaceCpp clean
cleanautopilot :
@$(MAKE) -C server clean
mrproper : mrproperbackprop mrproperinterface mrproperautopilot
rm -rf AutoPilot
mrproperbackprop :
@$(MAKE) -C backprop mrproper
mrproperinterface :
@$(MAKE) -C InterfaceCpp mrproper
mrproperautopilot :
@$(MAKE) -C server mrproper
+4
View File
@@ -0,0 +1,4 @@
D/manuel_interface////
/class1.dia/1.2/Wed Apr 13 15:04:04 2005//
/interfaceUML.dia/1.1/Wed Apr 13 15:04:04 2005//
/reseau.dia/1.1/Wed Apr 13 15:06:49 2005//
+1
View File
@@ -0,0 +1 @@
ia2005/Rapport
+1
View File
@@ -0,0 +1 @@
:pserver:feeling@jfroche.be:/opt/cvsroot
+1860
View File
File diff suppressed because it is too large Load Diff
+1861
View File
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
+3838
View File
File diff suppressed because it is too large Load Diff
+1860
View File
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
Binary file not shown.

After

Width:  |  Height:  |  Size: 153 KiB

+2
View File
@@ -0,0 +1,2 @@
/Interface.tex/1.3/Wed Apr 13 15:04:04 2005//
D
+1
View File
@@ -0,0 +1 @@
ia2005/Rapport/manuel_interface
+1
View File
@@ -0,0 +1 @@
:pserver:feeling@jfroche.be:/opt/cvsroot
+67
View File
@@ -0,0 +1,67 @@
\documentclass[a4paper,oneside,12pt]{article}
\usepackage{supertabular,vmargin}
\usepackage[francais]{babel}
\usepackage[latin1]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[dvips]{graphicx}
\usepackage{listings}
\title{ Techniques avancées de systèmes d'exploitation \\ Projet 1 \\}
\author{ROCHE Jean-Francois \And DELEERS Laurent}
\date\today
\begin{document}
\begin{tabular}{|l|l|}
\hline
Escape & quitter l'interface\\
\hline
$\leftarrow$ & aller a gauche\\
\hline
$\rightarrow$ & aller a droite\\
\hline
$\uparrow$ & augmenter la vitesse\\
\hline
$\downarrow$ & diminuer la vitesse\\
\hline
a & activer le pilote automatique (aprentissage)\\
\hline
b & afficher vecteur camera\\
\hline
c & afficher la camera\\
\hline
n & enclencher le reseau de neurones\\
\hline
o & afficher un obstacle\\
\hline
p & afficher les fleches de pilotage\\
\hline
r & repositionner la voiture dans l'ecran (reset)\\
\hline
t & tourner la voiture de 180\\
\hline
w & sauver l'etat du reseau\\
\hline
x & reloader le fichier d'etat du reseau\\
\hline
F1-F6 & Changer de circuit \\
\hline
F7 F8 & Afficher une trainee \\
\hline
F9-F12 & Changer de voiture \\
\hline
Pg-Up & augmenter l'angle de virage \\
\hline
Pg-Down & diminuer l'angle de virage\\
\hline
Home & augmenter le delay inter frame (affichage + lent)\\
\hline
End & diminuer le delay inter frame (affichage + rapide mais plus de CPU)
\hline
\end{tabular}
\end {document}
BIN
View File
Binary file not shown.
Executable
+2
View File
@@ -0,0 +1,2 @@
cd $HOME/Documents/IA/ia2005/
./AutoPilot -n
+94
View File
@@ -0,0 +1,94 @@
#include "reseau.h"
reseau::reseau(int In, int Hid, int Out)
{
Ocouche=OutputCouche(In,&Hcouche);
Hcouche=HiddenCouche(Hid,&Ocouche);
Icouche=InputCouche(Out,&Hcouche);
initshm();
}
reseau::~reseau()
{
}
void reseau::forward(bool input[],bool target[])
{
std::vector<bool> tmp;
/* on active les couches */
for(unsigned i=0;i<Icouche.getNumber();i++) tmp.push_back(input[i]);
Icouche.activate(tmp);
Hcouche.activate(Icouche);
Ocouche.activate();
/* on place le resultat dans la shm */
for(unsigned i=0;i<Ocouche.getNumber();i++) target[i]=util.accept(Ocouche[i].getWeight());
}
void reseau::backward(bool input[], bool target[])
{
std::vector<double> hidDelta;
std::vector<double> outDelta;
/* Calcul des delta pour la couche OUPUT */
for(unsigned i=0;i<Ocouche.getNumber();i++)
{
outDelta.push_back(util.dsigmoid((double)target[i] - Ocouche[i].getWeight()));
}
/* Calcul des delta pour la couche HIDDEN */
for(unsigned i=0;i<Hcouche.getNumber();i++)
{
hidDelta.push_back(util.dsigmoid((double)target[i] - Hcouche[i].getWeight()));
}
Icouche.backPropagate(hidDelta);
Ocouche.backPropagate(outDelta);
}
void reseau::initshm()
{
if ((shmid = shmget(SHMKEY, sizeof(struct shmdata), 0666)) < 0)
{
perror("Unable to get shm id \n");
exit(1);
}
if ((SData = (struct shmdata *)shmat(shmid, NULL, 0)) == (struct shmdata *) -1)
{
perror("Unable to attach shm segment\n");
exit(1);
}
}
double reseau::getError(bool target[])
{
double error=0.0;
for(unsigned i=0;i<Ocouche.getNumber();i++)
{
error += pow(((double)target[i] - Ocouche[i].getWeight()),2);
}
return sqrt(error);
}
double reseau::learnOne(bool input[], bool target[])
{
double error=0;
while (error>LEARNACCEPT)
{
forward(input,target);
backward(input,target);
error=getError(target);
cout << "erreur : " << error << endl ;
}
return error;
}
double reseau::learnAll(std::vector<bool *> inputs, std::vector<bool *> targets)
{
double error=0.0;
unsigned i;
for(i=0;i<inputs.size();i++)
{
error+=learnOne(inputs[i],targets[i]);
}
return (double)(error/i);
}
/* END */
+23
View File
@@ -0,0 +1,23 @@
D/test////
/Makefile/1.11/Wed Apr 13 15:17:45 2005//
/Utils.cpp/1.5/Wed Apr 13 15:04:03 2005//
/Utils.h/1.3/Wed Apr 13 15:04:03 2005//
/couche.cpp/1.7/Wed Apr 13 16:23:02 2005//
/couche.h/1.6/Wed Apr 13 15:04:03 2005//
/global.h/1.13/Wed Apr 13 15:04:03 2005//
/hiddenCouche.cpp/1.3/Wed Apr 13 15:04:03 2005//
/hiddenCouche.h/1.8/Wed Apr 13 15:04:03 2005//
/inputCouche.cpp/1.7/Wed Apr 13 15:04:03 2005//
/inputCouche.h/1.7/Wed Apr 13 15:04:03 2005//
/inputtest.h/1.3/Wed Apr 13 15:04:03 2005//
/main.cpp/1.12/Wed Apr 13 15:04:03 2005//
/neurone.cpp/1.1.1.1/Wed Apr 13 15:04:03 2005//
/neurone.h/1.1.1.1/Wed Apr 13 15:04:03 2005//
/outputCouche.cpp/1.9/Wed Apr 13 15:04:03 2005//
/outputCouche.h/1.11/Wed Apr 13 15:04:03 2005//
/reseau.cpp/1.18/Wed Apr 13 15:04:03 2005//
/reseau.h/1.11/Wed Apr 13 15:04:03 2005//
/synapse.cpp/1.4/Wed Apr 13 15:04:03 2005//
/synapse.h/1.3/Wed Apr 13 15:04:03 2005//
/synapseMatrix.cpp/1.2/Wed Apr 13 15:04:03 2005//
/synapseMatrix.h/1.1/Wed Apr 13 15:04:03 2005//
+1
View File
@@ -0,0 +1 @@
ia2005/backprop
+1
View File
@@ -0,0 +1 @@
:pserver:feeling@jfroche.be:/opt/cvsroot
+48
View File
@@ -0,0 +1,48 @@
#$Id: Makefile,v 1.11 2005/04/10 11:46:45 feeling Exp $'
CFLAGS= -Wall -g -fpermissive -std=c++14
CPP= g++
GCC= gcc
EXECUTABLE = backkprop
#INCLUDES= -I ./Include
all : buildobjs
test: backprop testall
backprop : neurone.o main.o couche.o synapse.o synapseMatrix.o inputCouche.o outputCouche.o hiddenCouche.o reseau.o Utils.o
$(CPP) $(CFLAGS) $(INCLUDES) -o $(EXECUTABLE) neurone.o main.o couche.o synapse.o synapseMatrix.o inputCouche.o outputCouche.o hiddenCouche.o reseau.o Utils.o
buildobjs : neurone.o couche.o synapse.o synapseMatrix.o inputCouche.o outputCouche.o hiddenCouche.o reseau.o Utils.o
neurone.o : neurone.cpp
$(CPP) $(CFLAGS) $(INCLUDES) -c neurone.cpp
couche.o : couche.cpp
$(CPP) $(CFLAGS) $(INCLUDES) -c couche.cpp
inputCouche.o : inputCouche.cpp
$(CPP) $(CFLAGS) $(INCLUDES) -c inputCouche.cpp
outputCouche.o : outputCouche.cpp
$(CPP) $(CFLAGS) $(INCLUDES) -c outputCouche.cpp
hiddenCouche.o : hiddenCouche.cpp
$(CPP) $(CFLAGS) $(INCLUDES) -c hiddenCouche.cpp
synapse.o : synapse.cpp
$(CPP) $(CFLAGS) $(INCLUDES) -c synapse.cpp
reseau.o : reseau.cpp
$(CPP) $(CFLAGS) $(INCLUDES) -c reseau.cpp
synapseMatrix.o : synapseMatrix.cpp
$(CPP) $(CFLAGS) $(INCLUDES) -c synapseMatrix.cpp
main.o : main.cpp
$(CPP) $(CFLAGS) $(INCLUDES) -c main.cpp
Utils.o : Utils.cpp
$(CPP) $(CFLAGS) $(INCLUDES) -c Utils.cpp
clean :
rm -f *.o
mrproper :
rm -rf *.o
rm -f $(EXECUTABLE)
cleano :
rm -f *.o
testall:
$(MAKE) -C test
cleantest:
rm -f *.o
$(MAKE) -C test clean
+25
View File
@@ -0,0 +1,25 @@
#include "Utils.h"
#include <math.h>
#include <iostream>
using namespace std;
Utils::Utils()
{
}
double Utils::sigmoid(const double val)
{
return tanh(val);
}
double Utils::dsigmoid(const double val)
{
return (1.0-(val*val));
}
bool Utils::accept(const double val)
{
if(val>ACCEPT) return 1;
return 0;
}
+13
View File
@@ -0,0 +1,13 @@
#ifndef __UTILS_H
#define __UTILS_H
#include "global.h"
class Utils {
private:
public:
Utils();
double sigmoid(double val);
double dsigmoid(double val);
bool accept(double val);
};
#endif
BIN
View File
Binary file not shown.
+109
View File
@@ -0,0 +1,109 @@
#include "couche.h"
Couche::Couche(){
nbrNeurone=0;
nextCouche=NULL;
listNeurone=NULL;
synSortantes = SynapseMatrix();
}
Couche::~Couche(){
deleteList();
}
Couche::Couche(const unsigned int nbr,const Couche* next){
nbrNeurone=(unsigned int)nbr;
if(next!=NULL){
setNextCouche(next);
}
else{
nextCouche = NULL;
synSortantes = SynapseMatrix();
}
listNeurone=NULL;
for(unsigned int i=0;i<nbr;++i)
listNeurone=new Neurone(0.0,listNeurone);
}
const unsigned int Couche::getNumber() const{
return nbrNeurone;
}
Neurone& Couche::operator[](const unsigned int i) const throw(const unsigned int){
if(i>(getNumber()-1))
throw(i);
Neurone* res=listNeurone;
for(unsigned int j=0;j<i;++j)
res=res->getNextNeurone();
return *res;
}
bool Couche::operator==(const Couche c){
return ((this)==(&c));
}
Couche& Couche::operator=(const Couche& c){
if(this!=&c){
nbrNeurone=c.getNumber();
nextCouche=c.getNextCouche();
deleteList();
listNeurone=copy(c.listNeurone);
synSortantes=c.synSortantes;
}
return *this;
}
void Couche::setNextCouche(const Couche* c) throw(std::string){
//cout << "setnextcouche : " << this << " -> " << c << endl;
if(c==NULL){
throw(std::string("Passing NULL as next couche forbiden"));
}
else{
nextCouche=(Couche*)c;
synSortantes = SynapseMatrix(getNumber(),c->getNumber());
synSortantes.randomize();
}
}
void Couche::randomizeSynapseMatrix(){
synSortantes.randomize();
}
Couche* Couche::getNextCouche() const{
return nextCouche;
}
void Couche::deleteN(Neurone* n){
if(n){
deleteN(n->getNextNeurone());
delete n;
n=NULL;
}
}
void Couche::deleteList(){
deleteN(listNeurone);
}
Neurone* Couche::copy(Neurone* n){
if(n){
return new Neurone(n->getWeight(),copy(n->getNextNeurone()));
}
return NULL;
}
Synapse& Couche::getSynapse(const unsigned int neurThisCouche,const unsigned int neurNextCouche) const throw(const unsigned int,std::string){
return synSortantes(neurThisCouche,neurNextCouche);
}
ostream & operator<<(ostream& os,const Couche & c){
unsigned int size = c.getNumber();
for(unsigned int i=0 ; i<size ; i++)
os << setw(6) << c[i].getWeight() << " ";
os << endl;
return os;
}
void Couche::printSynapseMatrix(){
cout << synSortantes;
}
+35
View File
@@ -0,0 +1,35 @@
#ifndef _COUCHE_H
#define _COUCHE_H
#include <iostream>
#include "neurone.h"
#include "synapseMatrix.h"
#include "Utils.h"
using namespace std;
class Couche{
private:
Neurone* listNeurone;
Couche* nextCouche;
unsigned int nbrNeurone;
void deleteN(Neurone* n);
void deleteList();
Neurone* copy(Neurone* n);
protected:
Utils util;
SynapseMatrix synSortantes;
public:
Couche();
~Couche();
Couche(const unsigned int nbr,const Couche* next=NULL);
const unsigned int getNumber() const;
Neurone& operator [](const unsigned int i) const throw(const unsigned int);
bool operator ==(const Couche c);
Couche& operator=(const Couche& c);
void setNextCouche(const Couche* c) throw(std::string);
Couche* getNextCouche() const;
void randomizeSynapseMatrix();
Synapse& getSynapse(const unsigned int neurThisCouche,const unsigned int neurNextCouche) const throw(const unsigned int,std::string);
friend ostream& operator<<(ostream& os,const Couche &c);
void printSynapseMatrix();
};
#endif
BIN
View File
Binary file not shown.
+11
View File
@@ -0,0 +1,11 @@
#ifndef _GLOBAL_H
#define _GLOBAL_H
#define SYNAPSE_WEIGHT_MAX 2.0
#define SYNAPSE_WEIGHT_MIN -2.0
#define LEARNING_RATE 0.05
#define MOMENTUM 0.1
#define ACCEPT 0.65 /* Was 0.95 */
#define LEARNACCEPT 0.15 /* Was 0.05 */
#endif
+11
View File
@@ -0,0 +1,11 @@
#ifndef _GLOBAL_H
#define _GLOBAL_H
#define SYNAPSE_WEIGHT_MAX 2.0
#define SYNAPSE_WEIGHT_MIN -2.0
#define LEARNING_RATE 0.01
#define MOMENTUM 0.1
#define ACCEPT 0.65 /* Was 0.95 */
#define LEARNACCEPT 0.15 /* Was 0.05 */
#endif
+15
View File
@@ -0,0 +1,15 @@
#include "hiddenCouche.h"
void HiddenCouche::activate(const Couche &prevCouche){
unsigned int prevSize = prevCouche.getNumber();
unsigned int thisSize = getNumber();
double sum;
for(unsigned int j=0; j < thisSize ; ++j){
sum = 0.0;
for(unsigned int i=0; i < prevSize ; ++i){
sum += prevCouche[i].getWeight() * prevCouche.getSynapse(i,j).getWeight();
}
(*this)[j] = util.sigmoid(sum);
}
}
+15
View File
@@ -0,0 +1,15 @@
#ifndef __HIDDNCOUCHE_H
#define __HIDDNCOUCHE_H
#include "couche.h"
#include <math.h>
class HiddenCouche : public Couche{
private:
public:
HiddenCouche() : Couche(){};
HiddenCouche(const unsigned int nbr,const Couche* next=NULL) : Couche(nbr,next){};
void activate(const Couche &prevCouche);
};
#endif
Binary file not shown.
+60
View File
@@ -0,0 +1,60 @@
#include "inputCouche.h"
InputCouche::InputCouche() : Couche(){
//cout << " Create empty PrevChange Matrix [in InputCouche()]\n";
prevChange = SynapseMatrix();
//cout << " Done\n";
}
InputCouche::InputCouche(const unsigned int nbr,const Couche* next) : Couche(nbr,next){
//cout << " Create PrevChange Matrix [in InputCouche(const unsigned int nbr,const Couche* next)]\n";
if(next!=NULL)
prevChange = SynapseMatrix(getNumber(),next->getNumber());
else
prevChange = SynapseMatrix();
//cout << prevChange;
//cout << " Done\n";
}
void InputCouche::setNextCouche(const HiddenCouche* c) throw(std::string){
Couche::setNextCouche(c);
prevChange = SynapseMatrix(getNumber(),c->getNumber());
}
void InputCouche::activate(const std::vector<bool> &v) throw(std::string){
unsigned int size = v.size();
if(size != getNumber()-1){
throw(std::string("Wrong number of Input passed to InputCouche"));
}
for(unsigned int i=0 ; i<size ; ++i){
(*this)[i].setWeight((double)v[i]);
}
}
void InputCouche::backPropagate(const std::vector<double> deltaHidden){
unsigned int size = deltaHidden.size();
double change;
if(size != getNextCouche()->getNumber()) // prend les delta de la couche cachée!
throw(std::string("Wrong number of delta passed to InputCouche for back propagation"));
for(unsigned int i=0; i < getNumber() ; ++i)
for(unsigned int j=0; j < getNextCouche()->getNumber(); ++j){
change = (deltaHidden[j] * (*this)[i].getWeight());
synSortantes(i,j) = (getSynapse(i,j).getWeight()) + (LEARNING_RATE*change) + (MOMENTUM*getChange(i,j));
prevChange(i,j) = change;
}
}
double InputCouche::getChange(const unsigned int neurThisCouche,const unsigned int neurNextCouche) const throw(const unsigned int,std::string){
return prevChange(neurThisCouche,neurNextCouche).getWeight();
}
void InputCouche::printMatrix(){
cout << prevChange;
}
InputCouche& InputCouche::operator=(const InputCouche& c){
if(this!=&c){
Couche::operator=(c);
prevChange = c.prevChange;
}
return (*this);
};
+25
View File
@@ -0,0 +1,25 @@
#ifndef __INPUTCOUCHE_H
#define __INPUTCOUCHE_H
#include <vector>
#include <string>
#include "couche.h"
#include "hiddenCouche.h"
#include "global.h"
using namespace std;
class InputCouche : public Couche{
private:
SynapseMatrix prevChange;
public:
InputCouche();
InputCouche(const unsigned int nbr,const Couche* next=NULL);
void activate(const std::vector<bool> &v) throw(std::string);
void setNextCouche(const HiddenCouche* c) throw(std::string);
void backPropagate(const std::vector<double> deltaHidden);
double getChange(const unsigned int i, const unsigned int j) const throw(const unsigned int,std::string);
void printMatrix();
InputCouche& operator=(const InputCouche& c);
};
#endif
Binary file not shown.
+152
View File
@@ -0,0 +1,152 @@
static bool Inp[9][16]=
{
{1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0},
{0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0},
{0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0},
{0,0,0,1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ndR[4][1]=
{
{0},
{0},
{0},
{1}
};
static bool XorI[4][2]=
{
{0,0},
{0,1},
{1,0},
{1,1}
};
static bool XorR[4][1]=
{
{0},
{1},
{1},
{0}
};
+127
View File
@@ -0,0 +1,127 @@
#include "neurone.h"
#include "couche.h"
#include "inputCouche.h"
#include "synapse.h"
#include "synapseMatrix.h"
#include "reseau.h"
#include <iostream>
#include "inputtest.h"
//using namespace std;
void testafroche()
{
try {
Neurone NewNeu();
Couche Output(4);
Couche Input(3,&Output);
Synapse s(2.324232);
SynapseMatrix m(3,2);
InputCouche InputC(3);
cout << InputC ;
cout << "get syn:" << Input.getSynapse(2,2).getWeight() << endl;
m(1,1) = 2.5;
cout<< "Test: " << m(1,1).getWeight() << endl;
m.randomize();
cout<< m ;
//Input[4];
Input[2]=2.132;
Input.getSynapse(1,2).getWeight();
cout << "Synapse weight: " << s << endl;
}
catch(std::string str){
cout << "Exception: " <<endl;
cout << " "<< str << endl;
}
catch(const unsigned int index){
cout << "Exception: " <<endl;
cout << " Index " << index << " is out of range" << endl;
}
}
void testlau()
{
Utils u;
int inputsize = /*16; */ 64; /* */ // nombre de neurones d'input
int outputsize = /*3; */ 3; /* */ // nombre de neurones d'output
int nbtests = /*9; */ 32; /* */ // nombre de tests (xor = 4)
int intermid = /*8; */ 8; /* */ // nombre de neurones intermediaires
int nbtests2 = /*8; */ 8; /* */ // nombre de tests différents du learn
bool Input[nbtests][inputsize];
bool Output[nbtests][outputsize];
bool InputTest[nbtests2][inputsize];
for(int i=0;i<nbtests;i++)
for(int j=0;j<inputsize;j++)
Input[i][j] = /*Inp[i][j]; /**/ VoitureI[i][j];
for(int i=0;i<nbtests2;i++)
for(int j=0;j<inputsize;j++)
InputTest[i][j] = /*Inp_[i][j]; /**/ VoitureI_[i][j];
for(int i=0;i<nbtests;i++)
for(int j=0;j<outputsize;j++)
Output[i][j] = /*Rep[i][j];/**/ VoitureR[i][j];
cout << "Init Network ...." << endl;
Reseau R(inputsize,intermid,outputsize);
cout << "Done ...." << endl;
std::vector<bool *> in;
std::vector<bool *> r;
for(int i=0;i<nbtests;i++)
{
in.push_back(Input[i]);
r.push_back(Output[i]);
}
double erreur ;
int cpt=0;
while(1)
{
++cpt;
erreur = R.learnAll(in,r);
if(erreur < LEARNACCEPT)
break;
if(cpt%100 == 0)
cout << " : step " << cpt << " error:" << erreur << endl;
}
cout << "final error: " << erreur << endl;
/* et on teste */
cout << "tests sur les jeux de données : \n";
std::vector<double> V;
for(int i=0;i<nbtests;i++)
{
V=R.forward(Input[i]);
cout << "pour : ";
for(int j=0;j<inputsize;j++) cout << Input[i][j] ;
for(unsigned i=0;i<V.size();i++)
cout << " [" << u.accept(V[i]) << "]";
cout << " bool " ;
for(unsigned i=0;i<V.size();i++)
cout << " [" << V[i] << "] " ;
cout << "\n";
}
cout << "tests différents : \n";
V.clear();
for(int i=0;i<nbtests2;i++)
{
V=R.forward(InputTest[i]);
cout << "pour : ";
for(int j=0;j<inputsize;j++) cout << InputTest[i][j];
for(unsigned i=0;i<V.size();i++)
cout << " [" << u.accept(V[i]) << "]";
cout << " bool " ;
for(unsigned i=0;i<V.size();i++)
cout << " [" << V[i] << "] " ;
cout << "\n";
}
}
int main(void){
testlau();
return 0;
}
+43
View File
@@ -0,0 +1,43 @@
#include "neurone.h"
Neurone::Neurone(){
nextNeurone=NULL;
weight=0.0;
}
Neurone::Neurone(const double w,const Neurone* next){
nextNeurone=(Neurone*)next;
weight=(double)w;
}
bool Neurone::getState() const{
return bool(weight > ACTIVE);
}
double Neurone::getWeight() const{
return weight;
}
void Neurone::setWeight(const double a){
weight=a;
}
void Neurone::setNextNeurone(const Neurone* next){
nextNeurone=(Neurone*)next;
}
Neurone* Neurone::getNextNeurone() const{
return nextNeurone;
}
void Neurone::operator =(double w){
weight = w;
}
Neurone& Neurone::operator =(const Neurone& n){
if(this!=&n){
weight = n.getWeight();
nextNeurone = n.getNextNeurone();
}
return *this;
}
+23
View File
@@ -0,0 +1,23 @@
#ifndef _NEURONE_H
#define _NEURONE_H
#include <iostream>
#include <stdlib.h>
#define ACTIVE 0.9
using namespace std;
class Neurone{
private:
double weight;
Neurone* nextNeurone;
public:
Neurone();
Neurone(const double w,const Neurone* next=NULL);
bool getState() const;
double getWeight() const;
void setWeight(const double a);
void setNextNeurone(const Neurone* next);
Neurone* getNextNeurone() const;
void operator =(double w);
Neurone& operator =(const Neurone& n);
};
#endif
Binary file not shown.
+66
View File
@@ -0,0 +1,66 @@
#include "outputCouche.h"
OutputCouche::OutputCouche() : Couche(){
//cout << " Create empty PrevChange Matrix [in OutputCouche()]\n";
prevChange = SynapseMatrix();
//cout << " Done\n";
}
OutputCouche::OutputCouche(const unsigned int nbr,const HiddenCouche *prev) : Couche(nbr,NULL){
prevCouche = prev;
if(prev)
prevChange = SynapseMatrix(prev->getNumber(),getNumber());
else
prevChange = SynapseMatrix();
}
void OutputCouche::setPrevCouche(const HiddenCouche *prev){
prevCouche = prev;
//cout << " Create Synapse PrevChange [in OutputCouche.setPrevCouche(const HiddenCouche *prev)]\n";
prevChange = SynapseMatrix(prev->getNumber(),getNumber());
//cout << prevChange;
//cout << " Done\n";
}
void OutputCouche::activate(){
unsigned int prevSize = prevCouche->getNumber();
unsigned int thisSize = getNumber();
double sum;
for(unsigned int i=0; i < thisSize ; ++i){
sum = 0.0;
for(unsigned int j=0; j < prevSize ; ++j){
sum += (*prevCouche)[j].getWeight() * (*prevCouche).getSynapse(j,i).getWeight();
}
(*this)[i] = util.sigmoid(sum);
}
}
void OutputCouche::backPropagate(const std::vector<double> deltaOutput){
unsigned int prevSize = prevCouche->getNumber();
unsigned int thisSize = getNumber();
double change;
for(unsigned int i=0; i < prevSize ; ++i){
for(unsigned int j=0; j < thisSize ; ++j){
change = (deltaOutput[j] * (*prevCouche)[i].getWeight());
(*prevCouche).getSynapse(i,j) = (*prevCouche).getSynapse(i,j).getWeight() + LEARNING_RATE*change + MOMENTUM*getChange(i,j);
prevChange(i,j) = change;
}
}
}
double OutputCouche::getChange(const unsigned int neurPrevCouche,const unsigned int neurThisCouche) const throw(const unsigned int, std::string){
return prevChange(neurPrevCouche,neurThisCouche).getWeight();
}
void OutputCouche::printMatrix(){
cout << prevChange;
}
OutputCouche& OutputCouche::operator=(const OutputCouche& c){
if(this!=&c){
Couche::operator=(c);
prevChange = c.prevChange;
}
return (*this);
};
+20
View File
@@ -0,0 +1,20 @@
#include "couche.h"
#include "hiddenCouche.h"
#include <vector>
using namespace std;
class OutputCouche : public Couche{
private:
SynapseMatrix prevChange;
const HiddenCouche *prevCouche;
public:
OutputCouche();
OutputCouche(const unsigned int nbr,const HiddenCouche *prev=NULL);
void activate();
void backPropagate(const std::vector<double> deltaOutput);
double getChange(const unsigned int neurPrevCouche,const unsigned int neurThisCouche) const throw(const unsigned int, std::string);
/* void OutputCouche::setPrevCouche(const HiddenCouche *prev); */
void setPrevCouche(const HiddenCouche *prev);
void printMatrix();
OutputCouche& operator=(const OutputCouche& c);
};

Some files were not shown because too many files have changed in this diff Show More