Page 1 of 2

[Projet]LifeGame

Unread postPosted: 19 May 2016, 12:56
by Ti64CLi++
Bonjour,
Ayant vu la lenteur du jeu de la vie en Basic de Clément.7, j'ai voulu développé le mien, mais en un langage nettement plus rapide : le C.
Seulement, il ne fait pas du tout ce que je voudrais qu'il fasse.
Voilà mon code:

le main.c
Code: Select all
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <tice.h>
#include <math.h>

#include "keys.h"
#include "gc.h"

#include <lib/ce/graphc.h>
#include <lib/ce/fileioc.h>
#include <lib/ce/keypadc.h>

uint16_t globalColor;

void wait(uint16_t t)
{
    for (int i = 0 ; i < t ; i++)
        boot_WaitShort();
}

void main(void)
{
    int lifetab[100][100];
    int x_cur = 320/2-50, y_cur = 240/2-50;
    gc_InitGraph();
    gc_FillScrn(0x0F);
    gc_SetTextColor(255);
    gc_SetColorIndex(255);
    gc_NoClipRectangle(320/2-50,240/2-50,100,100);
    while(!isKeyPressed(KEY_2ND))
    {
        globalColor = 0;
        gc_drawPixel(x_cur,y_cur);
        if (isKeyPressed(KEY_ALPHA))    lifetab[x_cur - (320 / 2 - 50)][y_cur - (240 / 2 - 50)] = 1;
        if (isKeyPressed(KEY_LEFT) && x_cur > 320 / 2 - 50)
        {
            if (!lifetab[x_cur - (320 / 2 - 50)][y_cur - (240 / 2 - 50)])
            {
                globalColor = 255;
                gc_drawPixel(x_cur, y_cur);
            }
            x_cur--;
        }
        if (isKeyPressed(KEY_RIGHT) && x_cur < 320 / 2 + 50)
        {
            if (!lifetab[x_cur - (320 / 2 - 50)][y_cur - (240 / 2 - 50)])
            {
                globalColor = 255;
                gc_drawPixel(x_cur, y_cur);
            }
            x_cur++;
        }
        if (isKeyPressed(KEY_UP) && y_cur > 240 / 2 - 50)
        {
            if (!lifetab[x_cur - (320 / 2 - 50)][y_cur - (240 / 2 - 50)])
            {
                globalColor = 255;
                gc_drawPixel(x_cur, y_cur);
            }
            y_cur--;
        }
        if (isKeyPressed(KEY_DOWN) && y_cur < 240 / 2 + 50)
        {
            if (!lifetab[x_cur - (320 / 2 - 50)][y_cur - (240 / 2 - 50)])
            {
                globalColor = 255;
                gc_drawPixel(x_cur, y_cur);
            }
            y_cur++;
        }
        wait(5);
    }
    gc_CloseGraph();
}


le keys.h
Code: Select all
//Tanks to Adriweb for this code

#ifndef INC_2048C_KEYS_H
#define INC_2048C_KEYS_H

#include <stdint.h>

#define startKeys() do { (*(uint8_t*)0xF50000) = 3; } while(0)
#define stopKeys()  do { (*(uint8_t*)0xF50000) = 0; } while(0)

// key format : 2 bytes : keys group last addr byte + read mask
#define KEY_2ND     0x1220
#define KEY_MODE    0x1240
#define KEY_DEL     0x1280
#define KEY_ALPHA   0x1480
#define KEY_CLEAR   0x1C40

#define KEY_DOWN    0x1E01
#define KEY_LEFT    0x1E02
#define KEY_RIGHT   0x1E04
#define KEY_UP      0x1E08

#define KEY_ARROWS  0x1E

// 'key' having to be a uint16_t (use the defines above)
#define isKeyPressed(key)           ((bool)((*((uint8_t*)(0xF50000 + (uint8_t)((key) >> 8)))) & ((uint8_t)((key) & 0xFF))))
#define isKeyGroupPressed(group)    ((bool)((*((uint8_t*)(0xF50000 + (uint8_t)((group)))))))

#endif //INC_2048C_KEYS_H


et le gc.h
Code: Select all
#ifndef INC_GC_H
#define INC_GC_H

#include <stdint.h>

#define SCR_WIDTH   320
#define SCR_HEIGHT  240

static const uint16_t* screen_buf = (uint16_t*)0xD40000;
static const uint8_t* screen_buf_8bpp = (uint8_t*)0xD40000;
typedef uint16_t    color_t;

#define rgb2color(r, g, b)        ((color_t)((((r) << 8) & 0xf800) | (((g) << 3) & 0x07e0) | ((b) >> 3)))

#define gc_drawPixel(x, y)        *((uint16_t*)(screen_buf + (x) + (y)*SCR_WIDTH)) = globalColor;
#define gc_setColor(c)            globalColor = (c);
#define gc_setColorRGB(r, g, b)   globalColor = rgb2color((r), (g), (b));

#define gc_drawString(str, x, y) os_FontDrawText((str), (x), (y))
#define gc_drawStringTranp(str, x, y) os_FontDrawTransText((str), (x), (y))

extern color_t globalColor;

#endif //INC_2048C_GC_H


Je n'ai pas de screen à vous montrer, mais le fichier est en pièce jointe.
Merci de votre aide. :)

Re: [Projet]LifeGame

Unread postPosted: 19 May 2016, 12:59
by Adriweb
Hm, je vois que tu utilises les libraries de Mateo.
Donc tu n'as pas à reprendre mes keys.h et gc.h - utilise directement les fonctions des libraries graphc et keypadc.

Il y a déjà plusieurs jeux les utilisant, donc tu ne devrais pas avoir de problème à trouver des codes pour te familiariser avec :)

Re: [Projet]LifeGame

Unread postPosted: 19 May 2016, 13:01
by Wistaro
Tu devrais peut-être utiliser le project builder aussi, non ?

Tu dis que ton programme ne fais pas le résultat voulu, c'est à dire?

Re: [Projet]LifeGame

Unread postPosted: 19 May 2016, 13:03
by Adriweb
(Note: le project builder n'a pas encore les toutes dernières versions des libraries (et de la toolchain, cependant ces changements là ne seraient pas visibles pour l'utilisateur))

Re: [Projet]LifeGame

Unread postPosted: 19 May 2016, 15:12
by Ti64CLi++
J'utilise le PB. Mais je n'arrive pas à installer CEmu pour prendre une vidéo de l'écran :(

Re: [Projet]LifeGame

Unread postPosted: 20 May 2016, 14:34
by Ti64CLi++
Petite modification, rajouté une fonction wait pour ralentir ;)

Re: [Projet]LifeGame

Unread postPosted: 20 May 2016, 14:39
by Wistaro
neuronix wrote:Petite modification, rajouté une fonction wait pour ralentir ;)


C'est quoi cette fonction? Elle est là de base?

Re: [Projet]LifeGame

Unread postPosted: 20 May 2016, 14:41
by Ti64CLi++
Wistaro wrote:Elle est lade base?

Ça veut dire quoi?
C'est une fonction de ma conception ;)

Re: [Projet]LifeGame

Unread postPosted: 20 May 2016, 17:17
by Adriweb
Oui on la voit au début, avec un for sur boot_WaitShort();

Re: [Projet]LifeGame

Unread postPosted: 20 May 2016, 17:25
by Clément.7
Bonne idée de le porter sur TI-83 Premium CE :) par contre il existe déjà un LifeGame (ou Game of Life) sur ce modèle, à voir ce que tu peux rajouter.

Pour le mien je l'ai recodé en Axe et maintenant même avec une grille de 25x20 il va trop vite pour qu'on profite de la simulation :D
Mais il ne fonctionne du coup pas sur Premium CE...