Original Post
Well... This is lame. I can't quite get this input thing working properly (second time having tried input, if I still had my first source I'd only have the backspace and uppercase to worry about). Basically, text won't appear unless you hit backspace several times (didn't act like that the first time), and due to the unicode stuff, I don't have access to shift-uppercase. What I'm trying to accomplish is a backspace that works (instead of turning things into t's or other letters) and to have the user's input always be uppercase.
Here's my unpleasant-to-the-eye sourcecode, please be gentle. @_@ I'm including all of what is in this file, just in case:
If you see any other ways I could improve this wreck, I'll accept suggestions, but I really just want to get backspace and caps working. I hope someone can and will help. Any assistance is greatly appreciated and will be (likely) included in my credits.
[EDIT: I notice that I kinda accidentally enabled unicode twice. Wasn't intentional.]
Here's my unpleasant-to-the-eye sourcecode, please be gentle. @_@ I'm including all of what is in this file, just in case:
#include "/home/mdeans/Fiendlight/plyr.h"
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "SDL/SDL_mixer.h"
#include "SDL/SDL_thread.h"
#include "SDL/SDL_ttf.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>
#define WINDOW_WIDTH 640
#define WINDOW_HEIGHT 480
SDL_Surface *surface[34]; ///0 = window, 1 = frame, 2-10 = lower box's text, 11-20 = upper box's text
SDL_Event event;
SDL_Rect src,dest;
SDL_Color font_color;
TTF_Font* mt_font;
char tr_panel[40];
char br_panel[20];
char user_input[9];
void plyr_talk();
void plyr_attk();
void plyr_ztat();
void plyr_get();
void plyr_look();
void plyr_warp(int how,int where[3]);
int input_slot;
int game_start(),game_load(),game_credits(),game_field(),game_battle();
int main(){
if(SDL_Init(SDL_INIT_VIDEO)!=0){
printf("Unable to initialise SDL: %s\n", SDL_GetError());
return 1;
}
else{
SDL_EnableUNICODE(SDL_ENABLE);
if(TTF_Init()!=0){
printf("Unable to initialise SDL_TTF: %s\n", SDL_GetError());
return 1;
}
else if(TTF_Init()==0){
mt_font = TTF_OpenFont("gfx/FreeMono.ttf",16);
if(mt_font==NULL){
printf("Unable to load font: %s: %s",mt_font,TTF_GetError());
}
}
SDL_WM_SetCaption("Fiendlight",NULL);
surface[0] = SDL_SetVideoMode(WINDOW_WIDTH,WINDOW_HEIGHT,32,SDL_DOUBLEBUF);
if(surface[0]==NULL){
printf("Unable to set video mode: %s\n", SDL_GetError());
return 1;
}
surface[1] = IMG_Load("gfx/frame.png");
SDL_Color font_color = {180,180,180};
surface[2] = TTF_RenderText_Solid(mt_font,"[S]tart new quest",font_color);
surface[3] = TTF_RenderText_Solid(mt_font,"[C]ontinue your journey",font_color);
surface[4] = TTF_RenderText_Solid(mt_font,"[V]iew the credits",font_color);
src.x = 0;
src.y = 0;
src.h = surface[1]->h;
src.w = surface[1]->w;
dest.h = 480;
dest.w = 640;
dest.x = 0;
dest.y = 0;
SDL_FillRect(surface[0],NULL,SDL_MapRGBA(surface[0]->format,0,0,0,255));
SDL_BlitSurface(surface[1], &src, surface[0], &dest);
dest.x = 410;
dest.y = 280;
SDL_BlitSurface(surface[2], &src, surface[0], &dest);
dest.y = 296;
SDL_BlitSurface(surface[3], &src, surface[0], &dest);
dest.y = 312;
SDL_BlitSurface(surface[4], &src, surface[0], &dest);
SDL_Flip(surface[0]);
//while(1){
while (SDL_WaitEvent(&event) !=0){
switch (event.type) {
case SDL_QUIT:
exit(0);
case SDL_KEYDOWN:
if(event.key.keysym.sym == SDLK_s){
game_start();
}
else if(event.key.keysym.sym == SDLK_c){
///game_load();
}
else if(event.key.keysym.sym == SDLK_v){
///game_credits();
}
}
}
//}
}
}
game_start(){
surface[1] = IMG_Load("gfx/frame.png");
SDL_Color font_color = {180,180,180};
surface[2] = TTF_RenderText_Solid(mt_font,"By what name shalt",font_color);
surface[3] = TTF_RenderText_Solid(mt_font,"thou be known?",font_color);
surface[4] = TTF_RenderText_Solid(mt_font,user_input,font_color);
src.x = 0;
src.y = 0;
src.h = surface[1]->h;
src.w = surface[1]->w;
dest.h = 480;
dest.w = 640;
dest.x = 0;
dest.y = 0;
SDL_FillRect(surface[0],NULL,SDL_MapRGBA(surface[0]->format,0,0,0,255));
SDL_BlitSurface(surface[1], &src, surface[0], &dest);
dest.x = 414;
dest.y = 280;
SDL_BlitSurface(surface[2], &src, surface[0], &dest);
dest.y = 296;
SDL_BlitSurface(surface[3], &src, surface[0], &dest);
dest.y = 312;
SDL_BlitSurface(surface[4], &src, surface[0], &dest);
SDL_Flip(surface[0]);
input_slot = 0;
while(1){
while(SDL_WaitEvent(&event)!=0){
switch(event.type){
case SDL_QUIT:
exit(0);
case SDL_KEYDOWN:
if(event.key.keysym.sym == SDLK_RETURN){
printf(user_input);
}
else if(event.key.keysym.sym != SDLK_RETURN){
if(event.key.keysym.sym != SDLK_BACKSPACE){
if(input_slot <= 8){
input_slot = input_slot + 1;
}
SDL_EnableUNICODE(SDL_ENABLE);
user_input[input_slot] = event.key.keysym.unicode;
}
else if(event.key.keysym.sym == SDLK_BACKSPACE){
user_input[input_slot] = "_";
if(input_slot>=0){
input_slot = input_slot - 1;
}
}
}
surface[1] = IMG_Load("gfx/frame.png");
SDL_Color font_color = {180,180,180};
surface[2] = TTF_RenderText_Solid(mt_font,"By what name shalt",font_color);
surface[3] = TTF_RenderText_Solid(mt_font,"thou be known?",font_color);
surface[4] = TTF_RenderText_Solid(mt_font,user_input,font_color);
src.x = 0;
src.y = 0;
src.h = surface[1]->h;
src.w = surface[1]->w;
dest.h = 480;
dest.w = 640;
dest.x = 0;
dest.y = 0;
SDL_FillRect(surface[0],NULL,SDL_MapRGBA(surface[0]->format,0,0,0,255));
SDL_BlitSurface(surface[1], &src, surface[0], &dest);
dest.x = 414;
dest.y = 280;
SDL_BlitSurface(surface[2], &src, surface[0], &dest);
dest.y = 296;
SDL_BlitSurface(surface[3], &src, surface[0], &dest);
dest.y = 312;
SDL_BlitSurface(surface[4], &src, surface[0], &dest);
SDL_Flip(surface[0]);
}
}
}
}
If you see any other ways I could improve this wreck, I'll accept suggestions, but I really just want to get backspace and caps working. I hope someone can and will help. Any assistance is greatly appreciated and will be (likely) included in my credits.
[EDIT: I notice that I kinda accidentally enabled unicode twice. Wasn't intentional.]