From bf3909bfcc8d6acb006c0095e18d73c4854eefe6 Mon Sep 17 00:00:00 2001 From: Li Jia Date: Sun, 18 Oct 2015 23:31:57 +0800 Subject: [PATCH] Partial graphics and input works. --- Makefile | 5 +- dxlib.cpp | 684 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- dxlib.h | 31 +-- loadg.cpp | 5 +- main.cpp | 134 ++--------- main.h | 228 ------------------ 6 files changed, 694 insertions(+), 393 deletions(-) delete mode 100644 main.h diff --git a/Makefile b/Makefile index 6569ebe..dfd7801 100644 --- a/Makefile +++ b/Makefile @@ -1,2 +1,5 @@ debug: - emcc -g4 dxlib.cpp loadg.cpp main.cpp -o dlm.html + emcc -g4 dxlib.cpp loadg.cpp main.cpp -o dlm.html --preload-file res + +release: + emcc -o2 dxlib.cpp loadg.cpp main.cpp -o dlm.html --preload-file res diff --git a/dxlib.cpp b/dxlib.cpp index 29ef95d..ffb0442 100644 --- a/dxlib.cpp +++ b/dxlib.cpp @@ -1,22 +1,583 @@ #include "dxlib.h" -// グラフィックに設定する透過色をセットする -int SetTransColor(int Red, int Green, int Blue) { +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + + +static Display *x_display = NULL; +static EGLDisplay eglDisplay = NULL; +static EGLContext eglContext = NULL; +static EGLSurface eglSurface = NULL; + +//------------------------------------------------------------------------------ +// OpenGL util functions +//------------------------------------------------------------------------------ + +GLuint esLoadShader ( GLenum type, const char *shaderSrc ) +{ + GLuint shader; + GLint compiled; + + // Create the shader object + shader = glCreateShader ( type ); + + if ( shader == 0 ) + return 0; + + // Load the shader source + glShaderSource ( shader, 1, &shaderSrc, NULL ); + + // Compile the shader + glCompileShader ( shader ); + + // Check the compile status + glGetShaderiv ( shader, GL_COMPILE_STATUS, &compiled ); + + if ( !compiled ) + { + GLint infoLen = 0; + glGetShaderiv ( shader, GL_INFO_LOG_LENGTH, &infoLen ); + + if ( infoLen > 1 && infoLen < 1024) + { + char infoLog[infoLen]; + glGetShaderInfoLog ( shader, infoLen, NULL, infoLog ); + printf ( "Error compiling shader:\n%s\n", infoLog ); + } + + glDeleteShader ( shader ); + return 0; + } + + return shader; + +} + +GLuint esLoadProgram ( const char *vertShaderSrc, const char *fragShaderSrc ) +{ + GLuint vertexShader; + GLuint fragmentShader; + GLuint programObject; + GLint linked; + + // Load the vertex/fragment shaders + vertexShader = esLoadShader ( GL_VERTEX_SHADER, vertShaderSrc ); + if ( vertexShader == 0 ) + return 0; + + fragmentShader = esLoadShader ( GL_FRAGMENT_SHADER, fragShaderSrc ); + if ( fragmentShader == 0 ) + { + glDeleteShader( vertexShader ); + return 0; + } + + // Create the program object + programObject = glCreateProgram ( ); + + if ( programObject == 0 ) + return 0; + + glAttachShader ( programObject, vertexShader ); + glAttachShader ( programObject, fragmentShader ); + + // Link the program + glLinkProgram ( programObject ); + + // Check the link status + glGetProgramiv ( programObject, GL_LINK_STATUS, &linked ); + + if ( !linked ) + { + GLint infoLen = 0; + + glGetProgramiv ( programObject, GL_INFO_LOG_LENGTH, &infoLen ); + + if ( infoLen > 1 && infoLen < 1024) + { + char infoLog[infoLen]; + glGetProgramInfoLog ( programObject, infoLen, NULL, infoLog ); + printf ( "Error linking program:\n%s\n", infoLog ); + } + + glDeleteProgram ( programObject ); + return 0; + } + + // Free up no longer needed shader resources + glDeleteShader ( vertexShader ); + glDeleteShader ( fragmentShader ); + + return programObject; +} + +EGLBoolean CrateWindow(int width, int height) +{ + Window root; + XSetWindowAttributes swa; + Window win; + + /* + * X11 native display initialization + */ + + x_display = XOpenDisplay(NULL); + if ( x_display == NULL ) + { + return EGL_FALSE; + } + + root = DefaultRootWindow(x_display); + + swa.event_mask = ExposureMask | PointerMotionMask | KeyPressMask; + win = XCreateWindow( + x_display, root, + 0, 0, width, height, 0, + CopyFromParent, InputOutput, + CopyFromParent, CWEventMask, + &swa ); + + return EGL_TRUE; +} + +EGLBoolean CreateEGLContext (EGLint attribList[]) +{ + EGLint numConfigs; + EGLint majorVersion; + EGLint minorVersion; + EGLDisplay display; + EGLContext context; + EGLSurface surface; + EGLConfig config; + EGLint contextAttribs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE, EGL_NONE }; + + // Get Display + display = eglGetDisplay(EGL_DEFAULT_DISPLAY ); + if ( display == EGL_NO_DISPLAY ) + { + return EGL_FALSE; + } + + // Initialize EGL + if ( !eglInitialize(display, &majorVersion, &minorVersion) ) + { + return EGL_FALSE; + } + + // Get configs + if ( !eglGetConfigs(display, NULL, 0, &numConfigs) ) + { + return EGL_FALSE; + } + + // Choose config + if ( !eglChooseConfig(display, attribList, &config, 1, &numConfigs) ) + { + return EGL_FALSE; + } + + // Create a surface + surface = eglCreateWindowSurface(display, config, NULL, NULL); + if ( surface == EGL_NO_SURFACE ) + { + return EGL_FALSE; + } + + // Create a GL context + context = eglCreateContext(display, config, EGL_NO_CONTEXT, contextAttribs ); + if ( context == EGL_NO_CONTEXT ) + { + return EGL_FALSE; + } + + // Make the context current + if ( !eglMakeCurrent(display, surface, surface, context) ) + { + return EGL_FALSE; + } + + eglDisplay = display; + eglSurface = surface; + eglContext = context; + return EGL_TRUE; +} + +//------------------------------------------------------------------------------ +// Input functions +//------------------------------------------------------------------------------ +static byte key_state[256] = {0}; +static int joypad_state = 0; + +static EM_BOOL key_callback(int eventType, const EmscriptenKeyboardEvent *e, void *userData) +{ + if (e->repeat) + return 1; + + if (e->keyCode <= 0 || e->keyCode >= 256) + return 1; + + if (eventType == 2) { + key_state[e->keyCode] = 1; + } + else if (eventType == 3) { + key_state[e->keyCode] = 0; + } + + joypad_state = 0; + if (key_state[37]) joypad_state |= PAD_INPUT_LEFT; + if (key_state[38]) joypad_state |= PAD_INPUT_UP; + if (key_state[39]) joypad_state |= PAD_INPUT_RIGHT; + if (key_state[40]) joypad_state |= PAD_INPUT_DOWN; + + /* + printf("%d, key: \"%s\", code: \"%s\", location: %lu,%s%s%s%s repeat: %d, locale: \"%s\", char: \"%s\", charCode: %lu, keyCode: %lu, which: %lu\n", + eventType, e->key, e->code, e->location, + e->ctrlKey ? " CTRL" : "", e->shiftKey ? " SHIFT" : "", e->altKey ? " ALT" : "", e->metaKey ? " META" : "", + e->repeat, e->locale, e->charValue, e->charCode, e->keyCode, e->which); + */ + + return 1; +} + +EM_BOOL mouse_callback(int eventType, const EmscriptenMouseEvent *e, void *userData) +{ + if (eventType == 5) { + key_state[KEY_INPUT_RETURN] = 1; + } + else if (eventType == 6) { + key_state[KEY_INPUT_RETURN] = 0; + } + + /* + printf("%d, screen: (%ld,%ld), client: (%ld,%ld),%s%s%s%s button: %hu, buttons: %hu, movement: (%ld,%ld), canvas: (%ld,%ld)\n", + eventType, e->screenX, e->screenY, e->clientX, e->clientY, + e->ctrlKey ? " CTRL" : "", e->shiftKey ? " SHIFT" : "", e->altKey ? " ALT" : "", e->metaKey ? " META" : "", + e->button, e->buttons, e->movementX, e->movementY, e->canvasX, e->canvasY); + */ + return 0; } +EM_BOOL touch_callback(int eventType, const EmscriptenTouchEvent *e, void *userData) +{ + if (eventType == 22) { + key_state[KEY_INPUT_RETURN] = 1; + } + else if (eventType == 23) { + key_state[KEY_INPUT_RETURN] = 0; + } + /* + printf("%d, numTouches: %d %s%s%s%s\n", + eventType, e->numTouches, + e->ctrlKey ? " CTRL" : "", e->shiftKey ? " SHIFT" : "", e->altKey ? " ALT" : "", e->metaKey ? " META" : ""); + for(int i = 0; i < e->numTouches; ++i) + { + const EmscriptenTouchPoint *t = &e->touches[i]; + printf(" %ld: screen: (%ld,%ld), client: (%ld,%ld), page: (%ld,%ld), isChanged: %d, onTarget: %d, canvas: (%ld, %ld)\n", + t->identifier, t->screenX, t->screenY, t->clientX, t->clientY, t->pageX, t->pageY, t->isChanged, t->onTarget, t->canvasX, t->canvasY); + } + */ + + return 0; +} + +static void InputInit() { + emscripten_set_keydown_callback(0, 0, 1, key_callback); + emscripten_set_keyup_callback(0, 0, 1, key_callback); + emscripten_set_mousedown_callback(0, 0, 1, mouse_callback); + emscripten_set_mouseup_callback(0, 0, 1, mouse_callback); + + emscripten_set_touchstart_callback(0, 0, 1, touch_callback); + emscripten_set_touchend_callback(0, 0, 1, touch_callback); + +} + +//------------------------------------------------------------------------------ +// Graphics functions +//------------------------------------------------------------------------------ + +static const char vShaderStr[] = +"attribute vec4 a_position;\n" +"attribute vec2 a_texCoord;\n" +"uniform vec4 u_posTrans;\n" +"uniform vec4 u_uvTrans;\n" +"varying vec2 v_texCoord;\n" +"void main() {\n" +" vec2 pos = a_position.xy * u_posTrans.zw + u_posTrans.xy;\n" +" gl_Position = vec4(pos, 0, 1);\n" +" v_texCoord = a_texCoord * u_uvTrans.zw + u_uvTrans.xy;\n" +"}\n"; + +static const char fShaderStr[] = +"precision mediump float;\n" +"varying vec2 v_texCoord;\n" +"uniform sampler2D s_texture;\n" +"uniform vec4 u_color;\n" +"void main() {\n" +" gl_FragColor = texture2D(s_texture, v_texCoord) * u_color;\n" +"}\n"; + +static GLuint programObject = 0; +static GLint a_position = 0; +static GLint a_texCoord = 0; +static GLint u_posTrans = 0; +static GLint u_uvTrans = 0; +static GLint u_color = 0; +static GLint s_texture = 0; + +static GLuint vertexObject = 0; +static GLuint indexObject = 0; +static GLuint textureId = 0; +static GLuint whiteTexture = 0; + +static int screenSizeX = 100; +static int screenSizeY = 100; +static int transColor = 0; + +struct GraphData { + GLint texture; + GLint w; + GLint h; + float x; + float y; + float sx; + float sy; +}; + +static GraphData graphArray[128] = {0}; +static int graphLoadId = 1; + +static TTF_Font *font = NULL; + + +// 画面モードを設定する +int SetGraphMode(int ScreenSizeX, int ScreenSizeY, int ColorBitDepth, int RefreshRate) { + screenSizeX = ScreenSizeX; + screenSizeY = ScreenSizeY; + return 0; +} + +GLuint Create1x1Texture(byte r, byte g, byte b, byte a) +{ + // Texture object handle + GLuint textureId; + + // 2x2 Image, 3 bytes per pixel (R, G, B) + GLubyte pixels[4 * 4] = { + r, g, b, a, + r, g, b, a, + r, g, b, a, + r, g, b, a, + }; + + // Use tightly packed data + glPixelStorei ( GL_UNPACK_ALIGNMENT, 1 ); + + // Generate a texture object + glGenTextures ( 1, &textureId ); + + // Bind the texture object + glBindTexture ( GL_TEXTURE_2D, textureId ); + + // Load the texture + glTexImage2D ( GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels ); + + // Set the filtering mode + glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST ); + glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST ); + glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE ); + glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE ); + + return textureId; +} + + +// ライブラリ初期化関数 +int DxLib_Init(void) { + EGLint attribList[] = + { + EGL_RED_SIZE, 5, + EGL_GREEN_SIZE, 6, + EGL_BLUE_SIZE, 5, + EGL_ALPHA_SIZE, EGL_DONT_CARE, + EGL_DEPTH_SIZE, EGL_DONT_CARE, + EGL_STENCIL_SIZE, EGL_DONT_CARE, + EGL_SAMPLE_BUFFERS, 0, + EGL_NONE + }; + + if ( !CrateWindow (screenSizeX, screenSizeY) ) { + return -1; + } + + if ( !CreateEGLContext (attribList) ) { + return -1; + } + + // Load the shaders and get a linked program object + programObject = esLoadProgram ( vShaderStr, fShaderStr ); + + // Get the attribute locations + a_position = glGetAttribLocation ( programObject, "a_position" ); + a_texCoord = glGetAttribLocation ( programObject, "a_texCoord" ); + u_posTrans = glGetUniformLocation ( programObject, "u_posTrans" ); + u_uvTrans = glGetUniformLocation ( programObject, "u_uvTrans" ); + u_color = glGetUniformLocation ( programObject, "u_color" ); + + // Get the sampler location + s_texture = glGetUniformLocation ( programObject, "s_texture" ); + + // Load the texture + whiteTexture = Create1x1Texture(255, 255, 255, 255); + + // Setup the vertex data + GLfloat vVertices[] = { + // normal + 0, 0, 0, 0, 0, + 0, 1, 0, 0, 1, + 1, 0, 0, 1, 0, + 1, 1, 0, 1, 1, + + // flip + 0, 0, 0, 1, 0, + 0, 1, 0, 1, 1, + 1, 0, 0, 0, 0, + 1, 1, 0, 0, 1, + + // loop + 0, 0, 0, 1, 0, + 0, 1, 0, 1, 1, + 1, 1, 0, 0, 1, + 1, 0, 0, 0, 0, + }; + GLushort indices[] = { 0, 1, 2, 1, 2, 3 }; + + glGenBuffers(1, &vertexObject); + glBindBuffer(GL_ARRAY_BUFFER, vertexObject ); + glBufferData(GL_ARRAY_BUFFER, sizeof(vVertices), vVertices, GL_STATIC_DRAW ); + + glGenBuffers(1, &indexObject); + glBindBuffer ( GL_ELEMENT_ARRAY_BUFFER, indexObject ); + glBufferData ( GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW ); + + // Use the program object + glUseProgram ( programObject ); + + // Load the vertex position + glBindBuffer (GL_ARRAY_BUFFER, vertexObject ); + glVertexAttribPointer ( a_position, 3, GL_FLOAT, GL_FALSE, 5 * 4, 0 ); + glVertexAttribPointer ( a_texCoord, 2, GL_FLOAT, GL_FALSE, 5 * 4, (void*)(3 * 4) ); + + glEnableVertexAttribArray ( a_position ); + glEnableVertexAttribArray ( a_texCoord ); + + // Bind the texture + glActiveTexture ( GL_TEXTURE0 ); + glBindTexture ( GL_TEXTURE_2D, textureId ); + + // Alpha blend + glEnable ( GL_BLEND ); + glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ); + + // Set the sampler texture unit to 0 + glUniform1i ( s_texture, 0 ); + glBindBuffer ( GL_ELEMENT_ARRAY_BUFFER, indexObject ); + + // Font + font = TTF_OpenFont("sans-serif", 40); + + InputInit(); + return 0; +} + +// ライブラリ使用の終了関数 +int DxLib_End(void) { + return 0; +} + + // 画像ファイルのメモリへの読みこみ int LoadGraph(const char *FileName, int NotUse3DFlag) { - return 0; + if (graphLoadId >= sizeof(graphArray) / sizeof(graphArray[0])) { + printf("Too many images.\n"); + return 0; + } + + SDL_Surface *surface = IMG_Load(FileName); + if (surface == NULL) { + printf("Failed to load image: %s\n", FileName); + return 0; + } + + byte* data = (byte*)surface->pixels; + int size = surface->w * surface->h; + while (size > 0) { + if (data[0] == 153 && data[1] == 255 && data[2] == 255) { + data[3] = 0; + } + data += 4; + size--; + } + + GLuint texture = 0; + glPixelStorei ( GL_UNPACK_ALIGNMENT, 1 ); + glGenTextures( 1, &texture ); + glBindTexture( GL_TEXTURE_2D, texture ); + glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, surface->w, surface->h, 0, + GL_RGBA, GL_UNSIGNED_BYTE, surface->pixels ); + + glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST ); + glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE ); + glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE ); + + + GraphData *g = &graphArray[graphLoadId]; + g->texture = texture; + g->w = surface->w; + g->h = surface->h; + g->x = 0; + g->y = 0; + g->sx = 1; + g->sy = 1; + + return graphLoadId++; } // 指定のグラフィックの指定部分だけを抜き出して新たなグラフィックハンドルを作成する int DerivationGraph(int SrcX, int SrcY, int Width, int Height, int SrcGraphHandle) { - return 0; + if (graphLoadId >= sizeof(graphArray) / sizeof(graphArray[0])) { + printf("Too many images."); + return 0; + } + + GraphData *g1 = &graphArray[SrcGraphHandle]; + GraphData *g = &graphArray[graphLoadId]; + + g->texture = g1->texture; + g->w = Width; + g->h = Height; + g->x = (float)SrcX / g1->w; + g->y = (float)SrcY / g1->h; + g->sx = (float)Width / g1->w; + g->sy = (float)Height / g1->h; + + return graphLoadId++; } // グラフィックのサイズを得る int GetGraphSize(int GrHandle, int *SizeXBuf, int *SizeYBuf) { + GraphData *g = &graphArray[GrHandle]; + *SizeXBuf = g->w; + *SizeYBuf = g->h; return 0; } @@ -35,21 +596,6 @@ int ChangeVolumeSoundMem(int VolumePal, int SoundHandle) { return 0; } -// 画面モードを設定する -int SetGraphMode(int ScreenSizeX, int ScreenSizeY, int ColorBitDepth, int RefreshRate) { - return 0; -} - -// ライブラリ初期化関数 -int DxLib_Init(void) { - return 0; -} - -// ライブラリ使用の終了関数 -int DxLib_End(void) { - return 0; -} - // 描画するフォントのサイズをセットする int SetFontSize(int FontSize) { return 0; @@ -69,7 +615,8 @@ int ClearDrawScreen() { // 3原色値から現在の画面モードに対応した色データ値を得る uint32_t GetColor(int Red, int Green, int Blue) { - return 0; + byte Alpha = 0xff; + return Red | (Green << 8) | (Blue << 16) | (Alpha << 24); } // 書式指定文字列を描画する @@ -95,11 +642,14 @@ int ScreenFlip(void) { } // ミリ秒単位の精度を持つカウンタの現在値を得る int GetNowCount(int UseRDTSCFlag) { - return 0; + struct timeval t1; + struct timezone tz; + gettimeofday(&t1, &tz); + return ((t1.tv_sec) * 1000 + (t1.tv_usec) / 1000); } // ジョイバッドの入力状態取得 int GetJoypadInputState(int InputType) { - return 0; + return joypad_state; } // メモリに読み込んだWAVEデータの再生を止める int StopSoundMem(int SoundHandle) { @@ -111,6 +661,9 @@ int PlaySoundMem(int SoundHandle, int PlayType, int TopPositionFlag) { } // キーボードの入力状態取得 int CheckHitKey(int KeyCode) { + if (KeyCode > 0 && KeyCode < 256) { + return key_state[KeyCode]; + } return 0; } // 指定の時間だけ処理をとめる @@ -155,3 +708,90 @@ int CheckSoundMem(int SoundHandle) { return 0; } +int color; +int mirror; +int prevtexture = 0; + +//画像関係 +//{ +//色かえ(指定) +void setcolor(int red, int green, int blue) { + color = GetColor(red, green, blue); +} + +void clearscreen() { + float r = (color & 0xff) / 255.0f; + float g = ((color >> 8) & 0xff) / 255.0f; + float b = ((color >> 16) & 0xff) / 255.0f; + glClearColor(r, g, b, 1); + glClear ( GL_COLOR_BUFFER_BIT ); + prevtexture = 0; +} + +//色かえ(黒)(白) +void setc0() {color = GetColor(0, 0, 0); } +void setc1() {color = GetColor(255, 255, 255); } + +static void set_texture(int id) { + if (prevtexture != id) { + prevtexture = id; + glBindTexture ( GL_TEXTURE_2D, id ); + } +} + +static void set_transform(float x, float y, float w, float h) { + float sx = 2.0f / screenSizeX; + float sy = -2.0f / screenSizeY; + glUniform4f (u_posTrans, x * sx - 1, y * sy + 1, w * sx, h * sy); +} + +//線 +void drawline(int a, int b, int c, int d) {DrawLine(a, b, c, d, color); } +//四角形(塗り無し) +void drawrect(int x, int y, int w, int h) { + set_texture(whiteTexture); + set_transform(x, y, w, h); + + float r = (color & 0xff) / 255.0f; + float g = ((color >> 8) & 0xff) / 255.0f; + float b = ((color >> 16) & 0xff) / 255.0f; + glUniform4f (u_color, r, g, b, 1); + glDrawArrays ( GL_LINE_LOOP, 8, 4); +} + +//四角形(塗り有り) +void fillrect(int x, int y, int w, int h) { + set_texture(whiteTexture); + set_transform(x, y, w, h); + + float r = (color & 0xff) / 255.0f; + float g = ((color >> 8) & 0xff) / 255.0f; + float b = ((color >> 16) & 0xff) / 255.0f; + glUniform4f (u_color, r, g, b, 1); + glDrawArrays ( GL_TRIANGLE_STRIP, 0, 4); +} + +//円(塗り無し) +void drawarc(int a, int b, int c, int d) {DrawOval(a, b, c, d, color, FALSE); } +//円(塗り有り) +void fillarc(int a, int b, int c, int d) {DrawOval(a, b, c, d, color, TRUE); } + +//画像表示 +void drawimage(int mx, int a, int b) { + GraphData *g = &graphArray[mx]; + set_texture(g->texture); + set_transform(a, b, g->w, g->h); + glUniform4f (u_uvTrans, g->x, g->y, g->sx, g->sy); + glUniform4f (u_color, 1, 1, 1, 1); + + if (mirror == 0) + glDrawArrays ( GL_TRIANGLE_STRIP, 0, 4); + if (mirror == 1) + glDrawArrays ( GL_TRIANGLE_STRIP, 4, 4); +} + +//反転 +void setre() {} //g.setFlipMode(Graphics.FLIP_HORIZONTAL);} +void setre2() {} //g.setFlipMode(Graphics.FLIP_VERTICAL);} +void setno() {} //g.setFlipMode(Graphics.FLIP_NONE);} + diff --git a/dxlib.h b/dxlib.h index bc1433e..821cfeb 100644 --- a/dxlib.h +++ b/dxlib.h @@ -24,28 +24,25 @@ typedef uint8_t byte; -#define PAD_INPUT_LEFT (0x00000002) // ←チェックマスク -#define PAD_INPUT_RIGHT (0x00000004) // →チェックマスク -#define PAD_INPUT_DOWN (0x00000001) // ↓チェックマスク -#define KEY_INPUT_F1 1 // F1キー +#define PAD_INPUT_DOWN (0x00000001) +#define PAD_INPUT_LEFT (0x00000002) +#define PAD_INPUT_RIGHT (0x00000004) +#define PAD_INPUT_UP (0x00000008) -#define KEY_INPUT_0 0 // 0キー -#define KEY_INPUT_O 0 // Oキー -#define PAD_INPUT_UP (0x00000008) // ↑チェックマスク -#define KEY_INPUT_Z 0 // Zキー -#define KEY_INPUT_1 0 // Zキー -#define KEY_INPUT_ESCAPE 0 // エスケープキー -#define KEY_INPUT_SPACE 0 // エスケープキー -#define KEY_INPUT_RETURN 0 // エスケープキー +#define KEY_INPUT_F1 1 +#define KEY_INPUT_0 48 +#define KEY_INPUT_1 49 +#define KEY_INPUT_O 79 +#define KEY_INPUT_Z 90 +#define KEY_INPUT_ESCAPE 27 +#define KEY_INPUT_SPACE 32 +#define KEY_INPUT_RETURN 13 -// グラフィックに設定する透過色をセットする -extern int SetTransColor(int Red, int Green, int Blue); - // 画像ファイルのメモリへの読みこみ extern int LoadGraph(const char *FileName, int NotUse3DFlag = false); @@ -77,10 +74,6 @@ extern int DxLib_End(void); extern int SetFontSize(int FontSize); // フォントの太さをセット extern int SetFontThickness(int ThickPal); -// 描画先画面のセット -extern int SetDrawScreen(int DrawScreen); -// 画面の状態を初期化する -extern int ClearDrawScreen(); // 3原色値から現在の画面モードに対応した色データ値を得る extern uint32_t GetColor(int Red, int Green, int Blue); diff --git a/loadg.cpp b/loadg.cpp index 06f4abc..fbb6780 100644 --- a/loadg.cpp +++ b/loadg.cpp @@ -29,9 +29,6 @@ void loadg(void) { //画像読み込み - // 透過色を変更 - SetTransColor(9 * 16 + 9, 255, 255); - //プレイヤー mgrap[0] = LoadGraph("res/player.png"); //ブロック @@ -49,7 +46,7 @@ void loadg(void) { //おまけ2 mgrap[7] = LoadGraph("res/omake2.png"); //タイトル - mgrap[30] = LoadGraph("res/syobon3.PNG"); + mgrap[30] = LoadGraph("res/syobon3.png"); //プレイヤー読み込み diff --git a/main.cpp b/main.cpp index 15f31a4..fd24fa5 100644 --- a/main.cpp +++ b/main.cpp @@ -54,9 +54,11 @@ static int rand(int Rand); void end(); //描画 -int color; +extern int color; +extern int mirror; void setfont(int a); void setcolor(int red, int green, int blue); +void clearscreen(); void setc0(); void setc1(); void drawpixel(int a, int b); @@ -66,11 +68,7 @@ void fillrect(int a, int b, int c, int d); void drawarc(int a, int b, int c, int d); void fillarc(int a, int b, int c, int d); int grap[161][8], mgrap[51]; -int loadimage(const char* b); -int loadimage(int a, int x, int y, int r, int z); -int mirror; void drawimage(int mx, int a, int b); -void drawimage(int mx, int a, int b, int c, int d, int e, int f); void setre(); void setre2(); void setno(); @@ -231,7 +229,6 @@ long stime; // プログラムは WinMain から始まります int main() { - //画面サイズ設定 SetGraphMode(fxmax / 100, fymax / 100, 16); @@ -246,7 +243,7 @@ int main() { SetFontThickness(4); // Main loop - emscripten_set_main_loop(Mainprogram, 60, 1); + emscripten_set_main_loop(Mainprogram, 0, 1); // ソフトの終了 return 0; @@ -258,17 +255,12 @@ int main() { void rpaint() { //ダブルバッファリング - SetDrawScreen(DX_SCREEN_BACK); - - ClearDrawScreen(); - setcolor(0, 0, 0); - //if (stagecolor==1)setcolor(170,170,255); if (stagecolor == 1) setcolor(160, 180, 250); if (stagecolor == 2) setcolor(10, 10, 10); if (stagecolor == 3) setcolor(160, 180, 250); if (stagecolor == 4) setcolor(10, 10, 10); - fillrect(0, 0, fxmax, fymax); + clearscreen(); /* @@ -1118,11 +1110,11 @@ void rpaint() { } //if (mainproc==10){ -//タイトル + //タイトル if (mainproc == 100) { setcolor(160, 180, 250); - fillrect(0, 0, fxmax, fymax); + clearscreen(); drawimage(mgrap[30], 240 - 380 / 2, 60); @@ -1140,11 +1132,11 @@ void rpaint() { setcolor(0, 0, 0); str("Enterキーを押せ!!", 240 - 8 * 20 / 2, 250); - } //if (mainproc==100){ + } //if (mainproc==100) -//DrawFormatString(230,200,GetColor(255,255,255)," × %d,%d,%d",sta,stb,stc); + //DrawFormatString(230,200,GetColor(255,255,255)," × %d,%d,%d",sta,stb,stc); ScreenFlip(); @@ -1156,11 +1148,14 @@ void rpaint() { + //メインプログラム void Mainprogram() { + int time = GetNowCount(); + if (time - stime < 1000 / 30) + return; - stime = long(GetNowCount()); - + stime = time; if (ending == 1) mainproc = 2; @@ -3343,7 +3338,7 @@ void Mainprogram() { //30-fps xx[0] = 30; if (CheckHitKey(KEY_INPUT_SPACE) == 1) {xx[0] = 60; } - wait2(stime, long(GetNowCount()), 1000 / xx[0]); + wait2(stime, GetNowCount(), 1000 / xx[0]); //wait(20); } //Mainprogram() @@ -3482,67 +3477,6 @@ void end() { } -//画像関係 -//{ -//色かえ(指定) -void setcolor(int red, int green, int blue) { - color = GetColor(red, green, blue); -} -//色かえ(黒)(白) -void setc0() {color = GetColor(0, 0, 0); } -void setc1() {color = GetColor(255, 255, 255); } - -//点 -void drawpixel(int a, int b) {DrawPixel(a, b, color); } -//線 -void drawline(int a, int b, int c, int d) {DrawLine(a, b, c, d, color); } -//四角形(塗り無し) -void drawrect(int a, int b, int c, int d) {DrawBox(a, b, a + c, b + d, color, FALSE); } -//四角形(塗り有り) -void fillrect(int a, int b, int c, int d) {DrawBox(a, b, a + c, b + d, color, TRUE); } -//円(塗り無し) -void drawarc(int a, int b, int c, int d) {DrawOval(a, b, c, d, color, FALSE); } -//円(塗り有り) -void fillarc(int a, int b, int c, int d) {DrawOval(a, b, c, d, color, TRUE); } - -//画像の読み込み -int loadimage(const char* x) { - //mgrap[a]=LoadGraph(b); - return LoadGraph(x); -} -int loadimage(int a, int x, int y, int r, int z) { - return DerivationGraph(x, y, r, z, a); -} - -//画像表示 -void drawimage(int mx, int a, int b) { - if (mirror == 0) - DrawGraph(a, b, mx, TRUE); - if (mirror == 1) - DrawTurnGraph(a, b, mx, TRUE); -} -void drawimage(int mx, int a, int b, int c, int d, int e, int f) { - int m; - m = DerivationGraph(c, d, e, f, mx); - if (mirror == 0) - DrawGraph(a, b, m, TRUE); - if (mirror == 1) - DrawTurnGraph(a, b, m, TRUE); -} - -//反転 -void setre() {} //g.setFlipMode(Graphics.FLIP_HORIZONTAL);} -void setre2() {} //g.setFlipMode(Graphics.FLIP_VERTICAL);} -void setno() {} //g.setFlipMode(Graphics.FLIP_NONE);} - -/* - //文字 - void str(char d[],int a,int b){ - //char d[]=c; - DrawString(a,b,d,color); - } - */ - //文字 void str(const char *x, int a, int b) { //char d[]="あ"; @@ -3550,48 +3484,10 @@ void str(const char *x, int a, int b) { //DrawString(10,10,xs[3].c_str(),color); xx[2] = 4; - - } -/* - //数値を文字に変換 - void strchange(string x,int a){ - } - */ - -/* - //中央にあわせる//(font) - void str1(String c,int r,int b){ - int a=0,x=0; - int d=6; - - //x=c.length()*d;//tiny.6 - x=r*d; - a=120-x/2; - - g.drawString(c,a,b); - } - */ - - -//string→int -/* - char str[] = "12345"; - int num; - - num = atoi(str); - */ - - //文字ラベル変更 void setfont(int a) { - /* - if (a==0)g.setFont(Font.getFont(Font.SIZE_TINY)); - if (a==1)g.setFont(Font.getFont(Font.SIZE_SMALL)); - if (a==2)g.setFont(Font.getFont(Font.SIZE_MEDIUM)); - if (a==3)g.setFont(Font.getFont(Font.SIZE_LARGE)); - */ } //音楽再生 diff --git a/main.h b/main.h deleted file mode 100644 index c8ae658..0000000 --- a/main.h +++ /dev/null @@ -1,228 +0,0 @@ -#include "DxLib.h" -#include -#include -#include -using namespace std; - -void loadg(); - -//String 使用 - -//プログラム中 -//main-10 -//タイトル-100 -int main = 100, maintm = 0; - -//ステージ -int stagecolor = 0; -int sta = 1, stb = 4, stc = 0; - -//クイック -int fast = 1; - -//トラップ表示 -int trap = 1; - -//中間ゲート -int tyuukan = 0; - - -//スタッフロール -int ending = 0; - - -//ステージ読み込みループ(いじらない) -int stagerr, stagepoint; -//オーバーフローさせる -int over = 0; - -//ステージスイッチ -int stageonoff = 0; - - -//メインプログラム -void Mainprogram(); -void rpaint(); -int maint; - - -//サブクラス -//(ウエイト系 -void wait(int interval); -void wait2(long stime, long etime, int FLAME_TIME); -int rand(int Rand); -void end(); - -//描画 -int color; -void setfont(int a); -void setcolor(int red, int green, int blue); -void setc0(); -void setc1(); -void drawpixel(int a, int b); -void drawline(int a, int b, int c, int d); -void drawrect(int a, int b, int c, int d); -void fillrect(int a, int b, int c, int d); -void drawarc(int a, int b, int c, int d); -void fillarc(int a, int b, int c, int d); -int grap[161][8], mgrap[51]; -int loadimage(string b); -int loadimage(int a, int x, int y, int r, int z); -int mirror; -void drawimage(int mx, int a, int b); -void drawimage(int mx, int a, int b, int c, int d, int e, int f); -void setre(); -void setre2(); -void setno(); -int oto[151]; -void ot(int x); void bgmchange(int x); - -//文字 -void str(string c, int a, int b); - - -//) - -void stagecls(); -void stage(); -void stagep(); - - - - - -//1-ステージ -//10-ステージ前 -// - - - -//ループ -int t, tt, t1, t2, t3, t4; - - -//初期化 -int zxon, zzxon; - -//キーコンフィグ -int key, keytm; - -//三角関数 -double pai = 3.1415926535; - - -//地面 -#define smax 31 -int sx, sco; -int sa[smax], sb[smax], sc[smax], sd[smax], stype[smax], sxtype[smax], sr[smax]; -int sgtype[smax]; - - - -//プレイヤー -int mainmsgtype; -int ma, mb, mnobia, mnobib, mhp; -int mc, md, macttype, atkon, atktm, mactsok, msstar, nokori = 2, mactp, mact; - -int mtype, mxtype, mtm, mzz; -int mzimen, mrzimen, mkasok, mmuki, mmukitm, mjumptm, mkeytm, mcleartm; -int mmutekitm, mmutekion; -int mztm, mztype; -int actaon[7]; -//メッセージ -int mmsgtm, mmsgtype; - -int mascrollmax = 21000; //9000 - - - - -//ブロック -void tyobi(int x, int y, int type); -void brockbreak(int t); -#define tmax 641 -int tco; -int ta[tmax], tb[tmax], tc[tmax], td[tmax], thp[tmax], ttype[tmax]; -int titem[tmax], txtype[tmax]; - -//メッセージブロック -int tmsgtm, tmsgtype, tmsgx, tmsgy, tmsgnobix, tmsgnobiy, tmsg; -void ttmsg(); void txmsg(string x, int a); -void setfont(int x, int y); - -//効果を持たないグラ -void eyobi(int xa, int xb, int xc, int xd, int xe, int xf, int xnobia, int xnobib, int xgtype, int xtm); -#define emax 201 -int eco; -int ea[emax], eb[emax], enobia[emax], enobib[emax], ec[emax], ed[emax]; -int ee[emax], ef[emax], etm[emax]; -int egtype[emax]; - - - -//敵キャラ -void ayobi(int xa, int xb, int xc, int xd, int xnotm, int xtype, int xxtype); -void tekizimen(); -#define amax 24 -int aco; -int aa[amax], ab[amax], anobia[amax], anobib[amax], ac[amax], ad[amax]; -int ae[amax], af[amax], abrocktm[amax]; -int aacta[amax], aactb[amax], azimentype[amax], axzimen[amax]; -int atype[amax], axtype[amax], amuki[amax], ahp[amax]; -int anotm[amax], anx[160], any[160]; -int atm[amax], a2tm[amax]; -int amsgtm[amax], amsgtype[amax]; - -//敵出現 -#define bmax 81 -int bco; -int ba[bmax], bb[bmax], btm[bmax]; -int btype[bmax], bxtype[bmax], bz[bmax]; - - -//背景 -#define nmax 41 -int nxxmax, nco; -int na[nmax], nb[nmax], nc[nmax], nd[nmax], ntype[nmax]; -int ne[nmax], nf[nmax], ng[nmax], nx[nmax]; - - -//リフト -#define srmax 21 -int srco; -int sra[srmax], srb[srmax], src[srmax], srd[srmax], sre[srmax], srf[srmax]; -int srtype[srmax], srgtype[srmax], sracttype[srmax], srsp[srmax]; -int srmuki[srmax], sron[srmax], sree[srmax]; -int srsok[srmax], srmovep[srmax], srmove[srmax]; - - - - - -//スクロール範囲 -int fx = 0, fy = 0, fzx, fzy, scrollx, scrolly; -//全体のポイント -int fma = 0, fmb = 0; -//強制スクロール -int kscroll = 0; -//画面サイズ(ファミコンサイズ×2)(256-224) -int fxmax = 48000, fymax = 42000; - - - -//ステージ -byte stagedate[17][2001]; - -//画面黒 -int blacktm = 1, blackx = 0; - - - -//自由な値 -int xx[91]; -double xd[11]; -string xs[31]; - - -//タイマー測定 -long stime; \ No newline at end of file